diff --git a/core-java/src/test/java/org/baeldung/java/io/TestWriter.java b/core-java/src/test/java/org/baeldung/java/io/TestWriter.java index 0dfa854dc9..7605856b34 100644 --- a/core-java/src/test/java/org/baeldung/java/io/TestWriter.java +++ b/core-java/src/test/java/org/baeldung/java/io/TestWriter.java @@ -1,30 +1,32 @@ package test; -import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; +import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.io.PrintWriter; import java.io.RandomAccessFile; -import java.util.List; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.channels.FileLock; -import au.com.bytecode.opencsv.CSVReader; -import au.com.bytecode.opencsv.CSVWriter; import main.Foo; import junit.framework.TestCase; public class TestWriter extends TestCase { + private String fileName = "test.txt"; private String fileName1 = "test1.txt"; private String fileName2 = "test2.txt"; private String fileName3 = "test3.txt"; - private String fileName4 = "test4.csv"; + private String fileName4 = "test4.txt"; + private String fileName5 = "test5.txt"; public TestWriter(String name) { super(name); @@ -37,11 +39,28 @@ public class TestWriter extends TestCase { protected void tearDown() throws Exception { super.tearDown(); } + + public void testWriteFormattedStringUsingPrintWriter() throws IOException{ + FileWriter fileWriter = new FileWriter(fileName); + PrintWriter printWriter = new PrintWriter(fileWriter); + printWriter.printf("Product name is %s and its price is %d $","iPhone",1000); + printWriter.close(); + } - public void testWriteDouble_thenReadIt_shouldBeTheSame() { - double value = 2.5; - testWriteDouble(value); - assertEquals(value, testReadDouble(), 0.0000001); + public void testWriteStringWithDataOutputStream_thenReadIt_shouldBeTheSame() throws IOException { + String value = "Hello"; + FileOutputStream fos = new FileOutputStream(fileName1); + DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos)); + outStream.writeUTF(value); + outStream.close(); + + String result; + FileInputStream fis = new FileInputStream(fileName1); + DataInputStream reader = new DataInputStream(fis); + result = reader.readUTF(); + reader.close(); + + assertEquals(value, result); } public void testWriteToPosition_editValueIfSpecificPos_shouldChange() { @@ -53,39 +72,62 @@ public class TestWriter extends TestCase { assertEquals(data2, testReadFromPosition(4)); } - public void testWriteObject_thenReadIt_instanceVariableValuesShouldBeTheSame() { + public void testWriteObject_thenReadIt_instanceVariableValuesShouldBeTheSame() throws Exception { Foo foo = new Foo(1, "John"); - testWriteObject(foo); - Foo read = testReadObject(); - assertEquals(foo.getId(), read.getId()); - assertEquals(foo.getName(), read.getName()); + FileOutputStream fos = new FileOutputStream(fileName3); + ObjectOutputStream writer = new ObjectOutputStream(fos); + writer.writeObject(foo); + writer.close(); + Foo result = null; + + FileInputStream fis = new FileInputStream(fileName3); + ObjectInputStream reader = new ObjectInputStream(fis); + result = (Foo) reader.readObject(); + reader.close(); + + assertEquals(foo.getId(), result.getId()); + assertEquals(foo.getName(), result.getName()); } - public void testWriteCSVReport_thenReadAllData_shouldBeTheSame() { - Foo[] arr = new Foo[3]; - arr[0] = new Foo(1, "John"); - arr[1] = new Foo(2, "Adam"); - arr[2] = new Foo(3, "Jane"); - testWriteCSVReport(arr); - Foo[] read = testReadCSVReport(); - assertEquals(arr[0].getId(), read[0].getId()); - assertEquals(arr[2].getName(), read[2].getName()); - assertEquals(arr[1].getId(), read[1].getId()); - } -//================= writer methods======== - // use DataOutputStream to write primitive data types - public void testWriteDouble(double value) { - DataOutputStream stream; - try { - FileOutputStream fos = new FileOutputStream(fileName1); - stream = new DataOutputStream(new BufferedOutputStream(fos)); - stream.writeDouble(value); - stream.close(); - } catch (Exception e) { - System.out.println(e.getLocalizedMessage()); - } + public void testFileLock() throws IOException { + RandomAccessFile stream = new RandomAccessFile(fileName4, "rw"); + FileChannel channel = stream.getChannel(); + + FileLock lock = channel.tryLock(); + stream.writeChars("test lock"); + lock.release(); + + stream.close(); + channel.close(); } + + public void testCreateFile_thenCheckIfItExists_thenDeleteAndCheckIfExist() throws IOException{ + File file = new File("test_create.txt"); + file.createNewFile(); + assertTrue(file.exists()); + file.delete(); + assertFalse(file.exists()); + } + + public void testWriteStringUsingFileChannel() throws IOException{ + RandomAccessFile stream = new RandomAccessFile(fileName5, "rw"); + FileChannel channel = stream.getChannel(); + String value = "Hello"; + byte[] strBytes = value.getBytes(); + ByteBuffer buffer = ByteBuffer.allocate(strBytes.length); + buffer.put(strBytes); + buffer.flip(); + channel.write(buffer); + stream.close(); + channel.close(); + + RandomAccessFile reader = new RandomAccessFile(fileName5, "r"); + assertEquals(value , reader.readLine()); + reader.close(); + + } + // use RandomAccessFile to write data at specific position in the file public void testWriteToPosition(int data, long position) { try { @@ -98,50 +140,7 @@ public class TestWriter extends TestCase { } } - // use ObjectOutputStream to write object - public void testWriteObject(Foo foo) { - FileOutputStream fos; - try { - fos = new FileOutputStream(fileName3); - ObjectOutputStream writer = new ObjectOutputStream(fos); - writer.writeObject(foo); - writer.close(); - } catch (Exception e) { - System.out.println(e.getLocalizedMessage()); - } - } - - // use CSVWriter to write CSV data - public void testWriteCSVReport(Foo[] array) { - try { - CSVWriter writer = new CSVWriter(new FileWriter(fileName4, true), ','); - int len = array.length; - for (int i = 0; i < len; i++) { - writer.writeNext(array[i].toStringArray()); - } - writer.close(); - } catch (IOException e) { - System.out.println(e.getLocalizedMessage()); - } - } - - - //=============== read methods ====== - // use DataInputStream - public double testReadDouble() { - DataInputStream stream; - double result = 0; - try { - FileInputStream fis = new FileInputStream(fileName1); - stream = new DataInputStream(new BufferedInputStream(fis)); - result = stream.readDouble(); - stream.close(); - } catch (Exception e) { - System.out.println(e.getLocalizedMessage()); - } - return result; - } // use RandomAccessFile to read data from specific position in the file public int testReadFromPosition(long position) { @@ -157,46 +156,8 @@ public class TestWriter extends TestCase { return result; } - // use ObjectInputStream to read object - public Foo testReadObject() { - FileInputStream fis; - Foo result = null; - try { - fis = new FileInputStream(fileName3); - ObjectInputStream reader = new ObjectInputStream(fis); - result = (Foo) reader.readObject(); - reader.close(); - } catch (Exception e) { - System.out.println(e.getLocalizedMessage()); - } - return result; - } - - // use CSVReader to read CSV data - public Foo[] testReadCSVReport() { - CSVReader reader; - Foo[] result = null; - try { - reader = new CSVReader(new FileReader(fileName4)); - List list = reader.readAll(); - int size = list.size(); - result = new Foo[size]; - Foo temp; - String[] currentItem; - for (int i = 0; i < size; i++) { - temp = new Foo(); - currentItem = (String[]) list.get(i); - temp.setId(Long.parseLong(currentItem[0])); - temp.setName(currentItem[1]); - result[i] = temp; - } - reader.close(); - } catch (Exception e) { - System.out.println(e.getLocalizedMessage()); - } - return result; - } - + + }