java Six Methods of Reading Data from File

  • 2021-12-11 07:37:46
  • OfStack

Directory 1. Scanner 2.Files.lines (Java 8) 3. Files. readAllLines (java8) 4.Files.readString(JDK 11) 5.Files.readAllBytes() 6. The way of classical pipe flow

This article mainly introduces six methods of java reading data from files, which are shared with you as follows:

Scanner (Java 1.5) reads data by row and String, Int types read data by separator. Files. lines, Return Stream (Java 8) Streaming Data Processing, Read by Row Files. readAllLines, returning List (Java 8) Files. readString, read String (Java 11), file max 2G. Files. readAllBytes, read byte [] (Java 7), file max 2G. BufferedReader, Classic Mode (Java 1.1- > forever)

1.Scanner

The first method is Scanner, which is provided from JDK1.5. It is characterized by reading file data by line and divider, which can read String type, Int type, Long type and other basic data types.


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

   try (Scanner sc = new Scanner(new FileReader(fileName))) {
      while (sc.hasNextLine()) {  // Read string by line 
         String line = sc.nextLine();
         System.out.println(line);
      }
   }

   try (Scanner sc = new Scanner(new FileReader(fileName))) {
      sc.useDelimiter("\\|");  // Separator 
      while (sc.hasNext()) {   // Read string by separator 
         String str = sc.next();
         System.out.println(str);
      }
   }

   //sc.hasNextInt()  , hasNextFloat()  Basic data types and so on. 
   // File contents: 1|2
   fileName = "D:\\data\\test\\newFile5.txt";
   try (Scanner sc = new Scanner(new FileReader(fileName))) {
      sc.useDelimiter("\\|");  // Separator 
      while (sc.hasNextInt()) {   // Read by Separator Int
          int intValue = sc.nextInt();
         System.out.println(intValue);
      }
   }
}

The output is:

Hello World|Hello Zimug
Hello World
Hello Zimug

2.Files.lines (Java 8)

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


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

   //  Read the contents of the file to Stream Stream, read by row 
   Stream<String> lines = Files.lines(Paths.get(fileName));

   //  Random row sequence for data processing 
   lines.forEach(ele -> {
      System.out.println(ele);
   });
}

forEach fetches the row data in an Stream stream in no guaranteed order, but it is fast. If you want to process the rows in the file in sequence, you can use forEachOrdered, but the processing efficiency will be reduced.


//  Process in file line order 
lines.forEachOrdered(System.out::println);

You can also convert Stream to List, but note that this means that you have to load all the data into memory once, and note that java.lang.OutOfMemoryError


//  Convert to List<String>,  To note java.lang.OutOfMemoryError: Java heap space
List<String> collect = lines.collect(Collectors.toList()

3. Files. readAllLines (java8)


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

   //  Convert to List<String>,  Pay attention 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 with java11, we are provided with a method to read one file at a time. Files cannot exceed 2G, and pay attention to your server and JVM memory. This method is suitable for reading small text files quickly.


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

   // java 11  Start the provided method, and read the file cannot exceed 2G Is closely related to your memory 
   //String s = Files.readString(Paths.get(fileName));
}

5.Files.readAllBytes()

What if you don't have JDK11 (readAllBytes () started with JDK7) and still want to quickly read the contents of one file at a time and switch to String? The data is first read into a binary array, and then converted into String contents. This method is suitable for reading small text files quickly without JDK11.


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

   // If it is JDK11 It is easy to use the above method if not this method 
   byte[] bytes = Files.readAllBytes(Paths.get(fileName));

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

6. The way of classical pipe flow


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

   //  Buffered stream read, default buffer 8k
   try (BufferedReader br = new BufferedReader(new FileReader(fileName))){
      String line;
      while ((line = br.readLine()) != null) {
         System.out.println(line);
      }
   }

   //java 8 It is ok to write like this in 
   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 by nesting 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 written by ObjectOutputStream, and then we can read it with ObjectInputStream.


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


Related articles: