Java中如何使用IO函数实现文件读取和写入操作?
Java中的IO函数提供了一些类和方法,可以用于实现文件读取和写入操作。本文将介绍如何使用Java中的IO函数实现文件读取和写入操作。
**文件读取操作**
Java中读取文件的函数主要有三个类:FileInputStream、BufferedInputStream和DataInputStream。
1. 使用FileInputStream读取文件
FileInputStream类用于从文件中读取字节流,其构造方法如下:
FileInputStream(File file) throws FileNotFoundException FileInputStream(String name) throws FileNotFoundException
以下是使用FileInputStream读取文件的代码:
try {
File file = new File("file.txt");
FileInputStream fis = new FileInputStream(file);
int content;
while ((content = fis.read()) != -1) {
System.out.print((char) content);
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
2. 使用BufferedInputStream读取文件
BufferedInputStream类是一个带缓冲区的输入流,它可以提高读取文件的效率。以下是使用BufferedInputStream读取文件的代码:
try {
File file = new File("file.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int content;
while ((content = bis.read()) != -1) {
System.out.print((char) content);
}
bis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
3. 使用DataInputStream读取文件
DataInputStream类可以读取不同类型的数据,包括整数、浮点数、布尔值等。以下是使用DataInputStream读取文件的代码:
try {
File file = new File("file.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
// 读取整数
int num = dis.readInt();
System.out.println(num);
// 读取字符串
String str = dis.readUTF();
System.out.println(str);
dis.close();
bis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
**文件写入操作**
Java中写入文件的函数主要有三个类:FileOutputStream、BufferedOutputStream和DataOutputStream。
1. 使用FileOutputStream写入文件
FileOutputStream类用于向文件中写入字节流,其构造方法如下:
FileOutputStream(File file) throws FileNotFoundException FileOutputStream(File file, boolean append) throws FileNotFoundException FileOutputStream(String name) throws FileNotFoundException FileOutputStream(String name, boolean append) throws FileNotFoundException
以下是使用FileOutputStream写入文件的代码:
try {
File file = new File("file.txt");
FileOutputStream fos = new FileOutputStream(file);
String content = "hello world";
byte[] bytes = content.getBytes();
fos.write(bytes);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
2. 使用BufferedOutputStream写入文件
BufferedOutputStream类是一个带缓冲区的输出流,它可以提高写入文件的效率。以下是使用BufferedOutputStream写入文件的代码:
try {
File file = new File("file.txt");
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
String content = "hello world";
byte[] bytes = content.getBytes();
bos.write(bytes);
bos.flush();
bos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
3. 使用DataOutputStream写入文件
DataOutputStream类可以写入不同类型的数据,包括整数、浮点数、布尔值等。以下是使用DataOutputStream写入文件的代码:
try {
File file = new File("file.txt");
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
// 写入整数
int num = 100;
dos.writeInt(num);
// 写入字符串
String str = "hello world";
dos.writeUTF(str);
dos.flush();
dos.close();
bos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
总结:
Java中的IO函数提供了很多类和方法,可以用于实现文件读取和写入操作。我们可以根据需要选择合适的类和方法,以达到更好的效果。一般来说,为了提高读写效率,我们可以采用带缓冲区的输入输出流。
