-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileInfo.java
More file actions
36 lines (34 loc) · 1.25 KB
/
FileInfo.java
File metadata and controls
36 lines (34 loc) · 1.25 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
import java.io.File;
import java.util.Scanner;
public class FileInfo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the file name:");
String fileName = scanner.nextLine();
scanner.close();
File file = new File(fileName);
System.out.println("File Information:");
System.out.println("Exists:" + file.exists());
System.out.println("Readable:" + file.canRead());
System.out.println("Writable:" + file.canWrite());
if (file.exists()) {
String fileType = getFileType(file);
long fileLength = file.length();
System.out.println("Type:" + fileType);
System.out.println("Length(in bytes):" + fileLength);
}
}
private static String getFileType(File file) {
if (file.isDirectory()) {
return "Directory";
} else {
String fileName = file.getName();
int dotIndex = fileName.lastIndexOf('.');
if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
return fileName.substring(dotIndex + 1).toUpperCase() + "File";
} else {
return "unknown";
}
}
}
}