Java中如何使用函数进行文件的读取和写入?
Java中有多种方法可以读取和写入文件,其中最常用的包括java.io和java.nio包中的类和方法。
1. 读取文件
使用Java中的File类和FileReader类可以读取文件中的内容,读取方式如下:
try (FileReader fr = new FileReader(new File("filename.txt"));
BufferedReader br = new BufferedReader(fr)) {
String line;
while ((line = br.readLine()) != null) {
// 对每一行进行处理
}
} catch (IOException e) {
e.printStackTrace();
}
上述代码中,使用了Java中的try-with-resources语句,确保在读取完成后关闭文件读取器。
另外,可以使用Java中的Scanner类读取文件内容,读取方式如下:
try (Scanner scanner = new Scanner(new File("filename.txt"))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// 对每一行进行处理
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
2. 写入文件
使用Java中的FileWriter类和PrintWriter类可以将文本写入文件中,写入方式如下:
try (PrintWriter pw = new PrintWriter(new FileWriter(new File("filename.txt"), true))) {
pw.println("content to write");
} catch (IOException e) {
e.printStackTrace();
}
上述代码中,第二个参数设置为true表示以追加模式打开文件,如果设置为false,则会覆盖原有内容。
另外,可以使用Java中的BufferedWriter类和OutputStreamWriter类将文本写入文件中,写入方式如下:
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("filename.txt", true)))) {
bw.write("content to write");
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
上述代码中,使用了BufferedWriter类的newLine()方法换行,如果不需要换行,则可以使用write()方法直接写入。
3. 读写二进制文件
使用Java中的FileInputStream类和FileOutputStream类可以读写二进制文件,读取方式如下:
try (FileInputStream fis = new FileInputStream(new File("filename.dat"));
DataInputStream dis = new DataInputStream(fis)) {
int intValue = dis.readInt();
float floatValue = dis.readFloat();
double doubleValue = dis.readDouble();
} catch (IOException e) {
e.printStackTrace();
}
上述代码中,使用了DataInputStream类对二进制文件进行读取,其中readInt()、readFloat()和readDouble()方法分别读取整数、单精度浮点数和双精度浮点数。
写入方式如下:
try (FileOutputStream fos = new FileOutputStream(new File("filename.dat"));
DataOutputStream dos = new DataOutputStream(fos)) {
dos.writeInt(1);
dos.writeFloat(3.14f);
dos.writeDouble(2.71828);
} catch (IOException e) {
e.printStackTrace();
}
上述代码中,使用了DataOutputStream类将整数、单精度浮点数和双精度浮点数写入二进制文件中。
4. 使用NIO读写文件
Java NIO(New IO)包含了很多高效的I/O操作类和方法,可以更好地掌控I/O操作,使用NIO进行文件读写操作的方式如下:
读取方式:
try (FileChannel channel = FileChannel.open(Paths.get("filename.txt"), StandardOpenOption.READ)) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (channel.read(buffer) != -1) {
buffer.flip();
while (buffer.hasRemaining()) {
// 对每一字节进行处理
byte b = buffer.get();
}
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
上述代码中,使用了FileChannel类将文件内容读取到ByteBuffer中。
写入方式:
try (FileChannel channel = FileChannel.open(Paths.get("filename.txt"), StandardOpenOption.WRITE)) {
ByteBuffer buffer = ByteBuffer.wrap("content to write".getBytes());
while (buffer.hasRemaining()) {
channel.write(buffer);
}
} catch (IOException e) {
e.printStackTrace();
}
上述代码中,将ByteBuffer中的内容写入到指定文件中。
总结:
Java中有多种方法可以进行文件读写操作,具体选择哪种方法取决于读写文件的需求和性能要求。一般而言,如果需要在文件中读取或写入文本内容,可以使用java.io包中的类和方法,而如果需要读取或写入二进制文件,则可以使用FileInputStream、FileOutputStream、DataInputStream和DataOutputStream类。如果需要更高效地掌控I/O操作,则可以使用java.nio包中的类和方法。
