Six ways to read file content in Java

1.Scanner

The first method is Scanner, which is an API provided since JDK1.5. It is characterized by reading file data by line and by separator. It can read String type, Int type, Long type, etc. Data of the underlying data type.

@Test
void testReadFile1() throws IOException {
   //文件内容:Hello World|Hello Zimug
   String fileName = "D:\\data\\test\\newFile4.txt";

   try (Scanner sc = new Scanner(new FileReader(fileName))) {
      while (sc.hasNextLine()) {  //按行读取字符串
         String line = sc.nextLine();
         System.out.println(line);
      }
   }

   try (Scanner sc = new Scanner(new FileReader(fileName))) {
      sc.useDelimiter("\\|");  //分隔符
      while (sc.hasNext()) {   //按分隔符读取字符串
         String str = sc.next();
         System.out.println(str);
      }
   }

   //sc.hasNextInt() 、hasNextFloat() 、基础数据类型等等等等。
   //文件内容:1|2
   fileName = "D:\\data\\test\\newFile5.txt";
   try (Scanner sc = new Scanner(new FileReader(fileName))) {
      sc.useDelimiter("\\|");  //分隔符
      while (sc.hasNextInt()) {   //按分隔符读取Int
          int intValue = sc.nextInt();
         System.out.println(intValue);
      }
   }
}

The output of the above method is as follows:

Hello World|Hello Zimug
Hello World
Hello Zimug
1
2

2.Files.lines (Java 8)

If you need to process the content of the data file by line, this method is a method I recommend everyone to use. The code is simple, and the stream of java 8 is used to organically integrate file reading and file processing.

@Test
void testReadFile2() throws IOException {
   String fileName = "D:\\data\\test\\newFile.txt";

   // 读取文件内容到Stream流中,按行读取
   Stream<String> lines = Files.lines(Paths.get(fileName));

   // 随机行顺序进行数据处理
   lines.forEach(ele -> {
      System.out.println(ele);
   });
}

forEach does not guarantee the order of the row data in the Stream stream, but it is fast. If you want to process the row data in the file in order, you can use forEachOrdered, but the processing efficiency will decrease.

// 按文件行顺序进行处理
lines.forEachOrdered(System.out::println);

Or use the multi-sum capability of the CPU to perform data parallel processing parallel(), which is suitable for relatively large files.

// 按文件行顺序进行处理
lines.parallel().forEachOrdered(System.out::println);

You can also convert Stream<String>it to List<String>, but be aware that this means you have to load all the data into memory at once, and be aware of java.lang.OutOfMemoryError

// 转换成List<String>, 要注意java.lang.OutOfMemoryError: Java heap space
List<String> collect = lines.collect(Collectors.toList());

3.Files.readAllLines

This method is still provided by java8. If we don’t need it Stream<String>, we want to read the file directly to get one line by line List<String>, then use the following method. Same problem: this means you have to load all the data into memory at once, beware of java.lang.OutOfMemoryError

@Test
void testReadFile3() throws IOException {
   String fileName = "D:\\data\\test\\newFile3.txt";

   // 转换成List<String>, 要注意java.lang.OutOfMemoryError: Java heap space
   List<String> lines = Files.readAllLines(Paths.get(fileName),
               StandardCharsets.UTF_8);
   lines.forEach(System.out::println);

}

4.Files.readString(JDK 11)

Starting from java11, it provides us with a method to read a file at a time. The file cannot exceed 2G, and pay attention to your server and JVM memory. This method is suitable for fast reading of small text files.

@Test
void testReadFile4() throws IOException {
   String fileName = "D:\\data\\test\\newFile3.txt";

   // java 11 开始提供的方法,读取文件不能超过2G,与你的内存息息相关
   //String s = Files.readString(Paths.get(fileName));
}

5.Files.readAllBytes()

What should you do if you don't have JDK11 (readAllBytes() starts from JDK7) and still want to quickly read the content of a file and convert it into a String at one time? First read the data as a binary array, and then convert it into String content. This method is suitable for reading small text files quickly without JDK11.

@Test
void testReadFile5() throws IOException {
   String fileName = "D:\\data\\test\\newFile3.txt";

   //如果是JDK11用上面的方法,如果不是用这个方法也很容易
   byte[] bytes = Files.readAllBytes(Paths.get(fileName));

   String content = new String(bytes, StandardCharsets.UTF_8);
   System.out.println(content);
}

6. The way of classic pipeline flow

The last one is the classic way of pipe flow

@Test
void testReadFile6() throws IOException {
   String fileName = "D:\\data\\test\\newFile3.txt";

   // 带缓冲的流读取,默认缓冲区8k
   try (BufferedReader br = new BufferedReader(new FileReader(fileName))){
      String line;
      while ((line = br.readLine()) != null) {
         System.out.println(line);
      }
   }

   //java 8中这样写也可以
   try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))){
      String line;
      while ((line = br.readLine()) != null) {
         System.out.println(line);
      }
   }

}

This method can be used in combination through the nesting of pipeline flows, which is more flexible. For example, if we
want to read java Object from a file, we can use the following code, provided that the data in the file is the data written by ObjectOutputStream, it can be read by ObjectInputStream.

try (FileInputStream fis = new FileInputStream(fileName);
     ObjectInputStream ois = new ObjectInputStream(fis)){
   ois.readObject();
} 

Guess you like

Origin blog.csdn.net/xiansibao/article/details/127966807
Recommended