软件开发中最常见的任务之一是读取和写入文件的能力。在 Java 中,写入文件是一个简单的过程,可以使用内置类和方法来完成。在本文中,我们将探讨如何使用 Java 写入文件并讨论所涉及的各种类和方法。Java 提供了FileWriter和PrintWriter等内置类来写入 Java 中的文件。
Java 写入文件示例
首先,让我们看一个用 Java 写入文件的简单示例。下面是如何使用FileWriter类在 Java 中将字符串写入文件的示例:文件写入器类
FileWriter类是 Java 中写入文件的另一种方法。它是一个字符流类,允许您将字符数据写入文件。下面的示例演示了如何使用FileWriter类将字符串写入文件:import java.io.FileWriter;
import java.io.IOException;
// Main method for the string to file in java starts from here,
public class WriteToFileExample {
public static void main(String[] args) {
// create a string to write text to file
String data = "Hello, world!";
try {
// create a FileWriter object with the file name
FileWriter writer = new FileWriter("output.txt");
// write the string to the file
writer.write(data);
// close the writer
writer.close();
System.out.println("Successfully wrote text to file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
在此示例中,我们首先创建一个名为data的String对象,其中包含要写入文件的文本。然后,我们使用要写入的文件的名称创建一个FileWriter对象,在本例中名为output.txt。接下来,我们使用FileWriter对象的write()方法将数据字符串写入文件。此方法采用包含要写入的数据的字符串参数。最后,我们关闭FileWriter对象以释放文件资源,如果写入操作成功,则在控制台打印一条成功消息;如果捕获异常,则在控制台打印一条错误消息。FileWriter类是在 Java 中将字符数据写入文件的简单方法 。
文件输出流类
FileOutputStream类用于将数据作为字节流写入文件。这是一个例子:import java.io.FileOutputStream;
import java.io.IOException;
public class WriteToFileExample {
public static void main(String[] args) {
String textToWrite = "Hello World!";
try {
FileOutputStream stream = new FileOutputStream("output.txt");
byte[] bytesToWrite = textToWrite.getBytes();
stream.write(bytesToWrite);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在此示例中,我们创建一个名为Stream的FileOutputStream对象,并使用“output.txt”文件名进行写入。然后,我们使用getBytes()方法将textToWrite字符串转换为字节数组,并使用write()方法将字节数组写入流对象。最后,我们关闭流对象。
BufferedWriter类
BufferedWriter类用于将文本写入具有缓冲功能的文件。这是一个例子:import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFileExample {
public static void main(String[] args) {
String textToWrite = "Hello World!";
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
writer.write(textToWrite);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在此示例中,我们创建一个名为writer的BufferedWriter对象,其中包含一个写入“output.txt”文件的FileWriter对象。然后,我们使用writer对象的write()方法将textToWrite写入文件。最后,我们关闭writer对象。
WriteString() 方法
writeString ()方法是 Java 11 中引入的一种便捷方法,用于使用Files类将String写入文件。它允许您用一行代码将字符串写入文件。这是一个例子:import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class WriteToFileExample {
public static void main(String[] args) {
// create a string to write to a file
String data = "Hello, world!";
// create a file object
Path file = Paths.get("output.txt");
try {
// write the string to the file using writeString() method
Files.writeString(file, data);
System.out.println("Successfully wrote text to file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
在此示例中,我们首先创建一个名为data的String对象,其中包含要写入文件的文本。然后,我们创建一个名为file的Path对象,它代表我们要写入的文件,在本例中名为“output.txt”。接下来,我们使用Files.writeString()方法将数据字符串写入文件对象表示的文件中。此方法采用两个参数:表示要写入的文件的Path对象,以及包含要写入的数据的String对象。最后,如果写入操作成功,我们将成功消息打印到控制台;如果捕获异常,我们将打印错误消息。writeString ()方法是使用 Java 11 或更高版本将字符串写入文件 的便捷方法。
PrintWriter类
PrintWriter类在每行写入后自动刷新输出缓冲区,确保数据立即写入文件。这使得它成为将大量文本数据写入文件的便捷选择。这是一个例子:import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFileExample {
public static void main(String[] args) {
String textToWrite = "Hello World!";
try {
PrintWriter writer = new PrintWriter(new FileWriter("output.txt"));
writer.println(textToWrite);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在此示例中,我们创建一个名为writer的PrintWriter对象,其中包含一个写入“output.txt”文件的FileWrite对象。然后,我们使用writer对象的println()方法将textToWrite写入文件。最后,我们关闭writer对象。
数据输出流类
DataOutputStream类用于将原始 Java 数据类型作为字节流写入文件。这是一个例子:import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteToFileExample {
public static void main(String[] args) {
String textToWrite = "Hello World!";
int numberToWrite = 42;
try {
DataOutputStream stream = new DataOutputStream(new FileOutputStream("output.txt"));
stream.writeUTF(textToWrite);
stream.writeInt(numberToWrite);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在此示例中,我们创建一个名为stream的DataOutputStream对象,该对象写入“output.txt”文件。然后,我们使用流对象的writeUTF()方法将textToWrite字符串写入,并使用writeInt()方法将numberToWrite整数写入文件。最后,我们关闭流对象。
文件通道类
FileChannel类提供了一种使用内存映射 I/O 将数据写入文件的方法。这是一个例子:import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class WriteToFileExample {
public static void main(String[] args) {
String textToWrite = "Hello World!";
try {
File file = new File("output.txt");
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
FileChannel channel = randomAccessFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put(textToWrite.getBytes());
buffer.flip();
channel.write(buffer);
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在此示例中,我们首先创建一个名为file的File对象,并以文件名“output.txt”作为参数。然后,我们创建一个名为randomAccessFile的RandomAccessFile对象,并将文件对象和“rw”模式作为参数。然后,我们创建一个名为Channel的FileChannel对象,该对象使用randomAccessFile对象进行初始化。然后我们创建一个名为buffer的ByteBuffer对象,容量为 1024 字节,并将textToWrite字符串作为参数。然后,我们翻转缓冲区对象以准备写入,并使用write()方法将其写入通道对象。最后,我们关闭randomAccessFile对象。
GO TO FULL VERSION