forked from akash-coded/core-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadFromFile.java
More file actions
48 lines (43 loc) · 1.64 KB
/
Copy pathReadFromFile.java
File metadata and controls
48 lines (43 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package src.file_handling;
import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadFromFile {
public static void main(String[] args) {
// the general way
File f = new File("src/file_handling/Test3.txt");
try (Scanner fileReader = new Scanner(f);) { // try-with-resources
while (fileReader.hasNextLine()) {
System.out.println(fileReader.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// nio way of reading from file
try (BufferedReader reader = Files.newBufferedReader(Paths.get("src/file_handling/Test2.txt"))) {
String currentLine = null;
while ((currentLine = reader.readLine()) != null) {// while there is content on the current line
System.out.println(currentLine); // print the current line
}
} catch (IOException ex) {
ex.printStackTrace(); // handle an exception here
}
// using FileReader (reads character by character)
try (FileReader fileReader = new FileReader("src/file_handling/Test3.txt");) {
int ch;
int counter = 0;
while ((ch = fileReader.read()) != -1) {
System.out.println((char) ch);
counter++;
}
System.out.println(counter);
} catch (IOException ex) {
ex.printStackTrace(); // handle an exception here
}
}
}