-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadFiles.java
More file actions
23 lines (23 loc) · 982 Bytes
/
ReadFiles.java
File metadata and controls
23 lines (23 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
public class ReadFiles {
public static void main(String[]args) {
//BufferedReader=best fo reading txt files 👈
//FileInputStream=best for binary files(images,audio files)
//RandomAccessFile=best for read/write specific portions of large files
try (BufferedReader reader = new BufferedReader(new FileReader("text.txt"))) {
System.out.println("file exists");
String line;
while((line=reader.readLine()) !=null){//read every line through reader ans assign to line until line!=null
System.out.println(line);
}
} catch (FileNotFoundException e) {
System.out.println("could not locate file location");
} catch (IOException e) {
System.out.println("Something went wrong");
}
}
}