0% found this document useful (0 votes)
27 views1 page

Java File Reading Example

This Java code imports classes needed to read a file, creates a File object and Scanner to read the file line by line, prints each line, and closes the Scanner.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views1 page

Java File Reading Example

This Java code imports classes needed to read a file, creates a File object and Scanner to read the file line by line, prints each line, and closes the Scanner.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import [Link].

File; // Import the File class


import [Link]; // Import this class to handle errors
import [Link]; // Import the Scanner class to read text files

public class ReadFile1 {


public static void main(String[] args) {
try {
File myObj = new File("C:\\Users\\itlab3\\Desktop\\Module6_Act1.txt");
Scanner myReader = new Scanner(myObj);
while ([Link]()) {
String data = [Link]();
[Link](data);
}
[Link]();
} catch (FileNotFoundException e) {
[Link]("An error occurred.");
[Link]();
}
}
}

You might also like