forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferReaderExampleTest.java
More file actions
31 lines (28 loc) · 1012 Bytes
/
BufferReaderExampleTest.java
File metadata and controls
31 lines (28 loc) · 1012 Bytes
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
package com.examplehub.basics.io;
import static org.junit.jupiter.api.Assertions.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
class BufferReaderExampleTest {
@Test
void testCopyFile() throws IOException {
String filename = "pom.xml";
String newFileName = "pom_bk.xml";
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(newFileName))) {
String line = bufferedReader.readLine();
while (line != null) {
String nextLine = bufferedReader.readLine();
bufferedWriter.write(line);
if (nextLine != null) {
bufferedWriter.write(System.lineSeparator());
}
bufferedWriter.flush();
line = nextLine;
}
assertEquals(new File(filename).length(), new File(newFileName).length());
}
assertTrue(Files.deleteIfExists(Paths.get(newFileName)));
}
}