🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Java – 写入文件 > 原文: [https://howtodoinjava.com/java/io/java-write-to-file/](https://howtodoinjava.com/java/io/java-write-to-file/) 在企业应用程序上工作时,有时需要**用 Java** 写文件。 在文件系统中编写报告。 尽管有多种方法可以做到这一点,但让我们快速浏览其中的几种方法以在需要时快速参考。 ```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`](https://docs.oracle.com/javase/10/docs/api/java/io/BufferedWriter.html)将内容写入文件的最简单方法。 它将文本写入字符输出流,**缓冲字符**,以便有效地写入单个字符,数组和字符串。 除非需要快速输出,否则建议将`BufferedWriter`包裹在其`write()`操作可能成本很高的`Writer`周围,例如`FileWriter`和`OutputStreamWriter`。 由于它在写入之前进行缓冲,因此它减少了**的 IO 操作**,因此提高了性能。 ```java 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`](https://docs.oracle.com/javase/10/docs/api/java/io/FileWriter.html)最干净的文件写入方式。 语法是自我解释的,易于阅读和理解。 `FileWriter`直接写入文件(*性能较差*),并且仅在写入次数较少时才应使用。 ```java 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`](https://docs.oracle.com/javase/10/docs/api/java/io/PrintWriter.html)将格式化的文本写入文件。 此类实现`PrintStream`中提供的所有打印方法,因此您可以使用`System.out.println()`语句使用的所有格式。 ```java 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`](https://docs.oracle.com/javase/10/docs/api/java/io/FileOutputStream.html)**将二进制数据写入文件**。 `FileOutputStream`用于写入原始字节流,例如图像数据。 要编写字符流,请考虑使用`FileWriter`。 ```java 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`](https://docs.oracle.com/javase/10/docs/api/java/io/DataOutputStream.html)允许应用程序**以可移植的方式将原始 Java 数据类型**写入输出流。 然后,应用程序可以使用数据输入流来读回数据。 ```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`](https://docs.oracle.com/javase/10/docs/api/java/nio/channels/FileChannel.html)可用于读取,写入,映射和操作文件。 如果要处理大文件,`FileChannel`可能比标准 IO 更快。 文件通道可以安全地供多个并发线程使用。 ```java 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`将字节数组写入文件中的。 ```java 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`写入较大的文件。 学习愉快!