使用java.nio.file.Files读取文本文件
- Files类将文件的所有内容读入字节数组。Files类还可以读取所有行到字符串列表。
- Files类是在Java 7中引入的,适合在处理小文件并且需要加载所有文件内容到内存中时使用
String fileName = "D:docs/readme.txt"; Path path = Paths.get(fileName); byte[] bytes = Files.readAllBytes(path); List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
使用java.io.FileReader类
FileReader获取BufferedReader,然后逐行读取文件。不支持编码并使用系统默认编码,
它不是一种java中读取文本文件的非常有效的方法。
String fileName = "D:docs/readme.txt"; File file = new File(fileName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line; while((line = br.readLine()) != null){ // 一行一行地处理... System.out.println(line); }
使用java.io.BufferedReader
逐行读取文件并对它们进行处理,适用于处理大文件,也支持编码。
BufferedReader是同步的,因此可以安全地从多个线程完成对BufferedReader的读取操作。
BufferedReader的默认缓冲区大小为:8KB。
String fileName = "D:docs/readme.txt"; File file = new File(fileName); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, cs); BufferedReader br = new BufferedReader(isr); String line; while((line = br.readLine()) != null){ //process the line System.out.println(line); } br.close();
使用Scanner读取文本文件
逐行读取文件或基于某些java正则表达式读取文件,则可使用Scanner类。
Scanner类使用分隔符模式将其输入分解为标记,分隔符模式默认匹配空格。然后可以使用各种下一种方法将得到的标记转换成不同类型的值。
Scanner类不同步,因此不是线程安全的。
Path path = Paths.get(fileName); Scanner scanner = new Scanner(path); System.out.println("Read text file using Scanner"); // 一行一行地读取 while(scanner.hasNextLine()){ //process each line String line = scanner.nextLine(); System.out.println(line); } scanner.close();
使用RandomAccessFile读取文本文件
RandomAccessFile file = new RandomAccessFile("D:docs/readme.txt", "r"); String str; while ((str = file.readLine()) != null) { System.out.println(str); } file.close();
Java读取文件示例:
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.Scanner; public class JavaReadFile { public static void main(String[] args) throws IOException { String fileName = "D:/docs/source.txt"; //使用Java 7中的Files类处理小文件,获取完整的文件数据 readUsingFiles(fileName); //使用Scanner类来处理大文件,逐行读取 readUsingScanner(fileName); //使用BufferedReader读取,逐行读取 readUsingBufferedReader(fileName); readUsingBufferedReaderJava7(fileName, StandardCharsets.UTF_8); readUsingBufferedReader(fileName, StandardCharsets.UTF_8); //使用FileReader读取,没有编码支持,效率不高 readUsingFileReader(fileName); } private static void readUsingFileReader(String fileName) throws IOException { File file = new File(fileName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line; System.out.println("Reading text file using FileReader"); while((line = br.readLine()) != null){ //逐行读取 System.out.println(line); } br.close(); fr.close(); } private static void readUsingBufferedReader(String fileName, Charset cs) throws IOException { File file = new File(fileName); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, cs); BufferedReader br = new BufferedReader(isr); String line; System.out.println("Read text file using InputStreamReader"); while((line = br.readLine()) != null){ //逐行读取 System.out.println(line); } br.close(); } private static void readUsingBufferedReaderJava7(String fileName, Charset cs) throws IOException { Path path = Paths.get(fileName); BufferedReader br = Files.newBufferedReader(path, cs); String line; System.out.println("Read text file using BufferedReader Java 7 improvement"); while((line = br.readLine()) != null){ //逐行读取 System.out.println(line); } br.close(); } private static void readUsingBufferedReader(String fileName) throws IOException { File file = new File(fileName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line; System.out.println("Read text file using BufferedReader"); while((line = br.readLine()) != null){ //逐行读取 System.out.println(line); } //close resources br.close(); fr.close(); } private static void readUsingScanner(String fileName) throws IOException { Path path = Paths.get(fileName); Scanner scanner = new Scanner(path); System.out.println("Read text file using Scanner"); //逐行读取 while(scanner.hasNextLine()){ //逐行处理 String line = scanner.nextLine(); System.out.println(line); } scanner.close(); } private static void readUsingFiles(String fileName) throws IOException { Path path = Paths.get(fileName); //将文件读取到字节数组 byte[] bytes = Files.readAllBytes(path); System.out.println("Read text file using Files class"); //read file to String list @SuppressWarnings("unused") List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8); System.out.println(new String(bytes)); } }