import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class WriteTextFile {
public static void main(String[] args) {
Path file = Paths.get("D:\\resources\\data.txt");
List<String> lines = new ArrayList<>();
lines.add("Lorem Ipsum is simply dummy text of the printing ");
lines.add("and typesetting industry. Lorem Ipsum has been the ");
lines.add("industry's standard dummy text ever since the 1500s, ");
lines.add("when an unknown printer took a galley of type and ");
lines.add("scrambled it to make a type specimen book.");
try {
//
// Write lines of text to a file.
//
Files.write(file, lines, StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
public class Main {
public static void main(String[] args) {
Path myDir = Paths.get("D:/data");
//use the above Path instance as an anchor
Path writeFile = myDir.resolve("message.txt");
try {
"old" Java 7
OutputStream out = writeFile.newOutputStream();
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(out));
br.write("This is a text file written through Path API!");
br.write("\n");
br.flush();
br.close();
out.flush();
out.close();
//new Java 7
BufferedWriter br = Files.newBufferedWriter(writeFile,
Charset.forName("UTF-8"),
new OpenOption[] {StandardOpenOption.WRITE});
br.write("This is a text file written through Path API!");
br.write("\n");
br.flush();
br.close();
} catch (IOException e) {
System.err.println("Error: " + e.toString());
}
}
}
There are several options you can use when opening a file – conforming to documentation, we have:
• StandardOpenOption.CREATE (default behavior for writes)
Create a file if it does not already exist.
• StandardOpenOption.CREATE_NEW
Expect that the file does not already exist, and create it. Throw an exception if the file does already exist.
• StandardOpenOption.APPEND
Write new data to the end of the existing file.
• StandardOpenOption.TRUNCATE_EXISTING (default for writes)
Remove all data from an existing file when opening it.
• StandardOpenOption.NOFOLLOW_LINKS
Throw an exception if it is necessary to resolve a symbolic link to open a file.
• StandardOpenOption.SPARSE
Suggest that the file will be sparse so the underlying operating system can optimize for that use case. File systems that don't support sparse files will ignore this hint.
• StandardOpenOption.DSYNC
Write data to the underlying disk immediately. Do not use native buffering. This does not affect buffering internal to Java, such as that performed by BufferedInputStream and BufferedWriter.
• StandardOpenOption.SYNC
Write data and metadata (attributes) to the underlying disk immediately.
• StandardOpenOption.READ
Open for reading.
• StandardOpenOption.WRITE
Open for writing.
Per example, you can use these options to explicitly open a file for reading, like this:
OutputStream out = writeFile.newOutputStream(new OpenOption[]
{StandardOpenOption.WRITE});
Note: Not all of these are mutually exclusive. You can use several when opening a file.
1 comment:
The java.nio.file.Files class defines the newBufferedWriter method that would shorten this example, eg:
March 6, 2011 at 7:03 AMtry (BufferedWriter writer = Files.newBufferedWriter(writeFile, cs)) {
...
}
Alternatively, just use the Files.write method, eg:
Files.write(writeFile, Arrays.asList("This is text"), Charset.forName("UTF-8"))
Post a Comment