-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileOperation.java
109 lines (109 loc) · 2.56 KB
/
FileOperation.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
class Filehandling
{
void fileExists(File f)
{
if(f.exists())
System.out.println("The file exists");
else
System.out.println("The file does not exists");
}
void fileReadable(File f)
{
if(f.exists())
{
if(f.canRead())
System.out.println("The file is readable");
else
System.out.println("The file is not readable");
}
else
System.out.println("The file does not exists");
}
void fileWriteable(File f)
{
if(f.exists())
{
if(f.canWrite())
System.out.println("The file is writeable");
else
System.out.println("The file is not writeable");
}
else
System.out.println("The file does not exists");
}
void fileType(File f)
{
String fileString;
if(f.exists())
{
fileString=f.getPath();
String[] fileArray=fileString.split("[\\.]");
System.out.println("The file is of type "+fileArray[fileArray.length-1]);
}
else
System.out.println("The file does not exists");
}
void fileLength(File f)
{
if(f.exists())
System.out.println("The length of the file in bytes is "+f.length());
else
System.out.println("The file does not exists");
}
}
class FileOperation
{
public static void main(String args[])
{
Scanner reader=new Scanner(System.in);
String filename;
System.out.print("Enter the file name with directory: ");
filename=reader.next();
File f=new File(filename);
try{
if(f.createNewFile())
System.out.println("The file is created successfully");
else
System.out.println("The file already exists");}
catch(IOException e)
{System.out.println("error");}
Filehandling fh=new Filehandling();
int choice,ch;
do{
System.out.println("\t\tFile Operations");
System.out.println("1.Check whether the file exists");
System.out.println("2.Check whether the file is readable");
System.out.println("3.Check whether the file is writeable");
System.out.println("4.Check the type of file");
System.out.println("5.Display the length of file");
System.out.print("Enter your choice: ");
choice=reader.nextInt();
switch(choice)
{
case 1:
fh.fileExists(f);
break;
case 2:
fh.fileReadable(f);
break;
case 3:
fh.fileWriteable(f);
break;
case 4:
fh.fileType(f);
break;
case 5:
fh.fileLength(f);
break;
default:
System.out.println("Wrong Choice!!");
}
System.out.print("Do you want to continue<0/1>?: ");
ch=reader.nextInt();
}while(ch==1);
System.out.println("Thank you!!");
}
}