Java – 写入文件

Java – 写入文件

原文: https://howtodoinjava.com/java/io/java-write-to-file/

在企业应用程序上工作时,有时需要用 Java 写文件。 在文件系统中编写报告。 尽管有多种方法可以做到这一点,但让我们快速浏览其中的几种方法以在需要时快速参考。

Table of Contents

Write File using BufferedWritter
Write File using FileWriter/PrintWriter
Write File using FileOutputStream
Write File using DataOutputStream
Write File using FileChannel
Write File using Java 7 Path

使用BufferedWritter写入文件

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

除非需要快速输出,否则建议将BufferedWriter包裹在其write()操作可能成本很高的Writer周围,例如FileWriterOutputStreamWriter

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

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

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

使用FileWriter/PrintWriter写入文件

FileWriter最干净的文件写入方式。 语法是自我解释的,易于阅读和理解。 FileWriter直接写入文件(性能较差),并且仅在写入次数较少时才应使用。

public static void usingFileWriter() throws IOException 
{
	String fileContent = "Hello Learner !! Welcome to howtodoinjava.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 howtodoinjava.com.";

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

使用FileOutputStream写入文件

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

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

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

    outputStream.close();
}

使用DataOutputStream写入文件

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

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

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

	dataOutStream.close();
}

使用FileChannel写入文件

FileChannel可用于读取,写入,映射和操作文件。 如果要处理大文件,FileChannel可能比标准 IO 更快。

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

public static void usingFileChannel() throws IOException 
{
	String fileContent = "Hello Learner !! Welcome to howtodoinjava.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();
}

使用 Java 7 路径写入文件

Java 7 引入了Files工具类,我们可以使用其write函数来编写文件,而在内部,它是使用OutputStream将字节数组写入文件中的。

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

	Path path = Paths.get("c:/temp/samplefile7.txt");

    Files.write(path, fileContent.getBytes());
}

总结

  1. 如果我们尝试写入不存在的文件,则将首先创建该文件,并且不会引发任何异常(使用Path方法除外)。
  2. 写入文件内容以释放所有资源后,请始终关闭输出流。 它还将有助于避免损坏文件。
  3. 用途PrintWriter用于写入格式化的文本。
  4. FileOutputStream写入二进制数据。
  5. 使用DataOutputStream写入原始数据类型。
  6. 使用FileChannel写入较大的文件。

学习愉快!