8
8
*/
9
9
public class FileRead {
10
10
public static void main (String [] args ) throws Exception {
11
- String s = read_InputStreamReader_BufferedInputStream ("/etc/passwd" );
11
+ String s = read_FileUtils ("/etc/passwd" );
12
12
System .out .println (s );
13
13
}
14
14
@@ -17,7 +17,7 @@ public static void main(String[] args) throws Exception {
17
17
* java.io.InputStreamReader
18
18
* 自带的 read()
19
19
*/
20
- public String read_InputStreamReader (String filePath ) throws Exception {
20
+ public static String read_InputStreamReader (String filePath ) throws Exception {
21
21
FileInputStream fileInputStream = new FileInputStream (filePath );
22
22
23
23
InputStreamReader inputStreamReader = new InputStreamReader (fileInputStream );
@@ -38,7 +38,7 @@ public String read_InputStreamReader(String filePath) throws Exception {
38
38
* java.io.InputStreamReader
39
39
* java.io.BufferedReader
40
40
*/
41
- public String read_InputStreamReader_BufferedReader (String filePath ) throws Exception {
41
+ public static String read_InputStreamReader_BufferedReader (String filePath ) throws Exception {
42
42
FileInputStream fileInputStream = new FileInputStream (filePath );
43
43
InputStreamReader inputStreamReader = new InputStreamReader (fileInputStream );
44
44
BufferedReader bufferedReader = new BufferedReader (inputStreamReader );
@@ -51,7 +51,7 @@ public String read_InputStreamReader_BufferedReader(String filePath) throws Exce
51
51
return content .toString ();
52
52
}
53
53
54
- public String read_InputStreamReader_CharArrayReader (String filePath ) throws Exception {
54
+ public static String read_InputStreamReader_CharArrayReader (String filePath ) throws Exception {
55
55
FileInputStream fileInputStream = new FileInputStream (filePath );
56
56
InputStreamReader inputStreamReader = new InputStreamReader (fileInputStream );
57
57
char [] charArray = new char [1024 ];
@@ -72,7 +72,7 @@ public String read_InputStreamReader_CharArrayReader(String filePath) throws Exc
72
72
/**
73
73
* 对照组
74
74
*/
75
- public String read_InputStreamReader_text (String str ) throws Exception {
75
+ public static String read_InputStreamReader_text (String str ) throws Exception {
76
76
InputStream inputStream = new ByteArrayInputStream (str .getBytes ());
77
77
78
78
InputStreamReader inputStreamReader = new InputStreamReader (inputStream );
@@ -86,7 +86,7 @@ public String read_InputStreamReader_text(String str) throws Exception {
86
86
return content .toString ();
87
87
}
88
88
89
- public static String read_InputStreamReader_BufferedInputStream (String filePath ) throws Exception {
89
+ public static String read_FileInputStream_BufferedInputStream (String filePath ) throws Exception {
90
90
FileInputStream fileInputStream = new FileInputStream (filePath );
91
91
BufferedInputStream bufferedInputStream = new BufferedInputStream (fileInputStream );
92
92
byte [] buf = new byte [1024 ];
@@ -101,7 +101,7 @@ public static String read_InputStreamReader_BufferedInputStream(String filePath)
101
101
}
102
102
103
103
// java.io.FileInputStream
104
- public String read_FileInputStream (String filePath ) {
104
+ public static String read_FileInputStream (String filePath ) {
105
105
String content = "" ;
106
106
try {
107
107
FileInputStream fileInputStream = new FileInputStream (filePath );
@@ -120,7 +120,7 @@ public String read_FileInputStream(String filePath) {
120
120
* FileReader
121
121
*/
122
122
// 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 {
124
124
StringBuilder content = new StringBuilder ();
125
125
FileReader reader = new FileReader (filePath );
126
126
BufferedReader bufferedReader = new BufferedReader (reader );
@@ -134,7 +134,7 @@ public String read_FileReader_bufferedReader1(String filePath) throws Exception
134
134
}
135
135
136
136
// 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 {
138
138
StringBuilder content = new StringBuilder ();
139
139
FileReader reader = new FileReader (filePath );
140
140
BufferedReader bufferedReader = new BufferedReader (reader );
@@ -151,7 +151,7 @@ public String read_FileReader_bufferedReader2(String filePath) throws Exception
151
151
152
152
// java.io.FileReader.read()
153
153
// 不套其他 直接通过 FileReader 读取
154
- public String read_FileReader (String filePath ) throws Exception {
154
+ public static String read_FileReader (String filePath ) throws Exception {
155
155
StringBuilder content = new StringBuilder ();
156
156
FileReader reader = new FileReader (filePath );
157
157
int character ;
@@ -166,7 +166,7 @@ public String read_FileReader(String filePath) throws Exception {
166
166
}
167
167
168
168
// 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 {
170
170
StringBuilder content = new StringBuilder ();
171
171
FileReader reader = new FileReader (filePath );
172
172
LineNumberReader lineNumberReader = new LineNumberReader (reader );
@@ -180,7 +180,7 @@ public String read_FileReader_LineNumberReader(String filePath) throws Exception
180
180
}
181
181
182
182
// 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 {
184
184
StringBuilder content = new StringBuilder ();
185
185
186
186
FileReader reader = new FileReader (filePath );
@@ -199,7 +199,7 @@ public String read_FileReader_CharArrayReader(String filePath) throws Exception
199
199
}
200
200
201
201
// java.io.PushbackReader
202
- public String read_PushbackReader (String filePath ) throws Exception {
202
+ public static String read_PushbackReader (String filePath ) throws Exception {
203
203
StringBuilder content = new StringBuilder ();
204
204
FileReader reader = new FileReader (filePath );
205
205
PushbackReader pushbackReader = new PushbackReader (reader );
@@ -218,13 +218,13 @@ public String read_PushbackReader(String filePath) throws Exception {
218
218
* Files
219
219
*/
220
220
// 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 {
222
222
byte [] data = java .nio .file .Files .readAllBytes (java .nio .file .Paths .get (filePath ));
223
223
return new String (data , "UTF-8" );
224
224
}
225
225
226
226
// 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 {
228
228
StringBuilder content = new StringBuilder ();
229
229
java .util .List <String > lines = java .nio .file .Files .readAllLines (java .nio .file .Paths .get (filePath ));
230
230
for (String line : lines ) {
@@ -237,7 +237,7 @@ public String read_Files_readAllLines(String filePath) throws Exception {
237
237
* Scanner
238
238
*/
239
239
// java.util.Scanner
240
- public String read_Scanner_File (String filePath ) throws Exception {
240
+ public static String read_Scanner_File (String filePath ) throws Exception {
241
241
StringBuilder content = new StringBuilder ();
242
242
Scanner scanner = new Scanner (new File (filePath ));
243
243
while (scanner .hasNextLine ()) {
@@ -248,7 +248,7 @@ public String read_Scanner_File(String filePath) throws Exception {
248
248
}
249
249
250
250
// java.util.Scanner
251
- public String read_Scanner_Path (String filePath ) throws Exception {
251
+ public static String read_Scanner_Path (String filePath ) throws Exception {
252
252
StringBuilder content = new StringBuilder ();
253
253
Scanner scanner = new Scanner (java .nio .file .Paths .get (filePath ));
254
254
while (scanner .hasNextLine ()) {
@@ -262,7 +262,7 @@ public String read_Scanner_Path(String filePath) throws Exception {
262
262
* java.io.RandomAccessFile
263
263
* readLine()
264
264
*/
265
- public String read_RandomAccessFile_readLine (String filePath ) throws Exception {
265
+ public static String read_RandomAccessFile_readLine (String filePath ) throws Exception {
266
266
RandomAccessFile randomAccessFile = new RandomAccessFile (filePath , "r" );
267
267
StringBuilder content = new StringBuilder ();
268
268
String line ;
@@ -279,7 +279,7 @@ public String read_RandomAccessFile_readLine(String filePath) throws Exception {
279
279
* java.io.RandomAccessFile
280
280
* read()
281
281
*/
282
- public String read_RandomAccessFile_read (String filePath ) throws Exception {
282
+ public static String read_RandomAccessFile_read (String filePath ) throws Exception {
283
283
RandomAccessFile randomAccessFile = new RandomAccessFile (filePath , "r" );
284
284
StringBuilder content = new StringBuilder ();
285
285
int character ;
@@ -295,7 +295,7 @@ public String read_RandomAccessFile_read(String filePath) throws Exception {
295
295
}
296
296
297
297
// java.nio.channels.FileChannel
298
- public String read_FileChannel (String filePath ) throws Exception {
298
+ public static String read_FileChannel (String filePath ) throws Exception {
299
299
StringBuilder content = new StringBuilder ();
300
300
FileInputStream fileInputStream = new FileInputStream (filePath );
301
301
java .nio .channels .FileChannel fileChannel = fileInputStream .getChannel ();
@@ -311,7 +311,7 @@ public String read_FileChannel(String filePath) throws Exception {
311
311
}
312
312
313
313
// 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 {
315
315
StringBuilder content = new StringBuilder ();
316
316
java .nio .channels .FileChannel fileChannel = java .nio .channels .FileChannel .open (java .nio .file .Paths .get (filePath ));
317
317
java .nio .ByteBuffer byteBuffer = java .nio .ByteBuffer .allocate (1024 );
@@ -327,7 +327,14 @@ public String read_FileChannel_open(String filePath) throws Exception {
327
327
/**
328
328
* org.apache.commons.io.FileUtils
329
329
*/
330
- public String read_FileUtils (String filePath ) throws Exception {
330
+ public static String read_FileUtils (String filePath ) throws Exception {
331
331
return org .apache .commons .io .FileUtils .readFileToString (new File (filePath ), "UTF-8" );
332
332
}
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
+ }
333
340
}
0 commit comments