Java中的输入输出函数:如何读取和写入文件?
Java是一种广泛使用的编程语言,对于大多数编程任务来说都非常适用。在实际编程中,我们通常需要进行文件读写操作。Java提供了一系列方法帮助我们进行文件的读写操作。在本文中,我们将讨论Java中的输入输出函数,包括如何读取和写入文件。
Java中的I/O类
Java提供了大量的I/O类来完成文件的读写操作。这些类分别属于不同的包,如java.io包和java.nio包等。Java的I/O类主要有如下几种:
1. FileInputStream和FileOutputStream
FileInputStream类用于从文件中读取字节流,而FileOutputStream类用于将字节流写入文件中。这两个类都是基于字节的I/O类。它们可以用于读取和写入所有类型的文件,包括文本文件和二进制文件。这些类的构造函数需要传入文件的路径和名称。
下面是一个读取二进制文件的示例代码:
import java.io.*;
public class ReadBinaryFile {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("file.bin");
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fis.read(buffer)) != -1) {
System.out.write(buffer, 0, bytesRead);
}
fis.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
2. FileReader和FileWriter
FileReader类和FileWriter类用于读取和写入文本文件,它们是基于字符的I/O类。如果需要读写文本文件,应该使用这两个类而不是FileInputStream和FileOutputStream类。这些类的构造函数也需要传入文件的路径和名称。
下面是一个读取文本文件的示例代码:
import java.io.*;
public class ReadTextFile {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("file.txt");
BufferedReader br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
fr.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
3. DataInputStream和DataOutputStream
DataInputStream类和DataOutputStream类用于读写Java本身支持的基本数据类型,如int、float和double等。这些类的构造函数需要接受一个InputStream或OutputStream类型的参数。下面是一个使用DataInputStream类和DataOutputStream类的示例代码:
import java.io.*;
public class WriteDataToFile {
public static void main(String[] args) {
try {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("file.dat"));
dos.writeInt(1);
dos.writeDouble(3.14);
dos.writeBoolean(true);
dos.writeUTF("Hello, world!");
dos.close();
DataInputStream dis = new DataInputStream(new FileInputStream("file.dat"));
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());
System.out.println(dis.readUTF());
dis.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
4. BufferedReader和BufferedWriter
BufferedReader类和BufferedWriter类用于缓冲输入和输出。缓冲输入可以提高读取文件时的效率,缓冲输出可以提高写入文件时的效率。这些类的构造函数需要接受一个InputStream或OutputStream类型的参数。
下面是一个使用BufferedReader类和BufferedWriter类的示例代码:
import java.io.*;
public class ReadTextFile {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("file.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("output.txt");
BufferedWriter bw = new BufferedWriter(fw);
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
bw.close();
fw.close();
br.close();
fr.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
总结
Java提供了丰富的I/O类和方法,使得文件的读写操作变得非常简单。在使用这些类和方法时需要注意:
1. 在使用完I/O类后,应该调用close方法关闭文件流,以释放系统资源。
2. 在读取和写入文件时需要对异常进行处理,以避免程序崩溃。
除了上述提到的I/O类,Java还提供了其他的一些I/O类,如ObjectInputStream、ObjectOutputStream、PrintWriter等。使用不同的I/O类可以满足不同的需求。在读写文件时,应该根据需要选择适当的I/O类和方法。
