-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
104 lines (91 loc) · 3.9 KB
/
MyFileWriter.java
File metadata and controls
104 lines (91 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.io.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
public class MyFileWriter {
public static void main(String[] args) {
String data = "Hello, World!"; // what goes into the file
String fileName1 = "example.txt";
String fileName2 = "example2.txt";
String fileName3 = "example3.txt";
String fileName4 = "example4.txt";
String fileName5 = "example5.txt";
// 1. Using FileWriter
try (FileWriter writer = new FileWriter(fileName1)) {
writer.write(data);
} catch (IOException e) {
e.printStackTrace();
}
// 2. Using BufferedWriter
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName2))) {
bufferedWriter.write(data);
} catch (IOException e) {
e.printStackTrace();
}
// 3. Using FileOutputStream
try (FileOutputStream outputStream = new FileOutputStream(fileName3)) {
outputStream.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
// 4. Using BufferedOutputStream
try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(fileName4))) {
bufferedOutputStream.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
// 5. Using Files (java.nio.file)
try {
Files.write(Paths.get(fileName5), data.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void createHiddenFile() {
String data = "thisIsMyPassword";
String secretFileName = ".hiddenFileWithPassword";
try {
Files.write(Paths.get(secretFileName), data.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void createRegularFile() {
String data = "this has confidential info ";
String regularFileName = "personal.txt";
try {
Files.write(Paths.get(regularFileName), data.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
// String finalLocation = "\\.hiddenFolder\\personal.txt";
// Path pathSource = Paths.get("").toAbsolutePath();
// String pathSourceString = pathSource.toString();
// String StringFinalLocation = pathSourceString + finalLocation;
// Path pathDestination = Paths.get(StringFinalLocation);
// System.out.println("hiii " + pathDestination);
try {
String finalLocation = "\\.hiddenFolder\\personal.txt";
Path pathSource = Paths.get("").toAbsolutePath(); // to-do: add personal.txt end
String pathSourceString = pathSource.toString();
String OriginalLocation = pathSourceString + "\\personal.txt";
String StringFinalLocation = pathSourceString + finalLocation;
Path FinalPathSource = Paths.get(OriginalLocation);
Path pathDestination = Paths.get(StringFinalLocation);
File source = new File(pathSource.toString());
System.out.println(source.exists());
System.out.println("hiii " + FinalPathSource);
Files.move(FinalPathSource, pathDestination, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void personalInfoWrite(){
String data = "My favorite ice cream flavor is not mint chip\nI don't like the color blue\n my birthday isn't June 21";
String regularFileName = "StuffAboutMe.txt";
try {
Files.write(Paths.get(regularFileName), data.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
}