add files
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package org.FileExamples.WritingFiles;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
public class WriteFileExample01 {
|
||||
static void main() {
|
||||
try {
|
||||
FileWriter fileWriter = new FileWriter("quotes.txt");
|
||||
fileWriter.write("I'm not superstitious, but I am a little stitious.\n");
|
||||
fileWriter.write("I am running away from my responsibilities. And it feels good.\n");
|
||||
fileWriter.close();
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
FileWriter fileWriter = new FileWriter("quotes.txt", true);
|
||||
fileWriter.write("I DECLARE BANKRUPTCY!");
|
||||
fileWriter.close();
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.FileExamples.WritingFiles;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.OpenOption;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class WriteFileExample02 {
|
||||
static void main() {
|
||||
|
||||
|
||||
try {
|
||||
Path path = Path.of("quotes.txt");
|
||||
List<String> lines = List.of(
|
||||
"I'm not superstitious, but I am a little stitious.",
|
||||
"I am running away from my responsibilities. And it feels good."
|
||||
);
|
||||
Files.write(path, lines);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
Path path = Path.of("quotes.txt");
|
||||
List<String> lines = List.of(
|
||||
"I DECLARE BANKRUPTCY!"
|
||||
);
|
||||
Files.write(path, lines, StandardOpenOption.APPEND);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user