在Java中如何将内容写入文件?
  • 使用 PrintWriter用于编写格式化文本。
  • 使用 FileOutputStream写入二进制数据。
  • 使用 DataOutputStream来编写原始数据类型。
  • 使用 FileChannel写入更大的文件。它也是在 Java 8 中编写文件的首选方式。

使用 DataOutputStream 写入文件

DataOutputStream允许应用程序以可移植的方式将原始 Java 数据类型写入输出流。
然后,应用程序可以使用数据输入流将数据读回。

public static void usingDataOutputStream() throws IOException 
{
	String fileContent = "Hello Learner !! Welcome to onitroad.com.";

	FileOutputStream outputStream = new FileOutputStream("c:/temp/samplefile5.txt");
	DataOutputStream dataOutStream = new DataOutputStream(new BufferedOutputStream(outputStream));
	dataOutStream.writeUTF(fileContent);

	dataOutStream.close();
}

使用 FileOutputStream 写入文件

使用 FileOutputStream将二进制数据写入文件。
FileOutputStream用于写入原始字节流,例如图像数据。
要写入字符流,请考虑使用 FileWriter

public static void usingFileOutputStream() throws IOException 
{
	String fileContent = "Hello Learner !! Welcome to onitroad.com.";

	FileOutputStream outputStream = new FileOutputStream("c:/temp/samplefile4.txt");
    byte[] strToBytes = fileContent.getBytes();
    outputStream.write(strToBytes);

    outputStream.close();
}

使用 FileWriter/PrintWriter 写入文件

FileWriter最干净的文件写入方式。
语法不言自明,易于阅读和理解。
FileWriter直接写入文件(性能较低),只有在写入次数较少时才应使用。

public static void usingFileWriter() throws IOException 
{
	String fileContent = "Hello Learner !! Welcome to onitroad.com.";

	FileWriter fileWriter = new FileWriter("c:/temp/samplefile2.txt");
    fileWriter.write(fileContent);
    fileWriter.close();
}

使用 PrintWriter将格式化文本写入文件。
此类实现了在 PrintStream中找到的所有打印方法,因此我们可以使用与 System.out.println()语句一起使用的所有格式。

public static void usingPrintWriter() throws IOException 
{
	String fileContent = "Hello Learner !! Welcome to onitroad.com.";

	FileWriter fileWriter = new FileWriter("c:/temp/samplefile3.txt");
    PrintWriter printWriter = new PrintWriter(fileWriter);
    printWriter.print(fileContent);
    printWriter.printf("Blog name is %s", "onitroad.com");
    printWriter.close();
}

Files.writeString() - 从 Java 11 开始

使用 Java 11 中引入的 writeString() 方法,我们可以使用单行语句将 String 写入文件。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class WriteToFile 
{
	public static void main(String[] args) throws IOException 
	{
	    Path fileName = Path.of("demo.txt");
	    String content  = "hello world !!";
	    Files.writeString(fileName, content);

	    String actual = Files.readString(fileName);
	    System.out.println(actual);
	}
}
https://onitroad.com 更多教程

Java 使用 BufferedWritter 写入文件

BufferedWritter是将内容写入文件的最简单方法。
它将文本写入字符输出流,缓冲字符以提供单个字符、数组和字符串的高效写入。

除非需要提示输出,否则建议将一个 BufferedWriter软件包在任何 Write()操作可能开销较大的 Writer周围,例如 FileWriterOutputStreamWriter

由于它在写入前进行缓冲,因此减少了 IO 操作,从而提高了性能。

public static void usingBufferedWritter() throws IOException 
{
	String fileContent = "Hello Learner !! Welcome to onitroad.com.";

	BufferedWriter writer = new BufferedWriter(new FileWriter("c:/temp/samplefile1.txt"));
	writer.write(fileContent);
	writer.close();
}

Java 7 - Files.write()

Java 7 引入了Files实用程序类,我们可以使用它的write()函数写入文件,内部使用OutputStream将字节数组写入文件。

public static void usingPath() throws IOException 
{
	String fileContent = "Hello Learner !! Welcome to onitroad.com.";
	Path path = Paths.get("c:/temp/samplefile7.txt");
        Files.write(path, fileContent.getBytes());
}

使用 FileChannel 写入文件

FileChannel可用于读取、写入、映射和操作文件。

如果我们正在写入大文件,FileChannel可以比标准 IO 更快。

多个并发线程可以安全地使用文件通道。

public static void usingFileChannel() throws IOException 
{
    String fileContent = "Hello Learner !! Welcome to onitroad.com.";
    RandomAccessFile stream = new RandomAccessFile("c:/temp/samplefile6.txt", "rw");
    FileChannel channel = stream.getChannel();
    byte[] strBytes = fileContent.getBytes();
    ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
    buffer.put(strBytes);
    buffer.flip();
    channel.write(buffer);
    stream.close();
    channel.close();
}
日期:2020-09-17 00:09:28 来源:oir作者:oir