|
| 1 | +package com.baeldung.file; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.io.BufferedWriter; |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.io.FileOutputStream; |
| 9 | +import java.io.FileWriter; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.PrintWriter; |
| 12 | +import java.nio.channels.FileChannel; |
| 13 | +import java.nio.charset.Charset; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Paths; |
| 16 | +import java.nio.file.StandardOpenOption; |
| 17 | + |
| 18 | +import org.apache.commons.io.FileUtils; |
| 19 | +import org.junit.After; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import com.baeldung.util.StreamUtils; |
| 24 | + |
| 25 | +public class FilesClearDataUnitTest { |
| 26 | + |
| 27 | + public static final String FILE_PATH = "src/test/resources/fileexample.txt"; |
| 28 | + |
| 29 | + @Before |
| 30 | + @After |
| 31 | + public void setup() throws IOException { |
| 32 | + PrintWriter writer = new PrintWriter(FILE_PATH); |
| 33 | + writer.print("This example shows how we can delete the file contents without deleting the file"); |
| 34 | + writer.close(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void givenExistingFile_whenDeleteContentUsingPrintWritter_thenEmptyFile() throws IOException { |
| 39 | + PrintWriter writer = new PrintWriter(FILE_PATH); |
| 40 | + writer.print(""); |
| 41 | + writer.close(); |
| 42 | + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void givenExistingFile_whenDeleteContentUsingPrintWritterWithougObject_thenEmptyFile() throws IOException { |
| 47 | + new PrintWriter(FILE_PATH).close(); |
| 48 | + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void givenExistingFile_whenDeleteContentUsingFileWriter_thenEmptyFile() throws IOException { |
| 53 | + new FileWriter(FILE_PATH, false).close(); |
| 54 | + |
| 55 | + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void givenExistingFile_whenDeleteContentUsingFileOutputStream_thenEmptyFile() throws IOException { |
| 60 | + new FileOutputStream(FILE_PATH).close(); |
| 61 | + |
| 62 | + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void givenExistingFile_whenDeleteContentUsingFileUtils_thenEmptyFile() throws IOException { |
| 67 | + FileUtils.write(new File(FILE_PATH), "", Charset.defaultCharset()); |
| 68 | + |
| 69 | + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void givenExistingFile_whenDeleteContentUsingNIOFiles_thenEmptyFile() throws IOException { |
| 74 | + BufferedWriter writer = Files.newBufferedWriter(Paths.get(FILE_PATH)); |
| 75 | + writer.write(""); |
| 76 | + writer.flush(); |
| 77 | + |
| 78 | + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void givenExistingFile_whenDeleteContentUsingNIOFileChannel_thenEmptyFile() throws IOException { |
| 83 | + FileChannel.open(Paths.get(FILE_PATH), StandardOpenOption.WRITE).truncate(0).close(); |
| 84 | + |
| 85 | + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void givenExistingFile_whenDeleteContentUsingGuava_thenEmptyFile() throws IOException { |
| 90 | + File file = new File(FILE_PATH); |
| 91 | + byte[] empty = new byte[0]; |
| 92 | + com.google.common.io.Files.write(empty, file); |
| 93 | + |
| 94 | + assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length()); |
| 95 | + } |
| 96 | +} |
0 commit comments