上一篇
Java中调用文件可以使用`java.
Java中调用文件是一个常见的操作,通常用于读取或写入文件内容,以下是几种常见的方法来实现这一功能:

使用 java.io 包
读取文件
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
public class ReadFileExample {
public static void main(String[] args) {
File file = new File("path/to/your/file.txt");
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
写入文件
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
public class WriteFileExample {
public static void main(String[] args) {
File file = new File("path/to/your/file.txt");
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
bw.write("Hello, World!");
bw.newLine();
bw.write("This is a new line.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用 java.nio.file 包
读取文件
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.io.IOException;
import java.util.List;
public class ReadFileNIOExample {
public static void main(String[] args) {
Path path = Paths.get("path/to/your/file.txt");
try {
List<String> lines = Files.readAllLines(path);
for (String line : lines) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
写入文件
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.io.IOException;
import java.util.Collections;
public class WriteFileNIOExample {
public static void main(String[] args) {
Path path = Paths.get("path/to/your/file.txt");
try {
Files.write(path, Collections.singletonList("Hello, World!"));
Files.write(path, Collections.singletonList("This is a new line."), java.nio.file.StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用 java.nio.channels 包
读取文件
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import java.io.RandomAccessFile;
import java.io.IOException;
public class ReadFileChannelExample {
public static void main(String[] args) {
try (RandomAccessFile file = new RandomAccessFile("path/to/your/file.txt", "r");
FileChannel channel = file.getChannel()) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (channel.read(buffer) != -1) {
buffer.flip();
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
}
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
写入文件
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import java.io.RandomAccessFile;
import java.io.IOException;
public class WriteFileChannelExample {
public static void main(String[] args) {
try (RandomAccessFile file = new RandomAccessFile("path/to/your/file.txt", "rw");
FileChannel channel = file.getChannel()) {
ByteBuffer buffer = ByteBuffer.wrap("Hello, World!".getBytes());
channel.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
| 方法 | 包 | 读取文件 | 写入文件 |
|---|---|---|---|
| 1 | java.io |
BufferedReader + FileReader |
BufferedWriter + FileWriter |
| 2 | java.nio.file |
Files.readAllLines |
Files.write |
| 3 | java.nio.channels |
FileChannel + ByteBuffer |
FileChannel + ByteBuffer |
相关问答FAQs
Q1: 如何在Java中读取大文件?
A1: 对于大文件,建议使用 java.nio.file.Files 包中的流式API,如 Files.lines(Path path),它返回一个 Stream<String>,可以逐行处理文件内容,避免一次性加载整个文件到内存中。

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.io.IOException;
import java.util.stream.Stream;
public class ReadLargeFileExample {
public static void main(String[] args) {
Path path = Paths.get("path/to/large/file.txt");
try (Stream<String> lines = Files.lines(path)) {
lines.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Q2: 如何在Java中检查文件是否存在?
A2: 可以使用 java.io.File 类的 exists() 方法来检查文件是否存在。

import java.io.File;
public class CheckFileExistsExample {
public static void main(String[] args) {
File file = new File("path/to/your/file.txt");
if (file.exists()) {
System.out.println("文件存在");
} else {
System.out.println("文件不存在");
}
}
}
