上篇中分享了使用Java实现文件的读,本篇主要从写文件的角度做个简单总结。
代码展示:
package learnjava.fileIO;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
public class WriteFile {
static String content = "This is the file content.";
public static void main(String[] args) {
String path1 = new String("D:\software\intelli D\myDemo\src\learnjava\fileIO\file_wr_stream");
String path2 = new String("D:\software\intelli D\myDemo\src\learnjava\fileIO\file_wr_writer");
String path3 = new String("D:\software\intelli D\myDemo\src\learnjava\fileIO\file_wr_buf_stream");
String path4 = new String("D:\software\intelli D\myDemo\src\learnjava\fileIO\file_wr_buf_writer");
String path5 = new String("D:\software\intelli D\myDemo\src\learnjava\fileIO\file_wr_print_writer");
String path6 = new String("D:\software\intelli D\myDemo\src\learnjava\fileIO\file_wr_Files_writer");
fileOutputStream(path1);
fileWriter(path2);
fileBufferedOutputStream(path3);
fileBufferedWriter(path4);
filePrinterWriter(path5);
fileFilesWrite(path6);
}
// byte write, suit for binary file
public static void fileOutputStream(String dstPath) {
System.out.println("Write file by stream...");
try {
FileOutputStream fos = new FileOutputStream(dstPath);
byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
fos.write(bytes);
fos.close();
System.out.println("Write file successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
// buffer stream write in byte stream, with buffer
public static void fileBufferedOutputStream(String dstPath) {
System.out.println("Write file by BufferedOutputStream...");
try {
FileOutputStream fos = new FileOutputStream(dstPath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(content.getBytes(StandardCharsets.UTF_8));
bos.close();
fos.close();
System.out.println("Write file successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
// char write, suit for text file
public static void fileWriter(String dstPath) {
System.out.println("Write file by FileWriter...");
try {
FileWriter fw = new FileWriter(dstPath);
fw.write(content);
fw.close();
System.out.println("Write file successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
// buffer writer
public static void fileBufferedWriter(String dstPath) {
System.out.println("Write file by BufferedWriter...");
try {
FileWriter fw = new FileWriter(dstPath);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
fw.close();
System.out.println("Write file successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
// printer writer
public static void filePrinterWriter(String dstPath) {
System.out.println("Write file by PrinterWriter...");
try {
FileWriter fw = new FileWriter(dstPath);
PrintWriter pw = new PrintWriter(fw);
pw.write(content);
pw.close();
fw.close();
System.out.println("Write file successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
// Files writer
public static void fileFilesWrite(String dstPath) {
System.out.println("Write file by FilesWrite...");
try {
Files.write(Paths.get(dstPath), content.getBytes(StandardCharsets.UTF_8));
System.out.println("Write file successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
写文件也比较简单,上面我们实现了6种方式实现写文件,根据使用场景可以做相应选择,比如文件很大,就可以选择带缓冲的写文件,小文件的写入不考虑性能问题,就尽量简单编码。
参考: