How do you read from an external file in java?

Java

1 answer

Answer

1290101

2026-05-02 17:35

+ Follow

Java
Java

The class I find useful is the FileReader class in Java.io. It allows you to read an external file in your program.

For instance,

Consider a file input.txt:

Hello

How are you?

1 2 3 4 5

You can access it by:

FileReader read = new FileReader("input.txt"); // pass the file name as a String

//now the read is the file

//can scan from the file using Scanner

Scanner scan = new Scanner(read);

System.out.println( scan.nextLine()); // prints Hello

System.out.println( scan.nextLine()); // prints How are you?

while(scan.hasNextInt())

System.out.print(scan.nextInt() + " "); // prints 1 2 3 4 5

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.