4
4
框架提供Java异步读写文件功能,除依赖slf4j日志包,不依赖其他第三方包。Java应用程序引入框架可以简单的,异步和非阻塞的读写文件。框架包含三个工具类:
5
5
6
6
7
- [ ` AIOFileReader ` ] ( src/main/java/net/kava /file/reader/AIOFileReader.java ) 异步读取文件,使用Java NIO库 [ ` AsynchronousFileChannel ` ] ( https://docs.oracle.com/javase/10/docs/api/java/nio/channels/AsynchronousFileChannel.html ) 和 [ ` CompletionHandler ` ] ( https://docs.oracle.com/javase/10/docs/api/java/nio/channels/CompletionHandler.html ) 实现。
7
+ [ ` AIOFileReader ` ] ( src/main/java/io/github/kavahub /file/reader/AIOFileReader.java ) 异步读取文件,使用Java NIO库 [ ` AsynchronousFileChannel ` ] ( https://docs.oracle.com/javase/10/docs/api/java/nio/channels/AsynchronousFileChannel.html ) 和 [ ` CompletionHandler ` ] ( https://docs.oracle.com/javase/10/docs/api/java/nio/channels/CompletionHandler.html ) 实现。
8
8
9
- [ ` AIOFileWriter ` ] ( src/main/java/net/kava/ file/reader /AIOFileWriter.java ) 异步写入文件,使用Java NIO库 [ ` AsynchronousFileChannel ` ] ( https://docs.oracle.com/javase/10/docs/api/java/nio/channels/AsynchronousFileChannel.html ) 和 [ ` CompletionHandler ` ] ( https://docs.oracle.com/javase/10/docs/api/java/nio/channels/CompletionHandler.html ) 实现。
9
+ [ ` AIOFileWriter ` ] ( src/main/java/io/github/kavahub/ file/writer /AIOFileWriter.java ) 异步写入文件,使用Java NIO库 [ ` AsynchronousFileChannel ` ] ( https://docs.oracle.com/javase/10/docs/api/java/nio/channels/AsynchronousFileChannel.html ) 和 [ ` CompletionHandler ` ] ( https://docs.oracle.com/javase/10/docs/api/java/nio/channels/CompletionHandler.html ) 实现。
10
10
11
- [ ` NIOFileLineReader ` ] ( src/main/java/net/kava /file/reader/NIOFileLineReader.java ) 非阻塞读取文件,使用 [ ` ForkJoinPool ` ] ( https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/ForkJoinPool.html ) 和 [ ` BufferedReader ` ] ( https://docs.oracle.com/javase/10/docs/api/java/io/BufferedReader.html ) 实现
11
+ [ ` NIOFileLineReader ` ] ( src/main/java/io/github/kavahub /file/reader/NIOFileLineReader.java ) 非阻塞读取文件,使用 [ ` ForkJoinPool ` ] ( https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/ForkJoinPool.html ) 和 [ ` BufferedReader ` ] ( https://docs.oracle.com/javase/10/docs/api/java/io/BufferedReader.html ) 实现
12
12
13
13
Java提供的 [ ` Files ` ] ( https://docs.oracle.com/javase/10/docs/api/java/nio/file/Files.html ) 文件读取功能是阻塞的。
14
14
@@ -32,15 +32,15 @@ implementation 'net.kava:kava-async-file:1.0.0'
32
32
33
33
#### 使用说明
34
34
35
- [ ` AIOFileReader ` ] ( src/main/java/net/kava /file/reader/AIOFileReader.java ) 方法列表:
35
+ [ ` AIOFileReader ` ] ( src/main/java/io/github/kavahub /file/reader/AIOFileReader.java ) 方法列表:
36
36
37
- ` Query<byte[]> bytes(Path file) ` : 读取文件,返回文件数据字节数组,读取的大小有默认缓冲区决定。
37
+ - ` Query<byte[]> bytes(Path file) ` : 读取文件,返回文件数据字节数组,读取的大小有默认缓冲区决定。
38
38
39
- ` Query<byte[]> allBytes(Path file) ` : 读取文件,返回文件所有数据字节数组。每次按默认缓冲区读取文件,完成后合并。
39
+ - ` Query<byte[]> allBytes(Path file) ` : 读取文件,返回文件所有数据字节数组。每次按默认缓冲区读取文件,完成后合并。
40
40
41
- ` Query<String> line(Path file) ` : 读取文件,返回文件行字符串。每次按默认缓冲区读取文件数据字节数组,按换行符分割字节数组。
41
+ - ` Query<String> line(Path file) ` : 读取文件,返回文件行字符串。每次按默认缓冲区读取文件数据字节数组,按换行符分割字节数组。
42
42
43
- ` Query<String> allLines(Path file) ` : 读取文件,返回文件所有数据字符串。每次按默认缓冲区读取文件数据字节数组,合并后转换成字符串。
43
+ - ` Query<String> allLines(Path file) ` : 读取文件,返回文件所有数据字符串。每次按默认缓冲区读取文件数据字节数组,合并后转换成字符串。
44
44
45
45
默认缓冲区大小定义:
46
46
@@ -220,18 +220,18 @@ Thread-7
220
220
...
221
221
```
222
222
223
- 其结果表明:有两个线程读取文件,线程交替读取以保证读取文件数据的顺序,是 [ ` AsynchronousFileChannel ` ] ( https://docs.oracle.com/javase/10/docs/api/java/nio/channels/AsynchronousFileChannel.html ) 实现的
223
+ 其结果表明:有两个线程读取文件,线程交替读取以保证读取文件数据的顺序,这是 [ ` AsynchronousFileChannel ` ] ( https://docs.oracle.com/javase/10/docs/api/java/nio/channels/AsynchronousFileChannel.html ) 实现的
224
224
225
225
226
- [ ` AIOFileWriter ` ] ( src/main/java/net/kava/ file/reader/AIOFileReader .java ) 方法列表:
226
+ [ ` AIOFileWriter ` ] ( src/main/java/io/github/kavahub/ file/writer/AIOFileWriter .java ) 方法列表:
227
227
228
- ` CompletableFuture<Integer> write(Path file, byte[] bytes) ` : 字节数组数据写入文件。
228
+ - ` CompletableFuture<Integer> write(Path file, byte[] bytes) ` : 字节数组数据写入文件。
229
229
230
- ` CompletableFuture<Integer> write(Path file, String line) ` : 字符串数据写入文件。
230
+ - ` CompletableFuture<Integer> write(Path file, String line) ` : 字符串数据写入文件。
231
231
232
- ` CompletableFuture<Integer> write(Path file, Query<String> lines) ` : 字符串流数据写入文件。
232
+ - ` CompletableFuture<Integer> write(Path file, Query<String> lines) ` : 字符串流数据写入文件。
233
233
234
- ` CompletableFuture<Integer> write(Path file, Iterable<String> lines) ` : 字符串集合数据写入文件。
234
+ - ` CompletableFuture<Integer> write(Path file, Iterable<String> lines) ` : 字符串集合数据写入文件。
235
235
236
236
示例:
237
237
@@ -287,7 +287,7 @@ Thread-7
287
287
AIOFileWriter . write(Paths . get(FILE_TO_WRITE ), reader). join();
288
288
```
289
289
290
- [ ` NIOFileLineReader ` ] ( src/main/java/net/kava /file/reader/NIOFileLineReader.java ) 方法列表:
290
+ [ ` NIOFileLineReader ` ] ( src/main/java/io/github/kavahub /file/reader/NIOFileLineReader.java ) 方法列表:
291
291
292
292
` Query<String> read(Path file) ` : 读取文件行。
293
293
@@ -302,17 +302,21 @@ Thread-7
302
302
303
303
关于使用的建议:
304
304
305
- 1 . 文件的异步读写,并不是未了提高文件的读取性能,而是提高文件读取的吞吐量(读取更多的文件,并保持性能,使JVM可以稳定运行)。
306
- 2 . 在大多数情况下,使用Jdk提供的[ ` Files ` ] ( https://docs.oracle.com/javase/10/docs/api/java/nio/file/Files.html ) 或许更合适。
307
- 3 . 不要为了异步而异步,找到问题所在,也许解决问题的关键不是异步。
305
+ - 文件的异步读写,并不是未了提高文件的读取性能,而是提高文件读取的吞吐量(读取更多的文件,并保持性能,使JVM可以稳定运行)。
306
+ - 在大多数情况下,使用Jdk提供的[ ` Files ` ] ( https://docs.oracle.com/javase/10/docs/api/java/nio/file/Files.html ) 或许更合适。
307
+ - 不要为了异步而异步,找到问题所在,也许解决问题的关键不是异步。
308
308
309
- 性能测试,参考 [ ` ReadLineBenchmark ` ] ( src/test/java/net/kava/ file/performance ) 。 其他开源项目文件读写的性能测试 [ ` ReadFileBenchmark ` ] ( https://gitee.com/yangyunjiao/learn-java/blob/master/core-java/core-java-io/src/main/java/net/learnjava/ReadFileBenchmark.java )
309
+ 性能测试,参考 [ ` ReadLineBenchmark ` ] ( src/test/java/io/github/kavahub/ file/performance/ReadLineBenchmark.java ) 。 其他开源项目文件读写的性能测试 [ ` ReadFileBenchmark ` ] ( https://gitee.com/yangyunjiao/learn-java/blob/master/core-java/core-java-io/src/main/java/net/learnjava/ReadFileBenchmark.java )
310
310
311
311
312
312
建议使用优先级: ` Java NIO Files ` > ` NIOFileLineReader ` > ` AIOFileReader `
313
313
314
314
#### 其他开源项目
315
315
316
- 1 . [ RxIo] ( https://github.com/javasync/RxIo )
316
+ - [ RxIo] ( https://github.com/javasync/RxIo )
317
+
318
+ #### 参考文档
319
+
320
+ - [ vsCode利用git连接github] ( https://www.jianshu.com/p/f836da434e18 )
317
321
318
322
0 commit comments