-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
30 lines (24 loc) · 947 Bytes
/
MyFileWriter.java
File metadata and controls
30 lines (24 loc) · 947 Bytes
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
package Activity1;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class MyFileWriter {
private String fileName;
public MyFileWriter(String fileName) {
this.fileName = fileName;
}
public void writeToFile(String content) {
try (PrintWriter writer = new PrintWriter(new FileWriter(fileName, true))) {
writer.println(content);
System.out.println("Content successfully written to the file.");
} catch (IOException e) {
System.err.println("An error occurred while writing to the file: " + e.getMessage());
}
}
public static void main(String[] args) {
MyFileWriter fileWriter = new MyFileWriter("example.txt");
fileWriter.writeToFile("Hello, World!");
fileWriter.writeToFile("This is a new line.");
}
// System.out.println("This is a goofy change that will probably be deleted");
}