-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutFileIO.java
More file actions
114 lines (99 loc) · 3.28 KB
/
AboutFileIO.java
File metadata and controls
114 lines (99 loc) · 3.28 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package intermediate;
import com.sandwich.koan.Koan;
import java.io.*;
import java.util.logging.Logger;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutFileIO {
@Koan
public void fileObjectDoesntCreateFile() {
File f = new File("i-never.exist");
assertEquals(f.exists(),false);
}
@Koan
public void fileCreationAndDeletion() throws IOException {
File f = new File("foo.txt");
f.createNewFile();
assertEquals(f.exists(), true);
f.delete();
assertEquals(f.exists(), false);
}
@Koan
public void basicFileWritingAndReading() throws IOException {
File file = new File("file.txt");
FileWriter fw = new FileWriter(file);
String data = "First line\nSecond line";
fw.write(data);
fw.flush();
fw.close();
char[] in = new char[data.length()];
int size = 0;
FileReader fr = new FileReader(file);
size = fr.read(in);
// No flush necessary!
fr.close();
assertEquals(size, 22);
String expected = new String(in);
assertEquals(expected.length(), 22);
assertEquals(expected,"First line\nSecond line");
file.delete();
}
@Koan
public void betterFileWritingAndReading() throws IOException {
File file = new File("file.txt");
file.deleteOnExit();
FileWriter fw = new FileWriter(file);
PrintWriter pw = new PrintWriter(fw);
pw.println("First line");
pw.println("Second line");
pw.close();
FileReader fr = new FileReader(file);
BufferedReader br = null;
try {
br = new BufferedReader(fr);
assertEquals(br.readLine(), "First line"); // first line
assertEquals(br.readLine(), "Second line"); // second line
assertEquals(br.readLine(), null); // what now?
} finally {
// anytime you open access to a file, you should close it or you may
// lock it from other processes (ie frustrate people)
closeStream(br);
}
}
private void closeStream(BufferedReader br) {
if (br != null) {
try {
br.close();
} catch (IOException x) {
Logger.getAnonymousLogger().severe("Unable to close reader.");
}
}
}
@Koan
public void directChainingForReadingAndWriting() throws IOException {
File file = new File("file.txt");
PrintWriter pw = new PrintWriter(file);
pw.println("1. line");
pw.println("2. line");
pw.close();
StringBuffer sb = new StringBuffer();
// Add the loop to go through the file line by line and add the line
// to the StringBuffer
FileReader f = new FileReader(file);
BufferedReader b = new BufferedReader(f);
// while(b!=null)
//{
// sb.append(b.readLine());
//}
String s="";
//int j=0;
s=b.readLine();
while(s!=null) {
sb.append(s);
s=b.readLine();
if(s!=null)
sb.append('\n');
}
assertEquals(sb.toString(), "1. line\n2. line");
}
}