xxxxxxxxxx
try (Stream<String> stream = Files.lines(Paths.get(String.valueOf(new File("yourFile.txt"))))) {
stream.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
xxxxxxxxxx
File myFile = new File("filename.txt");
Scanner myReader = new Scanner(myFile);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
}
myReader.close();
xxxxxxxxxx
import java.io.*;
public class openFile {
public static void main(String[] args) {
try{
File file = new File("C:\\file.txt");
}catch(Exception e){
e.printStackTrace();
}
}
}
InputStreamReader Example
xxxxxxxxxx
import java.io.InputStreamReader;
import java.io.FileInputStream;
class Main {
public static void main(String[] args) {
// Creates an array of character
char[] array = new char[100];
try {
// Creates a FileInputStream
FileInputStream file = new FileInputStream("input.txt");
// Creates an InputStreamReader
InputStreamReader input = new InputStreamReader(file);
// Reads characters from the file
input.read(array);
System.out.println("Data in the stream:");
System.out.println(array);
// Closes the reader
input.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}