Skip to content

Commit 0e3c0f3

Browse files
author
eugenp
committed
cleanup work
1 parent 851d7c0 commit 0e3c0f3

1 file changed

Lines changed: 69 additions & 77 deletions

File tree

Lines changed: 69 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package test;
1+
package org.baeldung.java.io;
22

33
import java.io.BufferedOutputStream;
44
import java.io.BufferedReader;
@@ -32,167 +32,159 @@ public class TestWriter extends TestCase {
3232
private String fileName4 = "test4.txt";
3333
private String fileName5 = "test5.txt";
3434

35-
public TestWriter(String name) {
35+
public TestWriter(final String name) {
3636
super(name);
3737
}
3838

39+
@Override
3940
protected void setUp() throws Exception {
4041
super.setUp();
4142
}
4243

44+
@Override
4345
protected void tearDown() throws Exception {
4446
super.tearDown();
4547
}
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));
5152
writer.write(str);
5253
writer.close();
5354
}
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));
5859
writer.append(' ');
5960
writer.append(str);
6061
writer.close();
6162
}
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);
6768
printWriter.close();
6869
}
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();
7475
outputStream.write(strToBytes);
7576
outputStream.close();
7677
}
7778

7879
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));
8283
outStream.writeUTF(value);
8384
outStream.close();
84-
85+
8586
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);
8889
result = reader.readUTF();
8990
reader.close();
90-
91+
9192
assertEquals(value, result);
9293
}
9394

9495
public void whenWriteToPositionAndEditIt_thenItShouldChange() {
95-
int data1 = 2014;
96-
int data2 = 1500;
96+
final int data1 = 2014;
97+
final int data2 = 1500;
9798
testWriteToPosition(data1, 4);
9899
assertEquals(data1, testReadFromPosition(4));
99100
testWriteToPosition(data2, 4);
100101
assertEquals(data2, testReadFromPosition(4));
101102
}
102103

103-
104104
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();
107107

108108
FileLock lock = null;
109109
try {
110110
lock = channel.tryLock();
111-
} catch (OverlappingFileLockException e) {
111+
} catch (final OverlappingFileLockException e) {
112112
stream.close();
113113
channel.close();
114114
}
115115
stream.writeChars("test lock");
116116
lock.release();
117-
117+
118118
stream.close();
119119
channel.close();
120120
}
121121

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);
130128
buffer.put(strBytes);
131129
buffer.flip();
132-
channel.write(buffer);
130+
channel.write(buffer);
133131
stream.close();
134132
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());
138136
reader.close();
139-
137+
140138
}
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);
146144
writer.write(toWrite);
147145
writer.close();
148-
149-
BufferedReader reader = new BufferedReader(new FileReader(tmpFile));
146+
147+
final BufferedReader reader = new BufferedReader(new FileReader(tmpFile));
150148
assertEquals(toWrite, reader.readLine());
151149
reader.close();
152150
}
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+
160158
Files.write(path, strToBytes);
161-
162-
String read = Files.readAllLines(path).get(0);
159+
160+
final String read = Files.readAllLines(path).get(0);
163161
assertEquals(str, read);
164162
}
165-
163+
166164
// 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) {
168166
try {
169-
RandomAccessFile writer = new RandomAccessFile(fileName2, "rw");
167+
final RandomAccessFile writer = new RandomAccessFile(fileName2, "rw");
170168
writer.seek(position);
171169
writer.writeInt(data);
172170
writer.close();
173-
} catch (Exception e) {
171+
} catch (final Exception e) {
174172
System.out.println(e.getLocalizedMessage());
175173
}
176174
}
177175

178-
179-
180176
// use RandomAccessFile to read data from specific position in the file
181-
public int testReadFromPosition(long position) {
177+
public int testReadFromPosition(final long position) {
182178
int result = 0;
183179
try {
184-
RandomAccessFile reader = new RandomAccessFile(fileName2, "r");
180+
final RandomAccessFile reader = new RandomAccessFile(fileName2, "r");
185181
reader.seek(position);
186182
result = reader.readInt();
187183
reader.close();
188-
} catch (Exception e) {
184+
} catch (final Exception e) {
189185
System.out.println(e.getLocalizedMessage());
190186
}
191187
return result;
192188
}
193189

194-
195-
196-
197-
198190
}

0 commit comments

Comments
 (0)