|
1 | | -package test; |
| 1 | +package org.baeldung.java.io; |
2 | 2 |
|
3 | 3 | import java.io.BufferedOutputStream; |
4 | 4 | import java.io.BufferedReader; |
@@ -32,167 +32,159 @@ public class TestWriter extends TestCase { |
32 | 32 | private String fileName4 = "test4.txt"; |
33 | 33 | private String fileName5 = "test5.txt"; |
34 | 34 |
|
35 | | - public TestWriter(String name) { |
| 35 | + public TestWriter(final String name) { |
36 | 36 | super(name); |
37 | 37 | } |
38 | 38 |
|
| 39 | + @Override |
39 | 40 | protected void setUp() throws Exception { |
40 | 41 | super.setUp(); |
41 | 42 | } |
42 | 43 |
|
| 44 | + @Override |
43 | 45 | protected void tearDown() throws Exception { |
44 | 46 | super.tearDown(); |
45 | 47 | } |
46 | | - |
47 | | - |
48 | | - public void whenWriteStringUsingBufferedWritter_thenCorrect() throws IOException{ |
49 | | - String str = "Hello"; |
50 | | - BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3)); |
| 48 | + |
| 49 | + public void whenWriteStringUsingBufferedWritter_thenCorrect() throws IOException { |
| 50 | + final String str = "Hello"; |
| 51 | + final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3)); |
51 | 52 | writer.write(str); |
52 | 53 | writer.close(); |
53 | 54 | } |
54 | | - |
55 | | - public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo() throws IOException{ |
56 | | - String str = "World"; |
57 | | - BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3 , true)); |
| 55 | + |
| 56 | + public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo() throws IOException { |
| 57 | + final String str = "World"; |
| 58 | + final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3, true)); |
58 | 59 | writer.append(' '); |
59 | 60 | writer.append(str); |
60 | 61 | writer.close(); |
61 | 62 | } |
62 | | - |
63 | | - public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted() throws IOException{ |
64 | | - FileWriter fileWriter = new FileWriter(fileName); |
65 | | - PrintWriter printWriter = new PrintWriter(fileWriter); |
66 | | - printWriter.printf("Product name is %s and its price is %d $","iPhone",1000); |
| 63 | + |
| 64 | + public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted() throws IOException { |
| 65 | + final FileWriter fileWriter = new FileWriter(fileName); |
| 66 | + final PrintWriter printWriter = new PrintWriter(fileWriter); |
| 67 | + printWriter.printf("Product name is %s and its price is %d $", "iPhone", 1000); |
67 | 68 | printWriter.close(); |
68 | 69 | } |
69 | | - |
70 | | - public void whenWriteStringUsingFileOutputStream_thenCorrect() throws IOException{ |
71 | | - String str = "Hello"; |
72 | | - FileOutputStream outputStream = new FileOutputStream(fileName3); |
73 | | - byte[] strToBytes = str.getBytes(); |
| 70 | + |
| 71 | + public void whenWriteStringUsingFileOutputStream_thenCorrect() throws IOException { |
| 72 | + final String str = "Hello"; |
| 73 | + final FileOutputStream outputStream = new FileOutputStream(fileName3); |
| 74 | + final byte[] strToBytes = str.getBytes(); |
74 | 75 | outputStream.write(strToBytes); |
75 | 76 | outputStream.close(); |
76 | 77 | } |
77 | 78 |
|
78 | 79 | public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame() throws IOException { |
79 | | - String value = "Hello"; |
80 | | - FileOutputStream fos = new FileOutputStream(fileName1); |
81 | | - DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos)); |
| 80 | + final String value = "Hello"; |
| 81 | + final FileOutputStream fos = new FileOutputStream(fileName1); |
| 82 | + final DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos)); |
82 | 83 | outStream.writeUTF(value); |
83 | 84 | outStream.close(); |
84 | | - |
| 85 | + |
85 | 86 | String result; |
86 | | - FileInputStream fis = new FileInputStream(fileName1); |
87 | | - DataInputStream reader = new DataInputStream(fis); |
| 87 | + final FileInputStream fis = new FileInputStream(fileName1); |
| 88 | + final DataInputStream reader = new DataInputStream(fis); |
88 | 89 | result = reader.readUTF(); |
89 | 90 | reader.close(); |
90 | | - |
| 91 | + |
91 | 92 | assertEquals(value, result); |
92 | 93 | } |
93 | 94 |
|
94 | 95 | public void whenWriteToPositionAndEditIt_thenItShouldChange() { |
95 | | - int data1 = 2014; |
96 | | - int data2 = 1500; |
| 96 | + final int data1 = 2014; |
| 97 | + final int data2 = 1500; |
97 | 98 | testWriteToPosition(data1, 4); |
98 | 99 | assertEquals(data1, testReadFromPosition(4)); |
99 | 100 | testWriteToPosition(data2, 4); |
100 | 101 | assertEquals(data2, testReadFromPosition(4)); |
101 | 102 | } |
102 | 103 |
|
103 | | - |
104 | 104 | public void whenTryToLockFile_thenItShouldBeLocked() throws IOException { |
105 | | - RandomAccessFile stream = new RandomAccessFile(fileName4, "rw"); |
106 | | - FileChannel channel = stream.getChannel(); |
| 105 | + final RandomAccessFile stream = new RandomAccessFile(fileName4, "rw"); |
| 106 | + final FileChannel channel = stream.getChannel(); |
107 | 107 |
|
108 | 108 | FileLock lock = null; |
109 | 109 | try { |
110 | 110 | lock = channel.tryLock(); |
111 | | - } catch (OverlappingFileLockException e) { |
| 111 | + } catch (final OverlappingFileLockException e) { |
112 | 112 | stream.close(); |
113 | 113 | channel.close(); |
114 | 114 | } |
115 | 115 | stream.writeChars("test lock"); |
116 | 116 | lock.release(); |
117 | | - |
| 117 | + |
118 | 118 | stream.close(); |
119 | 119 | channel.close(); |
120 | 120 | } |
121 | 121 |
|
122 | | - |
123 | | - |
124 | | - public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException{ |
125 | | - RandomAccessFile stream = new RandomAccessFile(fileName5, "rw"); |
126 | | - FileChannel channel = stream.getChannel(); |
127 | | - String value = "Hello"; |
128 | | - byte[] strBytes = value.getBytes(); |
129 | | - ByteBuffer buffer = ByteBuffer.allocate(strBytes.length); |
| 122 | + public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException { |
| 123 | + final RandomAccessFile stream = new RandomAccessFile(fileName5, "rw"); |
| 124 | + final FileChannel channel = stream.getChannel(); |
| 125 | + final String value = "Hello"; |
| 126 | + final byte[] strBytes = value.getBytes(); |
| 127 | + final ByteBuffer buffer = ByteBuffer.allocate(strBytes.length); |
130 | 128 | buffer.put(strBytes); |
131 | 129 | buffer.flip(); |
132 | | - channel.write(buffer); |
| 130 | + channel.write(buffer); |
133 | 131 | stream.close(); |
134 | 132 | channel.close(); |
135 | | - |
136 | | - RandomAccessFile reader = new RandomAccessFile(fileName5, "r"); |
137 | | - assertEquals(value , reader.readLine()); |
| 133 | + |
| 134 | + final RandomAccessFile reader = new RandomAccessFile(fileName5, "r"); |
| 135 | + assertEquals(value, reader.readLine()); |
138 | 136 | reader.close(); |
139 | | - |
| 137 | + |
140 | 138 | } |
141 | | - |
142 | | - public void whenWriteToTmpFile_thenCorrect() throws IOException{ |
143 | | - String toWrite = "Hello"; |
144 | | - File tmpFile = File.createTempFile("test", ".tmp"); |
145 | | - FileWriter writer = new FileWriter(tmpFile); |
| 139 | + |
| 140 | + public void whenWriteToTmpFile_thenCorrect() throws IOException { |
| 141 | + final String toWrite = "Hello"; |
| 142 | + final File tmpFile = File.createTempFile("test", ".tmp"); |
| 143 | + final FileWriter writer = new FileWriter(tmpFile); |
146 | 144 | writer.write(toWrite); |
147 | 145 | writer.close(); |
148 | | - |
149 | | - BufferedReader reader = new BufferedReader(new FileReader(tmpFile)); |
| 146 | + |
| 147 | + final BufferedReader reader = new BufferedReader(new FileReader(tmpFile)); |
150 | 148 | assertEquals(toWrite, reader.readLine()); |
151 | 149 | reader.close(); |
152 | 150 | } |
153 | | - |
154 | | - public void whenWriteUsingJava7_thenCorrect() throws IOException{ |
155 | | - String str = "Hello"; |
156 | | - |
157 | | - Path path = Paths.get(fileName3); |
158 | | - byte[] strToBytes = str.getBytes(); |
159 | | - |
| 151 | + |
| 152 | + public void whenWriteUsingJava7_thenCorrect() throws IOException { |
| 153 | + final String str = "Hello"; |
| 154 | + |
| 155 | + final Path path = Paths.get(fileName3); |
| 156 | + final byte[] strToBytes = str.getBytes(); |
| 157 | + |
160 | 158 | Files.write(path, strToBytes); |
161 | | - |
162 | | - String read = Files.readAllLines(path).get(0); |
| 159 | + |
| 160 | + final String read = Files.readAllLines(path).get(0); |
163 | 161 | assertEquals(str, read); |
164 | 162 | } |
165 | | - |
| 163 | + |
166 | 164 | // use RandomAccessFile to write data at specific position in the file |
167 | | - public void testWriteToPosition(int data, long position) { |
| 165 | + public void testWriteToPosition(final int data, final long position) { |
168 | 166 | try { |
169 | | - RandomAccessFile writer = new RandomAccessFile(fileName2, "rw"); |
| 167 | + final RandomAccessFile writer = new RandomAccessFile(fileName2, "rw"); |
170 | 168 | writer.seek(position); |
171 | 169 | writer.writeInt(data); |
172 | 170 | writer.close(); |
173 | | - } catch (Exception e) { |
| 171 | + } catch (final Exception e) { |
174 | 172 | System.out.println(e.getLocalizedMessage()); |
175 | 173 | } |
176 | 174 | } |
177 | 175 |
|
178 | | - |
179 | | - |
180 | 176 | // use RandomAccessFile to read data from specific position in the file |
181 | | - public int testReadFromPosition(long position) { |
| 177 | + public int testReadFromPosition(final long position) { |
182 | 178 | int result = 0; |
183 | 179 | try { |
184 | | - RandomAccessFile reader = new RandomAccessFile(fileName2, "r"); |
| 180 | + final RandomAccessFile reader = new RandomAccessFile(fileName2, "r"); |
185 | 181 | reader.seek(position); |
186 | 182 | result = reader.readInt(); |
187 | 183 | reader.close(); |
188 | | - } catch (Exception e) { |
| 184 | + } catch (final Exception e) { |
189 | 185 | System.out.println(e.getLocalizedMessage()); |
190 | 186 | } |
191 | 187 | return result; |
192 | 188 | } |
193 | 189 |
|
194 | | - |
195 | | - |
196 | | - |
197 | | - |
198 | 190 | } |
0 commit comments