27 lines
679 B
Java
27 lines
679 B
Java
|
package Files;
|
||
|
|
||
|
import java.io.File; // Import the File class
|
||
|
import java.io.FileNotFoundException; // Import this class to handle errors
|
||
|
import java.util.Scanner; // Import the Scanner class to read text files
|
||
|
|
||
|
public class ReadFile {
|
||
|
|
||
|
public String data;
|
||
|
|
||
|
public String ReadFromFile(String path) {
|
||
|
try {
|
||
|
File myObj = new File(path);
|
||
|
Scanner myReader = new Scanner(myObj);
|
||
|
while (myReader.hasNextLine()) {
|
||
|
data = myReader.nextLine();
|
||
|
System.out.println(data);
|
||
|
}
|
||
|
myReader.close();
|
||
|
} catch (FileNotFoundException e) {
|
||
|
System.out.println("An error occurred.");
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
return data;
|
||
|
}
|
||
|
}
|