Java

Reading Data From a File using Java NIO

In this tutorial we are going to learn how to read data from file using Java NIO classes of Path and Files, if you are not aware of the methods available with these classes click here.

Steps are very Simple.

Create a Path Object pointing to the file and then use the readAllLines() of Files to read the data from the file.

ReadFile.java

import java.nio.file.*;
import java.util.*;

public class ReadFile
{
    public static void main(String[] args)
    {
        try
        {
            Path path=Paths.get("data.txt");
           List data= Files.readAllLines(path);
          
           for(String line:data)
           {
               System.out.println(line);
           }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

Related Posts

Table Of Contents

;