Skip to content

Commit 33d1680

Browse files
committed
update file :)
1 parent 8c115c9 commit 33d1680

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

FilesOperations/src/main/java/org/example/FileRead.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
public class FileRead {
1010
public static void main(String[] args) throws Exception {
11-
String s = read_InputStreamReader_BufferedInputStream("/etc/passwd");
11+
String s = read_FileUtils("/etc/passwd");
1212
System.out.println(s);
1313
}
1414

@@ -17,7 +17,7 @@ public static void main(String[] args) throws Exception {
1717
* java.io.InputStreamReader
1818
* 自带的 read()
1919
*/
20-
public String read_InputStreamReader(String filePath) throws Exception {
20+
public static String read_InputStreamReader(String filePath) throws Exception {
2121
FileInputStream fileInputStream = new FileInputStream(filePath);
2222

2323
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
@@ -38,7 +38,7 @@ public String read_InputStreamReader(String filePath) throws Exception {
3838
* java.io.InputStreamReader
3939
* java.io.BufferedReader
4040
*/
41-
public String read_InputStreamReader_BufferedReader(String filePath) throws Exception {
41+
public static String read_InputStreamReader_BufferedReader(String filePath) throws Exception {
4242
FileInputStream fileInputStream = new FileInputStream(filePath);
4343
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
4444
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
@@ -51,7 +51,7 @@ public String read_InputStreamReader_BufferedReader(String filePath) throws Exce
5151
return content.toString();
5252
}
5353

54-
public String read_InputStreamReader_CharArrayReader(String filePath) throws Exception {
54+
public static String read_InputStreamReader_CharArrayReader(String filePath) throws Exception {
5555
FileInputStream fileInputStream = new FileInputStream(filePath);
5656
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
5757
char[] charArray = new char[1024];
@@ -72,7 +72,7 @@ public String read_InputStreamReader_CharArrayReader(String filePath) throws Exc
7272
/**
7373
* 对照组
7474
*/
75-
public String read_InputStreamReader_text(String str) throws Exception {
75+
public static String read_InputStreamReader_text(String str) throws Exception {
7676
InputStream inputStream = new ByteArrayInputStream(str.getBytes());
7777

7878
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
@@ -86,7 +86,7 @@ public String read_InputStreamReader_text(String str) throws Exception {
8686
return content.toString();
8787
}
8888

89-
public static String read_InputStreamReader_BufferedInputStream(String filePath) throws Exception {
89+
public static String read_FileInputStream_BufferedInputStream(String filePath) throws Exception {
9090
FileInputStream fileInputStream = new FileInputStream(filePath);
9191
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
9292
byte[] buf = new byte[1024];
@@ -101,7 +101,7 @@ public static String read_InputStreamReader_BufferedInputStream(String filePath)
101101
}
102102

103103
// java.io.FileInputStream
104-
public String read_FileInputStream(String filePath) {
104+
public static String read_FileInputStream(String filePath) {
105105
String content = "";
106106
try {
107107
FileInputStream fileInputStream = new FileInputStream(filePath);
@@ -120,7 +120,7 @@ public String read_FileInputStream(String filePath) {
120120
* FileReader
121121
*/
122122
// java.io.BufferedReader.readLine()
123-
public String read_FileReader_bufferedReader1(String filePath) throws Exception {
123+
public static String read_FileReader_bufferedReader1(String filePath) throws Exception {
124124
StringBuilder content = new StringBuilder();
125125
FileReader reader = new FileReader(filePath);
126126
BufferedReader bufferedReader = new BufferedReader(reader);
@@ -134,7 +134,7 @@ public String read_FileReader_bufferedReader1(String filePath) throws Exception
134134
}
135135

136136
// java.io.BufferedReader.read()
137-
public String read_FileReader_bufferedReader2(String filePath) throws Exception {
137+
public static String read_FileReader_bufferedReader2(String filePath) throws Exception {
138138
StringBuilder content = new StringBuilder();
139139
FileReader reader = new FileReader(filePath);
140140
BufferedReader bufferedReader = new BufferedReader(reader);
@@ -151,7 +151,7 @@ public String read_FileReader_bufferedReader2(String filePath) throws Exception
151151

152152
// java.io.FileReader.read()
153153
// 不套其他 直接通过 FileReader 读取
154-
public String read_FileReader(String filePath) throws Exception {
154+
public static String read_FileReader(String filePath) throws Exception {
155155
StringBuilder content = new StringBuilder();
156156
FileReader reader = new FileReader(filePath);
157157
int character;
@@ -166,7 +166,7 @@ public String read_FileReader(String filePath) throws Exception {
166166
}
167167

168168
// java.io.LineNumberReader.read()
169-
public String read_FileReader_LineNumberReader(String filePath) throws Exception {
169+
public static String read_FileReader_LineNumberReader(String filePath) throws Exception {
170170
StringBuilder content = new StringBuilder();
171171
FileReader reader = new FileReader(filePath);
172172
LineNumberReader lineNumberReader = new LineNumberReader(reader);
@@ -180,7 +180,7 @@ public String read_FileReader_LineNumberReader(String filePath) throws Exception
180180
}
181181

182182
// java.io.CharArrayReader.read()
183-
public String read_FileReader_CharArrayReader(String filePath) throws Exception {
183+
public static String read_FileReader_CharArrayReader(String filePath) throws Exception {
184184
StringBuilder content = new StringBuilder();
185185

186186
FileReader reader = new FileReader(filePath);
@@ -199,7 +199,7 @@ public String read_FileReader_CharArrayReader(String filePath) throws Exception
199199
}
200200

201201
// java.io.PushbackReader
202-
public String read_PushbackReader(String filePath) throws Exception {
202+
public static String read_PushbackReader(String filePath) throws Exception {
203203
StringBuilder content = new StringBuilder();
204204
FileReader reader = new FileReader(filePath);
205205
PushbackReader pushbackReader = new PushbackReader(reader);
@@ -218,13 +218,13 @@ public String read_PushbackReader(String filePath) throws Exception {
218218
* Files
219219
*/
220220
// java.nio.file.Files.readAllBytes()
221-
public String read_Files_readAllBytes(String filePath) throws Exception {
221+
public static String read_Files_readAllBytes(String filePath) throws Exception {
222222
byte[] data = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(filePath));
223223
return new String(data, "UTF-8");
224224
}
225225

226226
// java.nio.file.Files.readAllLines()
227-
public String read_Files_readAllLines(String filePath) throws Exception {
227+
public static String read_Files_readAllLines(String filePath) throws Exception {
228228
StringBuilder content = new StringBuilder();
229229
java.util.List<String> lines = java.nio.file.Files.readAllLines(java.nio.file.Paths.get(filePath));
230230
for (String line : lines) {
@@ -237,7 +237,7 @@ public String read_Files_readAllLines(String filePath) throws Exception {
237237
* Scanner
238238
*/
239239
// java.util.Scanner
240-
public String read_Scanner_File(String filePath) throws Exception {
240+
public static String read_Scanner_File(String filePath) throws Exception {
241241
StringBuilder content = new StringBuilder();
242242
Scanner scanner = new Scanner(new File(filePath));
243243
while (scanner.hasNextLine()) {
@@ -248,7 +248,7 @@ public String read_Scanner_File(String filePath) throws Exception {
248248
}
249249

250250
// java.util.Scanner
251-
public String read_Scanner_Path(String filePath) throws Exception {
251+
public static String read_Scanner_Path(String filePath) throws Exception {
252252
StringBuilder content = new StringBuilder();
253253
Scanner scanner = new Scanner(java.nio.file.Paths.get(filePath));
254254
while (scanner.hasNextLine()) {
@@ -262,7 +262,7 @@ public String read_Scanner_Path(String filePath) throws Exception {
262262
* java.io.RandomAccessFile
263263
* readLine()
264264
*/
265-
public String read_RandomAccessFile_readLine(String filePath) throws Exception {
265+
public static String read_RandomAccessFile_readLine(String filePath) throws Exception {
266266
RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "r");
267267
StringBuilder content = new StringBuilder();
268268
String line;
@@ -279,7 +279,7 @@ public String read_RandomAccessFile_readLine(String filePath) throws Exception {
279279
* java.io.RandomAccessFile
280280
* read()
281281
*/
282-
public String read_RandomAccessFile_read(String filePath) throws Exception {
282+
public static String read_RandomAccessFile_read(String filePath) throws Exception {
283283
RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "r");
284284
StringBuilder content = new StringBuilder();
285285
int character;
@@ -295,7 +295,7 @@ public String read_RandomAccessFile_read(String filePath) throws Exception {
295295
}
296296

297297
// java.nio.channels.FileChannel
298-
public String read_FileChannel(String filePath) throws Exception {
298+
public static String read_FileChannel(String filePath) throws Exception {
299299
StringBuilder content = new StringBuilder();
300300
FileInputStream fileInputStream = new FileInputStream(filePath);
301301
java.nio.channels.FileChannel fileChannel = fileInputStream.getChannel();
@@ -311,7 +311,7 @@ public String read_FileChannel(String filePath) throws Exception {
311311
}
312312

313313
// java.nio.channels.FileChannel.open
314-
public String read_FileChannel_open(String filePath) throws Exception {
314+
public static String read_FileChannel_open(String filePath) throws Exception {
315315
StringBuilder content = new StringBuilder();
316316
java.nio.channels.FileChannel fileChannel = java.nio.channels.FileChannel.open(java.nio.file.Paths.get(filePath));
317317
java.nio.ByteBuffer byteBuffer = java.nio.ByteBuffer.allocate(1024);
@@ -327,7 +327,14 @@ public String read_FileChannel_open(String filePath) throws Exception {
327327
/**
328328
* org.apache.commons.io.FileUtils
329329
*/
330-
public String read_FileUtils(String filePath) throws Exception {
330+
public static String read_FileUtils(String filePath) throws Exception {
331331
return org.apache.commons.io.FileUtils.readFileToString(new File(filePath), "UTF-8");
332332
}
333+
334+
public static String read_IOUtils(String filePath) throws Exception {
335+
FileInputStream input = new FileInputStream(filePath);
336+
byte[] data = new byte[(int) new File(filePath).length()];
337+
org.apache.commons.io.IOUtils.readFully(input, data);
338+
return new String(data, "UTF-8");
339+
}
333340
}

FilesOperations/src/main/java/org/example/FileWrite.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,30 @@ public class FileWrite {
99

1010
public static void main(String[] args) throws Exception{
1111
String path = "/tmp/1.txt";
12-
String content = "Hello World!";
13-
new FileWrite().write_FileWriter_CharArrayWriter(path, content);
12+
String content = "Hello Whoopsunix!";
13+
FileWrite.write_FileOutputStream_file(path, content);
1414
}
1515

1616
/**
1717
* java.io.FileWriter
1818
*/
1919
// java.io.OutputStreamWriter
20-
public void write_OutputStreamWriter(String filePath, String context) throws Exception {
20+
public static void write_OutputStreamWriter(String filePath, String context) throws Exception {
2121
OutputStream outputStream = new FileOutputStream(filePath);
2222
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
2323
outputStreamWriter.write(context);
2424
outputStreamWriter.close();
2525
}
2626

2727
// java.io.FileWriter
28-
public void write_FileWriter(String filePath, String context) throws Exception {
28+
public static void write_FileWriter(String filePath, String context) throws Exception {
2929
FileWriter fileWriter = new FileWriter(filePath);
3030
fileWriter.write(context);
3131
fileWriter.close();
3232
}
3333

3434
// java.io.BufferedWriter
35-
public void write_FileWriter_BufferedWriter(String filePath, String context) throws Exception {
35+
public static void write_FileWriter_BufferedWriter(String filePath, String context) throws Exception {
3636
FileWriter fileWriter = new FileWriter(filePath);
3737
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
3838
bufferedWriter.write(context);
@@ -41,7 +41,7 @@ public void write_FileWriter_BufferedWriter(String filePath, String context) thr
4141
}
4242

4343
// java.io.CharArrayWriter
44-
public void write_FileWriter_CharArrayWriter(String filePath, String context) throws Exception {
44+
public static void write_FileWriter_CharArrayWriter(String filePath, String context) throws Exception {
4545
CharArrayWriter charArrayWriter = new CharArrayWriter();
4646
charArrayWriter.write(context);
4747
FileWriter fileWriter = new FileWriter(filePath);
@@ -51,7 +51,7 @@ public void write_FileWriter_CharArrayWriter(String filePath, String context) th
5151
}
5252

5353
// java.io.PrintWriter
54-
public void write_FileWriter_PrintWriter(String filePath, String context) throws Exception {
54+
public static void write_FileWriter_PrintWriter(String filePath, String context) throws Exception {
5555
PrintWriter printWriter = new PrintWriter(new FileWriter(filePath));
5656
printWriter.println(context);
5757
printWriter.close();
@@ -62,35 +62,42 @@ public void write_FileWriter_PrintWriter(String filePath, String context) throws
6262
* java.io.PrintWriter
6363
*/
6464
// java.io.BufferedWriter java.io.FileWriter
65-
public void write_PrintWriter_BufferedWriter_FileWriter(String filePath, String context) throws Exception {
65+
public static void write_PrintWriter_BufferedWriter_FileWriter(String filePath, String context) throws Exception {
6666
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
6767
out.println(context);
6868
out.close();
6969
}
7070

7171
// java.io.FileOutputStream.write()
72-
public void write_FileOutputStream(String filePath, String context) throws Exception {
72+
public static void write_FileOutputStream(String filePath, String context) throws Exception {
7373
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
7474
fileOutputStream.write(context.getBytes());
7575
fileOutputStream.close();
7676
}
7777

78+
public static void write_FileOutputStream_file(String filePath, String context) throws Exception {
79+
File file = new File(filePath);
80+
FileOutputStream fileOutputStream = new FileOutputStream(file);
81+
fileOutputStream.write(context.getBytes());
82+
fileOutputStream.close();
83+
}
84+
7885
// java.io.PrintStream
79-
public void write_PrintStream(String filePath, String context) throws Exception {
86+
public static void write_PrintStream(String filePath, String context) throws Exception {
8087
PrintStream printStream = new PrintStream(filePath);
8188
printStream.print(context);
8289
printStream.close();
8390
}
8491

8592
// java.io.RandomAccessFile
86-
public void write_RandomAccessFile(String filePath, String context) throws Exception {
93+
public static void write_RandomAccessFile(String filePath, String context) throws Exception {
8794
RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "rw");
8895
randomAccessFile.write(context.getBytes());
8996
randomAccessFile.close();
9097
}
9198

9299
// java.nio.channels.FileChannel
93-
public void write_FileChannel(String filePath, String context) throws Exception {
100+
public static void write_FileChannel(String filePath, String context) throws Exception {
94101
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
95102
java.nio.channels.FileChannel fileChannel = fileOutputStream.getChannel();
96103
fileChannel.write(java.nio.ByteBuffer.wrap(context.getBytes()));
@@ -99,12 +106,12 @@ public void write_FileChannel(String filePath, String context) throws Exception
99106
}
100107

101108
// java.nio.file.Files
102-
public void write_Files(String filePath, String context) throws Exception {
109+
public static void write_Files(String filePath, String context) throws Exception {
103110
java.nio.file.Files.write(java.nio.file.Paths.get(filePath), context.getBytes());
104111
}
105112

106113
// org.apache.commons.io.FileUtils
107-
public void write_FileUtils(String filePath, String context) throws Exception {
114+
public static void write_FileUtils(String filePath, String context) throws Exception {
108115
org.apache.commons.io.FileUtils.writeStringToFile(new File(filePath), context, "UTF-8");
109116
}
110117
}

0 commit comments

Comments
 (0)