`n 如何在Java中读取文件?

如何在Java中读取文件?

Clock Icon 发布时间:2026/8/24 19:08  · 

NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">java中读取文件是一项常见任务。实现这一功能可以使用多种类库和方法。很多开发者比较喜欢使用`NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">java.nio.file`包中的类。使用`Files`类的方法可以简化文件读取的过程。比如,`Files.readAllLines(Path path)`可以读取所有行并返回一个字符串列表。实现起来相对简单,代码示例如下:
```NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">javaimport NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">java.nio.file.Files;import NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">java.nio.file.Paths;import NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">java.io.IOException;import NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">java.util.List;public class FileReaderExample { public static void main(String[] args) { String filePath = "example.txt"; try { List lines = Files.readAllLines(Paths.get(filePath)); for (String line : lines) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } }}```以上代码首先指定要读取的文件路径,然后使用`Files.readAllLines`方法读取该文件的所有行。读取的结果是一个字符串列表,可以通过简单的循环进行遍历输出。
使用`BufferedReader`进行文件读取也是非常高效的方法。`BufferedReader`适合于逐行读取文本文件,尤其是在处理大文件时,能够提高性能。示例代码包含如下内容:
```NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">javaimport NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">java.io.BufferedReader;import NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">java.io.FileReader;import NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">java.io.IOException;public class BufferedReaderExample { public static void main(String[] args) { String filePath = "example.txt"; try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } }}```在这个例子中,使用`BufferedReader`逐行读取文件内容。`try-with-resources`语法确保流在使用完毕后能够自动关闭,从而避免了资源泄漏的问题。
对于小文件而言,也可以使用`Scanner`类来读取文件内容,操作非常简便。很多时候,`Scanner`可以处理不同的数据格式。例子如下:
```NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">javaimport NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">java.io.File;import NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">java.io.FileNotFoundException;import NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">java.util.Scanner;public class ScannerExample { public static void main(String[] args) { String filePath = "example.txt"; try { Scanner scanner = new Scanner(new File(filePath)); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } }}```在这段代码中,`Scanner`类用于读取文件。逐行读取时,同样需检查文件是否为空,使用`hasNextLine()`方法来确认。
NET/" style="text-decoration: none; color: inherit;" title="NET">NET/" style="text-decoration: none; color: inherit;" title="java">java提供了多种方式来读取文件内容,包括`Files`、`BufferedReader`和`Scanner`等工具。选择合适的方法可以根据具体需求,比如文件大小和处理方式等。

推荐文章

热门文章