Java Data Science Cookbook
上QQ阅读APP看书,第一时间看更新

Reading contents from text files all at once using Java 8

On many occasions, data scientists have their data in text format. There are many different ways to read text file contents, and they each have their own pros and cons: some of them consume time and memory, while some are fast and do not require much computer memory; some read the text contents all at once, while some read text files line by line. The choice depends on the task at hand and a data scientist's approach to that task.

This recipe demonstrates how to read text file contents all at once using Java 8.

How to do it...

  1. First, create a String object to hold the path and name of the text file you are going to read:
            String file = "C:/dummy.txt";  
    
  2. Using the get() method of the Paths class, we get to the path of the file we are trying to read. The parameter for this method is the String object that points to the name of the file. The output of this method is fed to another method named lines(), which is in the Files class. This method reads all lines from a file as a Stream, and therefore, the output of this method is directed to a Stream variable. Because our dummy.txt file contains string data, the generics of the Stream variable is set to String.

The entire process of reading needs a try...catch block for attempts such as reading a file that does not exist or damaged and so on.

The following code segment displays the contents of our dummy.txt file. The stream variable contains the lines of the text file, and therefore, the forEach() method of the variable is used to display each line content:

        try (Stream<String> stream = Files.lines(Paths.get(file))) { 
        stream.forEach(System.out::println); } catch (IOException e) { 
        System.out.println("Error reading " +  file.getAbsolutePath()); 
        }