How to read text file in Java

1 Answer

0 votes
package javaapplication1;

import java.io.*;

public class JavaApplication1 
{
    public static void main(String[] args) 
    {
        try 
        {
            File file = new File("d:\\test.txt");
            FileReader fr = new FileReader(file);
            try (BufferedReader br = new BufferedReader(fr)) 
            {
                String line;
                
                while((line = br.readLine()) != null) 
                    System.out.println(line);
            }
            catch (Exception e)
            {
                System.out.println(e.toString());
            }
        }
        catch (Exception e) 
        {
            System.out.println("Error reading file: " + e.getMessage());   
        }
    }
}

/*

run:

Text File Line 1
Text File Line 2
Text File Line 3

*/



answered Mar 8, 2015 by avibootz
edited Mar 8, 2015 by avibootz

Related questions

1 answer 185 views
185 views asked Jun 30, 2017 by avibootz
1 answer 252 views
1 answer 258 views
1 answer 172 views
1 answer 109 views
...