-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
117 lines (113 loc) · 3.65 KB
/
Main.java
File metadata and controls
117 lines (113 loc) · 3.65 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
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
110
111
112
113
114
115
116
117
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
class Main{
String filename ;
String extenstion;
File file;
Scanner scanner = new Scanner(System.in);
Main(String filename, String extenstion){
this.filename = filename;
this.extenstion = filename+extenstion;
this.file = new File(this.extenstion);
}
void createfile(){
try{
if(!file.exists()){
file.createNewFile();
System.out.println("Creating file " + file.toPath());
}else{
System.out.println("File already exists: " + file.getAbsolutePath());
}
}catch(Exception e){
System.out.println("Error"+e.getMessage());
}
}
void writefile(){
if(file.length() >1){
appendtext();
return;
}
try(FileWriter fw = new FileWriter(file)){
System.out.println("Enter content");
String content = scanner.nextLine();
fw.write(content);
System.out.println("Content written to file.");
}catch(Exception e){
System.out.println("Error"+e.getMessage());
}
}
void readfile(){
try(BufferedReader br = new BufferedReader(new FileReader(file) )){
while(br.ready()){
System.out.println(br.readLine());
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
void appendtext(){
try(FileWriter fw = new FileWriter(file, true)){
System.out.println("Enter content to append:");
String content = scanner.nextLine();
fw.write(content + System.lineSeparator());
System.out.println("Content appended to file.");
}catch(Exception e){
System.out.println("Error"+e.getMessage());
}
}
void deletefile(){
try{
if(file.delete()){
System.out.println("File deleted successfully");
}else{
System.out.println("Failed to delete the file");
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter filename");
String newfilename = sc.next();
System.out.println("Enter extension (e.g., txt):");
String extension = sc.next();
Main app = new Main(newfilename,"."+ extension);
while(true){
System.out.println("1.Create file");
System.out.println("2.Delete file");
System.out.println("3.Append text");
System.out.println("4.Read file");
System.out.println("5.Write text");
System.out.println("6.Exit");
System.out.println("Enter choice");
int choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
app.createfile();
break;
case 2:
app.deletefile();
break;
case 3:
app.appendtext();
break;
case 4:
app.readfile();
break;
case 5:
app.writefile();
break;
case 6:
sc.close();
System.exit(0);
default:
System.out.println("Invalid choice. Try again.");
}
}
}
}