1try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
2 StringBuilder sb = new StringBuilder();
3 String line = br.readLine();
4
5 while (line != null) {
6 sb.append(line);
7 sb.append(System.lineSeparator());
8 line = br.readLine();
9 }
10 String everything = sb.toString();
11}
1import java.io.*;
2
3public class openFile {
4
5 public static void main(String[] args) {
6 try{
7 File file = new File("C:\\file.txt");
8 }catch(Exception e){
9 e.printStackTrace();
10 }
11 }
12
13}
1try (Stream<String> stream = Files.lines(Paths.get(String.valueOf(new File("yourFile.txt"))))) {
2 stream.forEach(System.out::println);
3} catch (IOException e) {
4 e.printStackTrace();
5}