-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadingFiles.java
More file actions
37 lines (25 loc) · 1 KB
/
Copy pathReadingFiles.java
File metadata and controls
37 lines (25 loc) · 1 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadingFiles {
public static void main(String[] args) throws FileNotFoundException {
//Read a text file in project folder
File inputFile = new File("test.txt");
//Read a test file outside the project folder
File desktopFile = new File("C:\\Users\\user\\Desktop\\test.txt");
//Check methods available on file variable
System.out.println(desktopFile.exists());
System.out.println(desktopFile.exists());
System.out.println(inputFile.length());
System.out.println(inputFile.canRead());
System.out.println(inputFile.getTotalSpace());
//READ CONTENT IN TEXT FILE
Scanner input = new Scanner(inputFile);
//open text file and read content
//WHILE LOOP TO ITERATE THROUGH THE FILE
while(input.hasNextLine()) {
System.out.println(input.nextLine());
}
input.close();
}
}