BAEL-3214 | Code review related changes - 1
This commit is contained in:
parent
31be377141
commit
4e063a1120
@ -1,37 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,12 +1,9 @@
|
|||||||
package com.baeldung.filewriter;
|
package com.baeldung.filewriter;
|
||||||
|
|
||||||
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
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.After;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
@ -15,11 +12,6 @@ import org.junit.Test;
|
|||||||
public class FileWriterExampleUnitTest {
|
public class FileWriterExampleUnitTest {
|
||||||
|
|
||||||
private static final String FILE_NAME = "src/test/resources/FileWriterTest.txt";
|
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
|
@After
|
||||||
public void tearDown() throws IOException {
|
public void tearDown() throws IOException {
|
||||||
@ -28,30 +20,30 @@ public class FileWriterExampleUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWriteString() throws IOException {
|
public void testWriteString() throws IOException {
|
||||||
fileWriterExample.writeString(FILE_NAME, STRING_TO_WRITE);
|
try (FileWriter fileWriter = new FileWriter(FILE_NAME)) {
|
||||||
Assert.assertEquals(STRING_TO_WRITE, new String(Files.readAllBytes(Path.of(FILE_NAME))));
|
fileWriter.write("Hello Folks!");
|
||||||
|
}
|
||||||
|
Assert.assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of(FILE_NAME))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAppendString() throws IOException {
|
public void testAppendString() throws IOException {
|
||||||
fileWriterExample.writeString(FILE_NAME, STRING_TO_WRITE);
|
try (FileWriter fileWriter = new FileWriter(FILE_NAME)) {
|
||||||
fileWriterExample.appendString(FILE_NAME, STRING_TO_APPEND);
|
fileWriter.write("Hello Folks!");
|
||||||
Assert.assertEquals(STRING_TO_WRITE + STRING_TO_APPEND, new String(Files.readAllBytes(Path.of(FILE_NAME))));
|
}
|
||||||
|
// using another try with resources to reopen the file in append mode
|
||||||
|
try (FileWriter fileWriter = new FileWriter(FILE_NAME, true)) {
|
||||||
|
fileWriter.write("Hello Folks Again!");
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertEquals("Hello Folks!" + "Hello Folks Again!", new String(Files.readAllBytes(Path.of(FILE_NAME))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWriteCharArray() throws IOException {
|
public void testWriteCharArray() throws IOException {
|
||||||
fileWriterExample.writeCharArray(FILE_NAME, CHAR_ARRAY_TO_WRITE);
|
try (FileWriter fileWriter = new FileWriter(FILE_NAME)) {
|
||||||
Assert.assertEquals(STRING_TO_WRITE, new String(Files.readAllBytes(Path.of(FILE_NAME))));
|
fileWriter.write("Hello Folks!".toCharArray());
|
||||||
}
|
|
||||||
|
|
||||||
@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.assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of(FILE_NAME))));
|
||||||
Assert.assertNotNull(new String(Files.readAllBytes(Path.of(FILE_NAME))));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user