BAEL-3214 | Java FileWriter class code - 2

This commit is contained in:
Nikunj Gandhi 2019-09-15 13:17:48 -04:00
parent 827c97e47b
commit 31be377141
3 changed files with 94 additions and 122 deletions

View File

@ -0,0 +1,37 @@
package com.baeldung.filewriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
public class FileWriterExample {
public void writeString(String fileName, String strToWrite) throws IOException {
try (FileWriter fileWriter = new FileWriter(fileName)) {
fileWriter.write(strToWrite);
}
}
public void appendString(String fileName, String stringToAppend) throws IOException {
try (FileWriter fileWriter = new FileWriter(fileName, true)) {
fileWriter.append(stringToAppend);
}
}
public void writeCharArray(String fileName, char[] charArrToWrite) throws IOException {
try (FileWriter fileWriter = new FileWriter(fileName)) {
fileWriter.write(charArrToWrite);
}
}
public void writeWithBufferedWriter(String fileName, List<String> stringsToWrite) throws IOException {
try (FileWriter fileWriter = new FileWriter(fileName); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
for (String stringToWrite : stringsToWrite) {
bufferedWriter.write(stringToWrite);
}
}
}
}

View File

@ -1,122 +0,0 @@
package com.baeldung.filewriter;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.After;
import org.junit.Test;
public class FileWriterExampleTest {
@After
public void tearDown() throws IOException {
Files.delete(Path.of("src/test/resources/FileWriterTest.txt"));
}
@Test
public void testFileWriter_Constructor_With_FileName() throws IOException {
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt")) {
fileWriter.write("Hello Folks!");
}
assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
@Test
public void testFileWriter_Constructor_With_FileName_And_Charset() throws IOException {
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt", Charset.forName("US-ASCII"))) {
fileWriter.write("Hello Folks! Let us learn the Façade design pattern");
}
System.out.println(new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
assertEquals("Hello Folks! Let us learn the Fa?ade design pattern", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
@Test
public void testFileWriter_Constructor_With_FileName_And_AppendFlag() throws IOException {
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt")) {
fileWriter.write("Hello Folks!");
}
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt", true)) {
fileWriter.write("Let us learn the Façade design pattern");
}
assertEquals("Hello Folks!Let us learn the Façade design pattern", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
@Test
public void testFileWriter_Constructor_With_FileName_And_Charset_And_AppendFlag() throws IOException {
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt", Charset.forName("US-ASCII"))) {
fileWriter.write("Hello Folks!");
}
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt", Charset.forName("US-ASCII"), true)) {
fileWriter.write("Let us learn the Façade design pattern");
}
assertEquals("Hello Folks!Let us learn the Fa?ade design pattern", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
@Test
public void testFileWriter_Constructor_With_File() throws IOException {
try (FileWriter fileWriter = new FileWriter(new File("src/test/resources/FileWriterTest.txt"))) {
fileWriter.write("Hello Folks!");
}
assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
@Test
public void testFileWriter_Constructor_With_File_And_Charset() throws IOException {
try (FileWriter fileWriter = new FileWriter(new File("src/test/resources/FileWriterTest.txt"), Charset.forName("US-ASCII"))) {
fileWriter.write("Hello Folks!Let us learn the Façade design pattern");
}
assertEquals("Hello Folks!Let us learn the Fa?ade design pattern", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
@Test
public void testFileWriter_Constructor_With_File_And_AppendFlag() throws IOException {
try (FileWriter fileWriter = new FileWriter(new File("src/test/resources/FileWriterTest.txt"))) {
fileWriter.write("Hello Folks!");
}
try (FileWriter fileWriter = new FileWriter(new File("src/test/resources/FileWriterTest.txt"), true)) {
fileWriter.write("Let us learn the Façade design pattern");
}
assertEquals("Hello Folks!Let us learn the Façade design pattern", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
@Test
public void testFileWriter_Constructor_With_File_And_Charset_And_AppendFlag() throws IOException {
try (FileWriter fileWriter = new FileWriter(new File("src/test/resources/FileWriterTest.txt"), Charset.forName("US-ASCII"))) {
fileWriter.write("Hello Folks!");
}
try (FileWriter fileWriter = new FileWriter(new File("src/test/resources/FileWriterTest.txt"), Charset.forName("US-ASCII"), true)) {
fileWriter.write("Let us learn the Façade design pattern");
}
assertEquals("Hello Folks!Let us learn the Fa?ade design pattern", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
@Test
public void testFileWriter_Constructor_With_FileDescriptor() throws FileNotFoundException, IOException {
try (FileOutputStream fos = new FileOutputStream(new File("src/test/resources/FileWriterTest.txt")); FileWriter fileWriter = new FileWriter(fos.getFD())) {
fileWriter.write("Hello Folks!");
}
assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt"))));
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.filewriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
public class FileWriterExampleUnitTest {
private static final String FILE_NAME = "src/test/resources/FileWriterTest.txt";
private static final String STRING_TO_WRITE = "Hello Folks!";
private static final String STRING_TO_APPEND = "Hello Folks Again!";
private static final char[] CHAR_ARRAY_TO_WRITE = STRING_TO_WRITE.toCharArray();
private FileWriterExample fileWriterExample = new FileWriterExample();
@After
public void tearDown() throws IOException {
Files.delete(Path.of(FILE_NAME));
}
@Test
public void testWriteString() throws IOException {
fileWriterExample.writeString(FILE_NAME, STRING_TO_WRITE);
Assert.assertEquals(STRING_TO_WRITE, new String(Files.readAllBytes(Path.of(FILE_NAME))));
}
@Test
public void testAppendString() throws IOException {
fileWriterExample.writeString(FILE_NAME, STRING_TO_WRITE);
fileWriterExample.appendString(FILE_NAME, STRING_TO_APPEND);
Assert.assertEquals(STRING_TO_WRITE + STRING_TO_APPEND, new String(Files.readAllBytes(Path.of(FILE_NAME))));
}
@Test
public void testWriteCharArray() throws IOException {
fileWriterExample.writeCharArray(FILE_NAME, CHAR_ARRAY_TO_WRITE);
Assert.assertEquals(STRING_TO_WRITE, new String(Files.readAllBytes(Path.of(FILE_NAME))));
}
@Test
public void testWriteWithBufferedWriter() throws IOException{
final List<String> stringsToWrite = new ArrayList<>();
for(int i=0 ; i < 10000;i++) {
stringsToWrite.add("Random string "+i);
}
fileWriterExample.writeWithBufferedWriter(FILE_NAME, stringsToWrite);
Assert.assertNotNull(new String(Files.readAllBytes(Path.of(FILE_NAME))));
}
}