-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFileStreams.java
250 lines (214 loc) · 6.65 KB
/
FileStreams.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package potplayer;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.LineNumberReader;
import java.io.FileReader;
import java.io.FileWriter;
public class FileStreams {
InputStream inputStream;
OutputStream outputStream;
String filePath;
String defaultPath = "./potplayer.play";
public FileStreams(String path) {
/**
* [C:\example\com\java\video\hello.txt]
* Specify the file to be read and written
*/
this.filePath = path;
}
public String readFile() {
int flength;
try {
this.inputStream = new FileInputStream(this.filePath);
if (this.inputStream != null) {
flength = (int)(new File(this.filePath).length());
byte[] bytes = new byte[flength];
inputStream.read(bytes);
return new String(bytes);
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void writeFile(String content) {
try {
this.outputStream = new FileOutputStream(this.filePath);
if (this.outputStream != null) {
this.outputStream.write(content.getBytes());
this.outputStream.close();
} else {
this.outputStream = new FileOutputStream(this.defaultPath);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void writeFiles(String content) {
File files = new File(this.filePath);
FileOutputStream fileOutputStream = null;
OutputStreamWriter outputStreamWrite = null;
try {
// If the file does not exist, create this file
if (!files.exists()) {
// create new file success
if (files.createNewFile()) {
fileOutputStream = new FileOutputStream(files);
}
} else {
// The file already exists
fileOutputStream = new FileOutputStream(files, true);
}
outputStreamWrite = new OutputStreamWriter(fileOutputStream, "utf-8");
outputStreamWrite.write(content);
outputStreamWrite.write("\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStreamWrite != null) {
outputStreamWrite.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Boolean checkdir(String dir) {
/**
* dir type [./example/hello.mp3]
* dir type [./example/"]
* or type ["C:\\example\\hello.mp3]
*/
File check = new File(dir);
if (check != null) {
if (!check.exists()) {
// The directory not exists
return false;
} else {
// The directory exists
return true;
}
} else {
// new file object error
return false;
}
}
public static Boolean mkdir(String dir) {
// dir type [./example/hello]
File make = new File(dir);
if (make != null) {
// If the directory does not exist,
// create this directory
if (!make.exists()) {
make.mkdir();
// directory make success
return true;
} else {
// The directory already exists
return true;
}
} else {
// new file object error
return false;
}
}
public static Boolean emptyFile(String fileName) {
File file = new File(fileName);
if (file.length() == 0) {
return true;
} else {
return false;
}
}
public static Boolean deleteFile(String dir) {
File file = new File(dir);
try {
if (file.delete()) {
// delete success
return true;
} else {
// delete fail
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void deleteFileContent(String fileName) {
File file =new File(fileName);
try {
if(!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter =new FileWriter(file);
fileWriter.write("");
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static int totalNumberOfRows(String fileName) {
int lines = 0;
try {
File f = new File(fileName);
FileReader read = new FileReader(f);
LineNumberReader lineReader = new LineNumberReader(read);
String string = lineReader.readLine();
while (string != null) {
lines++;
string = lineReader.readLine();
}
lineReader.close();
read.close();
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}
public static String readAnyLine(String fileName, int lineNumber) {
// file begin from 1 line
int lines = 0;
String string;
try {
File f = new File(fileName);
FileReader read = new FileReader(f);
LineNumberReader lineReader = new LineNumberReader(read);
while (lineReader != null) {
string = lineReader.readLine();
lines++;
if (lines == lineNumber) {
lineReader.close();
read.close();
return string;
}
if (string == null) {
// No content, exit the loop
return null;
}
}
lineReader.close();
read.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}