-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay24.java
More file actions
171 lines (135 loc) · 4.63 KB
/
Day24.java
File metadata and controls
171 lines (135 loc) · 4.63 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// ---------------------------------------------------
// Author : Benjamin Kataliko Viranga
// Community : Stunt Business
// Community website : www.stuntbusiness.com
//
// 30 Days - Q&A Java basic
// Day 24 : Challenge XII - File Manager
// Day 24 | IG : https://www.instagram.com/benjivrik/
// ----------------------------------------------------
// what would be the output of this program ?
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
class FileManager {
private String path;
private String fileName;
public FileManager(String path, String fileName) {
this.path = path;
this.fileName = fileName;
this.init(this.path, this.fileName);
}
// initialize directory and file
private void init(String path, String fileName) {
File directory = new File(path);
// create directory if it does not exist
if (!(directory.exists())) {
directory.mkdir();
}
File file = new File(path + fileName);
// creating the file if it does not exist
if (!file.exists()) {
try {
if (file.createNewFile()) {
System.out.println("\nFile successfully created : " + file.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
} else // the file does not exist
{
System.out.println(String.format("\nFile %s already exists.", file.getName()));
}
}
// set filename and create the file
public void setFileName(String fileName) {
this.fileName = fileName;
this.init(this.path, this.fileName);
}
// get filename
public String getFileName() {
return this.fileName;
}
// set folder path and create the folder with the current if you set initPath to
// true
// Customizable function
public void setFolderPath(String path, boolean initPath) {
this.path = path;
if (initPath)
this.init(this.path, this.fileName);
}
// display all the data contained in the file
public void displayAllData() {
File file = new File(path + fileName);
Scanner sc;
try {
sc = new Scanner(file);
System.out.println("\n----------- READING ------------\n");
// read the content of the file
while (sc.hasNextLine())
System.out.println(sc.nextLine());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
// add the content to the existing file
public void addContentInFile(String data)
{
File file = new File(path + fileName);
// Append a text at the end of the file
try {
System.out.println("\n----------- WRITING ------------\n");
FileWriter writer = new FileWriter(file.getAbsolutePath(),true);
writer.write(data);
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
// clear content in file
public void clearFile()
{
File file = new File(path + fileName);
// Append a text at the end of the file
try {
FileWriter writer = new FileWriter(file.getAbsolutePath());
writer.write("");
writer.close();
System.out.println("\n----------- FILE CLEARED ------------\n");
}
catch (IOException e)
{
e.printStackTrace();
}
}
// to String
public String toString()
{
System.out.println("\n************** DISPLAYING FILE MANAGER INFO **************\n");
String fileManager = "\nHello,\nYour current folder is '"+ this.path+"'.\n";
fileManager += "And you are working with the file '" + this.fileName +"'.\n";
fileManager += "Your full path is '" + this.path + this.fileName+"'.";
return fileManager;
}
}
public class Day24
{
public static void main(String[] args)
{
FileManager fileManager = new FileManager("text_data/","Day24_data.txt");
// display the informatin about the task manager
System.out.println(fileManager);
// clearing data inside the file
fileManager.clearFile();
// adding random data
fileManager.addContentInFile("blah blah\n");
fileManager.addContentInFile("TEST\n");
fileManager.addContentInFile("blah blah blah\n");
// display all data
fileManager.displayAllData();
}
}