cleanup work
This commit is contained in:
parent
851d7c0ecf
commit
0e3c0f36df
@ -1,4 +1,4 @@
|
|||||||
package test;
|
package org.baeldung.java.io;
|
||||||
|
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
@ -32,59 +32,60 @@ public class TestWriter extends TestCase {
|
|||||||
private String fileName4 = "test4.txt";
|
private String fileName4 = "test4.txt";
|
||||||
private String fileName5 = "test5.txt";
|
private String fileName5 = "test5.txt";
|
||||||
|
|
||||||
public TestWriter(String name) {
|
public TestWriter(final String name) {
|
||||||
super(name);
|
super(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
protected void tearDown() throws Exception {
|
protected void tearDown() throws Exception {
|
||||||
super.tearDown();
|
super.tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void whenWriteStringUsingBufferedWritter_thenCorrect() throws IOException {
|
public void whenWriteStringUsingBufferedWritter_thenCorrect() throws IOException {
|
||||||
String str = "Hello";
|
final String str = "Hello";
|
||||||
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3));
|
final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3));
|
||||||
writer.write(str);
|
writer.write(str);
|
||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo() throws IOException {
|
public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo() throws IOException {
|
||||||
String str = "World";
|
final String str = "World";
|
||||||
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3 , true));
|
final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3, true));
|
||||||
writer.append(' ');
|
writer.append(' ');
|
||||||
writer.append(str);
|
writer.append(str);
|
||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted() throws IOException {
|
public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted() throws IOException {
|
||||||
FileWriter fileWriter = new FileWriter(fileName);
|
final FileWriter fileWriter = new FileWriter(fileName);
|
||||||
PrintWriter printWriter = new PrintWriter(fileWriter);
|
final PrintWriter printWriter = new PrintWriter(fileWriter);
|
||||||
printWriter.printf("Product name is %s and its price is %d $", "iPhone", 1000);
|
printWriter.printf("Product name is %s and its price is %d $", "iPhone", 1000);
|
||||||
printWriter.close();
|
printWriter.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void whenWriteStringUsingFileOutputStream_thenCorrect() throws IOException {
|
public void whenWriteStringUsingFileOutputStream_thenCorrect() throws IOException {
|
||||||
String str = "Hello";
|
final String str = "Hello";
|
||||||
FileOutputStream outputStream = new FileOutputStream(fileName3);
|
final FileOutputStream outputStream = new FileOutputStream(fileName3);
|
||||||
byte[] strToBytes = str.getBytes();
|
final byte[] strToBytes = str.getBytes();
|
||||||
outputStream.write(strToBytes);
|
outputStream.write(strToBytes);
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame() throws IOException {
|
public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame() throws IOException {
|
||||||
String value = "Hello";
|
final String value = "Hello";
|
||||||
FileOutputStream fos = new FileOutputStream(fileName1);
|
final FileOutputStream fos = new FileOutputStream(fileName1);
|
||||||
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
|
final DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
|
||||||
outStream.writeUTF(value);
|
outStream.writeUTF(value);
|
||||||
outStream.close();
|
outStream.close();
|
||||||
|
|
||||||
String result;
|
String result;
|
||||||
FileInputStream fis = new FileInputStream(fileName1);
|
final FileInputStream fis = new FileInputStream(fileName1);
|
||||||
DataInputStream reader = new DataInputStream(fis);
|
final DataInputStream reader = new DataInputStream(fis);
|
||||||
result = reader.readUTF();
|
result = reader.readUTF();
|
||||||
reader.close();
|
reader.close();
|
||||||
|
|
||||||
@ -92,23 +93,22 @@ public class TestWriter extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void whenWriteToPositionAndEditIt_thenItShouldChange() {
|
public void whenWriteToPositionAndEditIt_thenItShouldChange() {
|
||||||
int data1 = 2014;
|
final int data1 = 2014;
|
||||||
int data2 = 1500;
|
final int data2 = 1500;
|
||||||
testWriteToPosition(data1, 4);
|
testWriteToPosition(data1, 4);
|
||||||
assertEquals(data1, testReadFromPosition(4));
|
assertEquals(data1, testReadFromPosition(4));
|
||||||
testWriteToPosition(data2, 4);
|
testWriteToPosition(data2, 4);
|
||||||
assertEquals(data2, testReadFromPosition(4));
|
assertEquals(data2, testReadFromPosition(4));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void whenTryToLockFile_thenItShouldBeLocked() throws IOException {
|
public void whenTryToLockFile_thenItShouldBeLocked() throws IOException {
|
||||||
RandomAccessFile stream = new RandomAccessFile(fileName4, "rw");
|
final RandomAccessFile stream = new RandomAccessFile(fileName4, "rw");
|
||||||
FileChannel channel = stream.getChannel();
|
final FileChannel channel = stream.getChannel();
|
||||||
|
|
||||||
FileLock lock = null;
|
FileLock lock = null;
|
||||||
try {
|
try {
|
||||||
lock = channel.tryLock();
|
lock = channel.tryLock();
|
||||||
} catch (OverlappingFileLockException e) {
|
} catch (final OverlappingFileLockException e) {
|
||||||
stream.close();
|
stream.close();
|
||||||
channel.close();
|
channel.close();
|
||||||
}
|
}
|
||||||
@ -119,80 +119,72 @@ public class TestWriter extends TestCase {
|
|||||||
channel.close();
|
channel.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException {
|
public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException {
|
||||||
RandomAccessFile stream = new RandomAccessFile(fileName5, "rw");
|
final RandomAccessFile stream = new RandomAccessFile(fileName5, "rw");
|
||||||
FileChannel channel = stream.getChannel();
|
final FileChannel channel = stream.getChannel();
|
||||||
String value = "Hello";
|
final String value = "Hello";
|
||||||
byte[] strBytes = value.getBytes();
|
final byte[] strBytes = value.getBytes();
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
|
final ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
|
||||||
buffer.put(strBytes);
|
buffer.put(strBytes);
|
||||||
buffer.flip();
|
buffer.flip();
|
||||||
channel.write(buffer);
|
channel.write(buffer);
|
||||||
stream.close();
|
stream.close();
|
||||||
channel.close();
|
channel.close();
|
||||||
|
|
||||||
RandomAccessFile reader = new RandomAccessFile(fileName5, "r");
|
final RandomAccessFile reader = new RandomAccessFile(fileName5, "r");
|
||||||
assertEquals(value, reader.readLine());
|
assertEquals(value, reader.readLine());
|
||||||
reader.close();
|
reader.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void whenWriteToTmpFile_thenCorrect() throws IOException {
|
public void whenWriteToTmpFile_thenCorrect() throws IOException {
|
||||||
String toWrite = "Hello";
|
final String toWrite = "Hello";
|
||||||
File tmpFile = File.createTempFile("test", ".tmp");
|
final File tmpFile = File.createTempFile("test", ".tmp");
|
||||||
FileWriter writer = new FileWriter(tmpFile);
|
final FileWriter writer = new FileWriter(tmpFile);
|
||||||
writer.write(toWrite);
|
writer.write(toWrite);
|
||||||
writer.close();
|
writer.close();
|
||||||
|
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(tmpFile));
|
final BufferedReader reader = new BufferedReader(new FileReader(tmpFile));
|
||||||
assertEquals(toWrite, reader.readLine());
|
assertEquals(toWrite, reader.readLine());
|
||||||
reader.close();
|
reader.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void whenWriteUsingJava7_thenCorrect() throws IOException {
|
public void whenWriteUsingJava7_thenCorrect() throws IOException {
|
||||||
String str = "Hello";
|
final String str = "Hello";
|
||||||
|
|
||||||
Path path = Paths.get(fileName3);
|
final Path path = Paths.get(fileName3);
|
||||||
byte[] strToBytes = str.getBytes();
|
final byte[] strToBytes = str.getBytes();
|
||||||
|
|
||||||
Files.write(path, strToBytes);
|
Files.write(path, strToBytes);
|
||||||
|
|
||||||
String read = Files.readAllLines(path).get(0);
|
final String read = Files.readAllLines(path).get(0);
|
||||||
assertEquals(str, read);
|
assertEquals(str, read);
|
||||||
}
|
}
|
||||||
|
|
||||||
// use RandomAccessFile to write data at specific position in the file
|
// use RandomAccessFile to write data at specific position in the file
|
||||||
public void testWriteToPosition(int data, long position) {
|
public void testWriteToPosition(final int data, final long position) {
|
||||||
try {
|
try {
|
||||||
RandomAccessFile writer = new RandomAccessFile(fileName2, "rw");
|
final RandomAccessFile writer = new RandomAccessFile(fileName2, "rw");
|
||||||
writer.seek(position);
|
writer.seek(position);
|
||||||
writer.writeInt(data);
|
writer.writeInt(data);
|
||||||
writer.close();
|
writer.close();
|
||||||
} catch (Exception e) {
|
} catch (final Exception e) {
|
||||||
System.out.println(e.getLocalizedMessage());
|
System.out.println(e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// use RandomAccessFile to read data from specific position in the file
|
// use RandomAccessFile to read data from specific position in the file
|
||||||
public int testReadFromPosition(long position) {
|
public int testReadFromPosition(final long position) {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
try {
|
try {
|
||||||
RandomAccessFile reader = new RandomAccessFile(fileName2, "r");
|
final RandomAccessFile reader = new RandomAccessFile(fileName2, "r");
|
||||||
reader.seek(position);
|
reader.seek(position);
|
||||||
result = reader.readInt();
|
result = reader.readInt();
|
||||||
reader.close();
|
reader.close();
|
||||||
} catch (Exception e) {
|
} catch (final Exception e) {
|
||||||
System.out.println(e.getLocalizedMessage());
|
System.out.println(e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user