From e6e0c065d509357f6a04bef853ad0f52c4c9e420 Mon Sep 17 00:00:00 2001 From: Rajesh Bhojwani Date: Thu, 17 Jan 2019 15:17:09 +0530 Subject: [PATCH 1/4] Added test file to demo the deletion of file content --- .../file/FilesClearDataManualTest.java | 98 +++++++++++++++++++ .../src/test/resources/fileexample.txt | 1 + 2 files changed, 99 insertions(+) create mode 100644 core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java create mode 100644 core-java-io/src/test/resources/fileexample.txt diff --git a/core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java b/core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java new file mode 100644 index 0000000000..8a4b3a7380 --- /dev/null +++ b/core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java @@ -0,0 +1,98 @@ +package com.baeldung.file; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.channels.FileChannel; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; + +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.util.StreamUtils; + +public class FilesClearDataManualTest { + + public static final String FILE_PATH = "src/test/resources/fileexample.txt"; + + @Before + @After + public void setup() throws IOException { + PrintWriter writer = new PrintWriter(FILE_PATH); + writer.print("This example shows how we can delete the file contents without deleting the file"); + writer.close(); + } + + @Test + public void givenExistingFile_whenDeleteContentUsingPrintWritter_thenEmptyFile() throws IOException { + PrintWriter writer = new PrintWriter(FILE_PATH); + writer.print(""); + writer.close(); + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); + } + + @Test + public void givenExistingFile_whenDeleteContentUsingPrintWritterWithougObject_thenEmptyFile() throws IOException { + new PrintWriter(FILE_PATH).close(); + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); + } + + + @Test + public void givenExistingFile_whenDeleteContentUsingFileWriter_thenEmptyFile() throws IOException { + new FileWriter(FILE_PATH, false).close(); + + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); + } + + @Test + public void givenExistingFile_whenDeleteContentUsingFileOutputStream_thenEmptyFile() throws IOException { + new FileOutputStream(FILE_PATH).close(); + + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); + } + + @Test + public void givenExistingFile_whenDeleteContentUsingFileUtils_thenEmptyFile() throws IOException { + FileUtils.write(new File(FILE_PATH), "", Charset.defaultCharset()); + + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); + } + + @Test + public void givenExistingFile_whenDeleteContentUsingNIOFiles_thenEmptyFile() throws IOException { + BufferedWriter writer = Files.newBufferedWriter(Paths.get(FILE_PATH)); + writer.write(""); + writer.flush(); + + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); + } + + @Test + public void givenExistingFile_whenDeleteContentUsingNIOFileChannel_thenEmptyFile() throws IOException { + FileChannel.open(Paths.get(FILE_PATH), StandardOpenOption.WRITE).truncate(0).close(); + + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); + } + + @Test + public void givenExistingFile_whenDeleteContentUsingGuava_thenEmptyFile() throws IOException { + File file = new File(FILE_PATH); + byte[] empty = new byte[0]; + com.google.common.io.Files.write(empty, file); + + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); + } + +} diff --git a/core-java-io/src/test/resources/fileexample.txt b/core-java-io/src/test/resources/fileexample.txt new file mode 100644 index 0000000000..ee48fdfb84 --- /dev/null +++ b/core-java-io/src/test/resources/fileexample.txt @@ -0,0 +1 @@ +This example shows how we can delete the file contents without deleting the file \ No newline at end of file From 273c569b88de74d048a1b7989021c686c9764dbf Mon Sep 17 00:00:00 2001 From: Rajesh Bhojwani Date: Thu, 17 Jan 2019 15:20:25 +0530 Subject: [PATCH 2/4] remove space --- .../test/java/com/baeldung/file/FilesClearDataManualTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java b/core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java index 8a4b3a7380..c00290168c 100644 --- a/core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java +++ b/core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java @@ -47,7 +47,6 @@ public class FilesClearDataManualTest { new PrintWriter(FILE_PATH).close(); assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); } - @Test public void givenExistingFile_whenDeleteContentUsingFileWriter_thenEmptyFile() throws IOException { From 8510b07a5642e261eceb76711c3739e4379d359b Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Thu, 17 Jan 2019 23:27:50 +0200 Subject: [PATCH 3/4] Update FilesClearDataManualTest.java --- .../test/java/com/baeldung/file/FilesClearDataManualTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java b/core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java index c00290168c..6abe677acf 100644 --- a/core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java +++ b/core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java @@ -93,5 +93,4 @@ public class FilesClearDataManualTest { assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); } - } From 08b8c427bd1784f0dc0de4f43f7638a0aabcdfc3 Mon Sep 17 00:00:00 2001 From: Rajesh Bhojwani Date: Mon, 21 Jan 2019 08:15:38 +0530 Subject: [PATCH 4/4] rename file --- ...ilesClearDataManualTest.java => FilesClearDataUnitTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename core-java-io/src/test/java/com/baeldung/file/{FilesClearDataManualTest.java => FilesClearDataUnitTest.java} (98%) diff --git a/core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java b/core-java-io/src/test/java/com/baeldung/file/FilesClearDataUnitTest.java similarity index 98% rename from core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java rename to core-java-io/src/test/java/com/baeldung/file/FilesClearDataUnitTest.java index c00290168c..653c4320c0 100644 --- a/core-java-io/src/test/java/com/baeldung/file/FilesClearDataManualTest.java +++ b/core-java-io/src/test/java/com/baeldung/file/FilesClearDataUnitTest.java @@ -22,7 +22,7 @@ import org.junit.Test; import com.baeldung.util.StreamUtils; -public class FilesClearDataManualTest { +public class FilesClearDataUnitTest { public static final String FILE_PATH = "src/test/resources/fileexample.txt";