BAEL-5767: Writing an ArrayList of Strings into a Text File (#12783)

* Deep vs Shallow copy of an object in java

* update indentaions

* Create a BMI Caclualtor in Java

* Create a BMI Caclualtor in Java

* Delete unused packages

* BAEL-5708: Create a BMI Calculator in Java

* BAEL-5708: Create a BMI Calculator in Java

* BAEL-5767:Writing an ArrayList of Strings into a Text File

* BAEL-5767:Writing an ArrayList of Strings into a Text File

* Deleting files from core-java-io

* BAEL-5767: Writing an ArrayList of Strings into a Text File

* Removed files from core-java-11-2

* BAEL-5767:Writing an ArrayList of Strings into a Text File

* BAEL-5767:Writing an ArrayList of Strings into a Text File

* BAEL-5767:Writing an ArrayList of Strings into a Text File
This commit is contained in:
Mahn00rMalik 2022-10-04 17:03:59 +05:00 committed by GitHub
parent e16182f325
commit 790e53837d
10 changed files with 138 additions and 3 deletions

View File

@ -0,0 +1,19 @@
package com.baeldung.filewriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
public class BufferedWriterExample {
public static String generateFileFromStringList(List<String> stringList) throws IOException {
final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt";
BufferedWriter br = new BufferedWriter(new FileWriter(TEXT_FILENAME));
for (String str : stringList) {
br.write(str + System.lineSeparator());
}
br.close();
return TEXT_FILENAME;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.filewriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
public class FileWriteStringExample {
public static String generateFileFromStringList(List<String> stringList) throws IOException {
final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt";
Path filePath = Paths.get(TEXT_FILENAME);
Files.deleteIfExists(filePath);
Files.createFile(filePath);
for (String str : stringList) {
Files.writeString(filePath, str + System.lineSeparator(),
StandardOpenOption.APPEND);
}
return filePath.toString();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.filewriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
public class FileWriterExample {
public static String generateFileFromStringList(List<String> stringList) throws IOException {
final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt";
FileWriter fileWriter = new FileWriter(TEXT_FILENAME);
for (String str : stringList) {
fileWriter.write(str + System.lineSeparator());
}
fileWriter.close();
return TEXT_FILENAME;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.filewriter;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
public class BufferedWriterUnitTest {
private static final List<String> stringList = Arrays.asList("Hello", "World");
@Test
public void givenUsingBufferedWriter_whenStringList_thenGetTextFile() throws IOException {
String fileName = BufferedWriterExample.generateFileFromStringList(stringList);
long count = Files.lines(Paths.get(fileName)).count();
assertTrue("No. of lines in file should be equal to no. of Strings in List", ((int) count) == stringList.size());
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.filewriter;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertTrue;
public class FileWriteStringUnitTest {
private static final List<String> stringList = Arrays.asList("Hello", "World");
@Test
public void givenUsingFileWriteString_whenStringList_thenGetTextFile() throws IOException {
String fileName = FileWriteStringExample.generateFileFromStringList(stringList);
long count = Files.lines(Paths.get(fileName)).count();
assertTrue("No. of lines in file should be equal to no. of Strings in List", ((int) count) == stringList.size());
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.filewriter;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertTrue;
public class FileWriterUnitTest {
private static final List<String> stringList = Arrays.asList("Hello", "World");
@Test
public void givenUsingFileWriter_whenStringList_thenGetTextFile() throws IOException {
String fileName = FileWriterExample.generateFileFromStringList(stringList);
long count = Files.lines(Paths.get(fileName)).count();
assertTrue("No. of lines in file should be equal to no. of Strings in List", ((int) count) == stringList.size());
}
}

View File

@ -0,0 +1,2 @@
Hello
World

View File

@ -38,6 +38,12 @@
<artifactId>fscontext</artifactId>
<version>${fscontext.version}</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -1,2 +1,2 @@
Hello
World
Hello
World