BAEL-1309 Append Data to a File with Java

This commit is contained in:
Ahmed Tawila 2017-11-16 06:44:34 +02:00
parent 3cc88518bd
commit 960d8e004c
6 changed files with 211 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package com.baeldung.file;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import org.junit.After;
import org.junit.Test;
import com.baeldung.util.StreamUtils;
public class FileOutputStreamTest {
public static final String fileName = "src/main/resources/countries.txt";
@Test
public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception {
FileOutputStream fos = new FileOutputStream(fileName, true);
fos.write("Spain\r\n".getBytes());
fos.close();
assertThat(StreamUtils.getStringFromInputStream(
new FileInputStream(fileName)))
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
@After
public void revertFile() throws IOException {
PrintWriter writer = new PrintWriter(fileName);
writer.print("");
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
writer.close();
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.file;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Test;
import com.baeldung.util.StreamUtils;
public class FileUtilsTest {
public static final String fileName = "src/main/resources/countries.txt";
@Test
public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
File file = new File(fileName);
FileUtils.writeStringToFile(file, "Spain\r\n", StandardCharsets.UTF_8, true);
assertThat(StreamUtils.getStringFromInputStream(
new FileInputStream(fileName)))
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
@After
public void revertFile() throws IOException {
PrintWriter writer = new PrintWriter(fileName);
writer.print("");
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
writer.close();
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.file;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.junit.After;
import org.junit.Test;
import com.baeldung.util.StreamUtils;
public class FileWriterTest {
public static final String fileName = "src/main/resources/countries.txt";
@Test
public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
String fileName = "src/main/resources/countries.txt";
FileWriter fw = new FileWriter(fileName, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Spain");
bw.newLine();
bw.close();
assertThat(
StreamUtils.getStringFromInputStream(
new FileInputStream(fileName)))
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
@After
public void revertFile() throws IOException {
PrintWriter writer = new PrintWriter(fileName);
writer.print("");
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
writer.close();
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.file;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import org.junit.After;
import org.junit.Test;
import com.baeldung.util.StreamUtils;
public class FilesTest {
public static final String fileName = "src/main/resources/countries.txt";
@Test
public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
Files.write(Paths.get(fileName), "Spain\r\n".getBytes(), StandardOpenOption.APPEND);
assertThat(StreamUtils.getStringFromInputStream(
new FileInputStream(fileName)))
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
@After
public void revertFile() throws IOException {
PrintWriter writer = new PrintWriter(fileName);
writer.print("");
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
writer.close();
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.file;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import org.junit.After;
import org.junit.Test;
import com.baeldung.util.StreamUtils;
import com.google.common.base.Charsets;
import com.google.common.io.CharSink;
import com.google.common.io.FileWriteMode;
import com.google.common.io.Files;
public class GuavaTest {
public static final String fileName = "src/main/resources/countries.txt";
@Test
public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
File file = new File(fileName);
CharSink chs = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
chs.write("Spain\r\n");
assertThat(StreamUtils.getStringFromInputStream(
new FileInputStream(fileName)))
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
@After
public void revertFile() throws IOException {
PrintWriter writer = new PrintWriter(fileName);
writer.print("");
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
writer.close();
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import org.apache.commons.io.IOUtils;
public class StreamUtils {
public static String getStringFromInputStream(InputStream input) throws IOException {
StringWriter writer = new StringWriter();
IOUtils.copy(input, writer, "UTF-8");
return writer.toString();
}
}