-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuavaFileIOTest.java
77 lines (68 loc) · 2.38 KB
/
GuavaFileIOTest.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package medium;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class GuavaFileIOTest {
@Test
public void testWriteAndReadSmallFiles() throws Exception {
File file = new File("guava.txt");
Files.write("Just For Test\nThe second line\nThe Third line".getBytes(), file);
List<String> lines = Files.readLines(file, Charsets.UTF_8);
assertEquals(3, lines.size());
file.delete();
}
@Test
public void testWriteAndReadBigFiles() throws Exception {
File file = new File("guava.txt");
if(file.exists()){
file.delete();
file.createNewFile();
}
for(int i=0; i<10000; i++){
Files.append("Just For Test\n", file, Charsets.UTF_8);
}
LineProcessor<Integer> countProcesser = new LineProcessor<Integer>() {
private int num;
@Override
public boolean processLine(String line) throws IOException {
num++;
return true;
}
@Override
public Integer getResult() {
return num;
}
};
Files.readLines(file, Charsets.UTF_8, countProcesser);
Integer retInt = countProcesser.getResult();
file.delete();
assertEquals(retInt, countProcesser.getResult());
}
@Test
public void testGuavaFileCopy() throws Exception {
File srcFile = new File("src.txt");
File destFile = new File("dest.txt");
Files.write("Just For Test".getBytes(), srcFile);
Files.copy(srcFile, destFile);
List<String> lines = Files.readLines(destFile, Charsets.UTF_8);
srcFile.delete();
destFile.delete();
assertEquals(1, lines.size());
assertEquals("Just For Test", lines.get(0));
}
@Test
public void testGuavaFilesCopy() throws Exception {
File srcFile = new File("src.txt");
File destFile = new File("dest.txt");
Files.write("Just For Test".getBytes(), srcFile);
Files.copy(srcFile, destFile);
assertTrue(Files.equal(srcFile,destFile));
}
}