Skip to content

Commit 6435d57

Browse files
Ahmed Tawilapivovarit
authored andcommitted
BAEL-1309 Append Data to a File with Java (eugenp#3054)
* Evaluation article: Different Types of Bean Injection in Spring * added tests & changed configuration to Java-based config * removed xml config files * rename unit tests * BAEL-972 - Apache Commons Text * remove code from evaluation article * remove code from evaluation article * BAEL-972 - Apache Commons Text - added another example * BAEL-972 - Apache Commons Text - just indentation * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java - fix problems * BAEL-1033 Introduction to StreamUtils * BAEL-1033 Introduction to StreamUtils * BAEL-1033 Introduction to StreamUtils * fix formatting * BAEL-1033 minor refactor * BAEL-1035 Introduction to Eclipse Collections * format * BAEL-1035 Introduction to Eclipse Collections * BAEL-1035 Introduction to Eclipse Collections * BAEL-1035 Introduction to Eclipse Collections * cleanup * cleanup * BAEL-1109 Introduction to JCache * BAEL-1109 Introduction to JCache * remove unneeded property in pom.xml * fix formatting * close cache instances properly * remove latest commit * BAEL-1057 Introduction to rxjava-jdbc * refactor rxjava-jdbc * Refactor rxjava-jdbc * Refactoring rxjava-jdbc * BAEL-1171 java.lang.String API * refactor rxjava-jdbc * refactor String * String API - move multiple classes into a single class * move class into test package * BAEL-1171 String.lang.String API * BAEL-1171 java.lang.String API * BAEL-1250 Initializing Arrays in Java * BAEL-1250 Initializing Arrays in Java * BAEL-1250 Initializing Arrays in Java * small fix * BAEL-1171 java.lang.String API * BAEL-1263 Daemon Threads in Java * merge with fork * BAEL-1263 Daemon Threads in Java * fix compilation error * BAEL-1309 Append Data to a File with Java * fix build fails * fix test error * fix build failures * cleanup * fix Sneaky Runnable test * revert sneaky runnable fix attempt * fix test failure * fix test failure * fix all test failures * fix test failure * fix test failures * Nested Classes in Java
1 parent c8d11d0 commit 6435d57

18 files changed

Lines changed: 344 additions & 2 deletions

File tree

‎core-java-concurrency/src/main/java/com/baeldung/concurrent/daemon/NewThread.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
public class NewThread extends Thread {
44

55
public void run() {
6-
76
long startTime = System.currentTimeMillis();
87
while (true) {
98
for (int i = 0; i < 10; i++) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.nestedclass;
2+
3+
public class Enclosing {
4+
5+
public static class Nested {
6+
7+
public void test() {
8+
System.out.println("Calling test...");
9+
}
10+
}
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.nestedclass;
2+
3+
public class NewEnclosing {
4+
5+
void run() {
6+
class Local {
7+
8+
void run() {
9+
System.out.println("Welcome to Baeldung!");
10+
}
11+
}
12+
Local local = new Local();
13+
local.run();
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.nestedclass;
2+
3+
public class Outer {
4+
5+
public class Inner {
6+
7+
public void test() {
8+
System.out.println("Calling test...");
9+
}
10+
}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.nestedclass;
2+
3+
abstract class SimpleAbstractClass {
4+
abstract void run();
5+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.util;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.StringWriter;
6+
7+
import org.apache.commons.io.IOUtils;
8+
9+
public class StreamUtils {
10+
11+
public static String getStringFromInputStream(InputStream input) throws IOException {
12+
StringWriter writer = new StringWriter();
13+
IOUtils.copy(input, writer, "UTF-8");
14+
return writer.toString();
15+
}
16+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
UK
2+
US
3+
Germany
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.file;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.FileInputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.io.PrintWriter;
9+
10+
import org.junit.After;
11+
import org.junit.Test;
12+
13+
import com.baeldung.util.StreamUtils;
14+
15+
public class FileOutputStreamTest {
16+
17+
public static final String fileName = "src/main/resources/countries.properties";
18+
19+
@Test
20+
public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception {
21+
FileOutputStream fos = new FileOutputStream(fileName, true);
22+
fos.write("Spain\r\n".getBytes());
23+
fos.close();
24+
25+
assertThat(StreamUtils.getStringFromInputStream(
26+
new FileInputStream(fileName)))
27+
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
28+
}
29+
30+
@After
31+
public void revertFile() throws IOException {
32+
PrintWriter writer = new PrintWriter(fileName);
33+
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
34+
writer.close();
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.file;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.IOException;
8+
import java.io.PrintWriter;
9+
import java.nio.charset.StandardCharsets;
10+
11+
import org.apache.commons.io.FileUtils;
12+
import org.junit.After;
13+
import org.junit.Test;
14+
15+
import com.baeldung.util.StreamUtils;
16+
17+
public class FileUtilsTest {
18+
19+
public static final String fileName = "src/main/resources/countries.properties";
20+
21+
@Test
22+
public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
23+
File file = new File(fileName);
24+
FileUtils.writeStringToFile(file, "Spain\r\n", StandardCharsets.UTF_8, true);
25+
26+
assertThat(StreamUtils.getStringFromInputStream(
27+
new FileInputStream(fileName)))
28+
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
29+
}
30+
31+
@After
32+
public void revertFile() throws IOException {
33+
PrintWriter writer = new PrintWriter(fileName);
34+
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
35+
writer.close();
36+
}
37+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.file;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.BufferedWriter;
6+
import java.io.FileInputStream;
7+
import java.io.FileWriter;
8+
import java.io.IOException;
9+
import java.io.PrintWriter;
10+
11+
import org.junit.After;
12+
import org.junit.Test;
13+
14+
import com.baeldung.util.StreamUtils;
15+
16+
public class FileWriterTest {
17+
18+
public static final String fileName = "src/main/resources/countries.properties";
19+
20+
@Test
21+
public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
22+
FileWriter fw = new FileWriter(fileName, true);
23+
BufferedWriter bw = new BufferedWriter(fw);
24+
bw.write("Spain");
25+
bw.newLine();
26+
bw.close();
27+
28+
assertThat(
29+
StreamUtils.getStringFromInputStream(
30+
new FileInputStream(fileName)))
31+
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n");
32+
}
33+
34+
@After
35+
public void revertFile() throws IOException {
36+
PrintWriter writer = new PrintWriter(fileName);
37+
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
38+
writer.close();
39+
}
40+
}

0 commit comments

Comments
 (0)