Skip to content

Commit 0ed2063

Browse files
committed
添加测试
1 parent b405aa6 commit 0ed2063

File tree

5 files changed

+57
-15
lines changed

5 files changed

+57
-15
lines changed

src/main/java/io/github/kavahub/file/writer/AIOFileWriter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.kavahub.file.writer;
22

3+
import java.io.IOException;
34
import java.nio.file.Path;
45
import java.nio.file.StandardOpenOption;
56
import java.util.concurrent.CompletableFuture;
@@ -11,6 +12,14 @@
1112
@UtilityClass
1213
@Slf4j
1314
public class AIOFileWriter {
15+
public CompletableFileWriter of(Path file) throws IOException {
16+
return of(file, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
17+
}
18+
19+
public CompletableFileWriter of(Path file, StandardOpenOption... options) throws IOException {
20+
return CompletableFileWriter.of(file, options);
21+
}
22+
1423
public CompletableFuture<Integer> write(Path file, byte[] bytes) {
1524
return write(file, bytes, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
1625
}

src/test/java/io/github/kavahub/file/AIOFileReaderExample.java renamed to src/test/java/io/github/kavahub/file/AIOFileReaderManualTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import io.github.kavahub.file.reader.AIOFileReader;
1313

14-
public class AIOFileReaderExample {
14+
public class AIOFileReaderManualTest {
1515
@Test
1616
public void giveFile_whenReadLine_thenPrint() {
1717
final Path FILE = Paths.get("src", "test", "resources", "fileWithmanyOfLine.txt");

src/test/java/io/github/kavahub/file/AIOFileWriterExample.java renamed to src/test/java/io/github/kavahub/file/AIOFileWriterManualTest.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import io.github.kavahub.file.reader.AIOFileReader;
1313
import io.github.kavahub.file.writer.AIOFileWriter;
1414

15-
public class AIOFileWriterExample {
15+
public class AIOFileWriterManualTest {
1616
private static final Path FILE_TO_WRITE = Paths.get("target", "fileToWrite.txt");
1717

1818
@BeforeEach
@@ -34,9 +34,8 @@ public void whenWriteWithStringSplit() throws IOException {
3434

3535
@Test
3636
public void whenWriteWithQueryFlatMapMerge() throws IOException {
37-
Query<String> data = Query.of("This is file content:你好")
38-
.flatMapMerge(line -> Query.of(line.split(" ")))
39-
.map((line) -> line + System.lineSeparator());
37+
Query<String> data = Query.of("This is file content:你好").flatMapMerge(line -> Query.of(line.split(" ")))
38+
.map((line) -> line + System.lineSeparator());
4039
AIOFileWriter.write(FILE_TO_WRITE, data).join();
4140
}
4241

@@ -45,14 +44,14 @@ public void whenWriteWithQueryFilter() throws IOException {
4544
final Path FILE = Paths.get("src", "test", "resources", "fileWithmanyOfLine.txt");
4645

4746
Query<String> reader = AIOFileReader.line(FILE)
48-
// 忽略前2行
49-
.skip(2)
50-
// 过滤掉空行
51-
.filter(line -> !line.isBlank())
52-
// 转换成大写
53-
.map(String::toUpperCase)
54-
// 加入换行符
55-
.map((line) -> line + System.lineSeparator());
47+
// 忽略前2行
48+
.skip(2)
49+
// 过滤掉空行
50+
.filter(line -> !line.isBlank())
51+
// 转换成大写
52+
.map(String::toUpperCase)
53+
// 加入换行符
54+
.map((line) -> line + System.lineSeparator());
5655
AIOFileWriter.write(FILE_TO_WRITE, reader).join();
57-
}
56+
}
5857
}

src/test/java/io/github/kavahub/file/NIOFileLineReaderExample.java renamed to src/test/java/io/github/kavahub/file/NIOFileLineReaderManualTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import io.github.kavahub.file.reader.NIOFileLineReader;
1212

13-
public class NIOFileLineReaderExample {
13+
public class NIOFileLineReaderManualTest {
1414

1515

1616
@Test
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.kavahub.file;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Paths;
5+
import java.util.Scanner;
6+
7+
import io.github.kavahub.file.writer.AIOFileWriter;
8+
import io.github.kavahub.file.writer.CompletableFileWriter;
9+
10+
public class SystemInToFileExample {
11+
12+
public static void main(String[] arg) throws IOException {
13+
// 异步写入文件
14+
CompletableFileWriter writer = AIOFileWriter.of(Paths.get("systemIn.txt"));
15+
16+
try (Scanner scanner = new Scanner(System.in)) {
17+
while (scanner.hasNext()) {
18+
String line = scanner.next();
19+
if ("bye".equals(line)) {
20+
break;
21+
}
22+
writer.write(line + System.lineSeparator());
23+
}
24+
25+
writer.getPosition().whenComplete((size, error) -> {
26+
if (error != null) {
27+
error.printStackTrace();
28+
}
29+
30+
System.out.println("输入字节数:" + size);
31+
});
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)