BAEL-1309 Append Data to a File with Java (#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
This commit is contained in:
parent
c8d11d0b81
commit
6435d5723a
|
@ -3,7 +3,6 @@ package com.baeldung.concurrent.daemon;
|
||||||
public class NewThread extends Thread {
|
public class NewThread extends Thread {
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
while (true) {
|
while (true) {
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
public class Enclosing {
|
||||||
|
|
||||||
|
public static class Nested {
|
||||||
|
|
||||||
|
public void test() {
|
||||||
|
System.out.println("Calling test...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
public class NewEnclosing {
|
||||||
|
|
||||||
|
void run() {
|
||||||
|
class Local {
|
||||||
|
|
||||||
|
void run() {
|
||||||
|
System.out.println("Welcome to Baeldung!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Local local = new Local();
|
||||||
|
local.run();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
public class Outer {
|
||||||
|
|
||||||
|
public class Inner {
|
||||||
|
|
||||||
|
public void test() {
|
||||||
|
System.out.println("Calling test...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
abstract class SimpleAbstractClass {
|
||||||
|
abstract void run();
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
UK
|
||||||
|
US
|
||||||
|
Germany
|
|
@ -0,0 +1,36 @@
|
||||||
|
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.properties";
|
||||||
|
|
||||||
|
@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("UK\r\n" + "US\r\n" + "Germany\r\n");
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
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.properties";
|
||||||
|
|
||||||
|
@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("UK\r\n" + "US\r\n" + "Germany\r\n");
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
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.properties";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
|
||||||
|
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\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void revertFile() throws IOException {
|
||||||
|
PrintWriter writer = new PrintWriter(fileName);
|
||||||
|
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
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.properties";
|
||||||
|
|
||||||
|
@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("UK\r\n" + "US\r\n" + "Germany\r\n");
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
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.Before;
|
||||||
|
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.properties";
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws Exception {
|
||||||
|
PrintWriter writer = new PrintWriter(fileName);
|
||||||
|
writer.print("UK\r\n" + "US\r\n" + "Germany\r\n");
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAppendToFileUsingGuava_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("UK\r\n" + "US\r\n" + "Germany\r\n");
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class AnonymousInnerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenRunAnonymousClass_thenCorrect() {
|
||||||
|
SimpleAbstractClass simpleAbstractClass = new SimpleAbstractClass() {
|
||||||
|
void run() {
|
||||||
|
System.out.println("Running Anonymous Class...");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
simpleAbstractClass.run();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class InnerClassTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInnerClassWhenInstantiating_thenCorrect() {
|
||||||
|
Outer outer = new Outer();
|
||||||
|
Outer.Inner inner = outer.new Inner();
|
||||||
|
inner.test();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class LocalClassTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTestingLocalClass_thenCorrect() {
|
||||||
|
NewEnclosing newEnclosing = new NewEnclosing();
|
||||||
|
newEnclosing.run();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class NestedClassTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenInstantiatingStaticNestedClass_thenCorrect() {
|
||||||
|
Enclosing.Nested nested = new Enclosing.Nested();
|
||||||
|
nested.test();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.nestedclass;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class NewOuterTest {
|
||||||
|
|
||||||
|
int a = 1;
|
||||||
|
static int b = 2;
|
||||||
|
|
||||||
|
public class InnerClass {
|
||||||
|
int a = 3;
|
||||||
|
static final int b = 4;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenShadowing_thenCorrect() {
|
||||||
|
assertEquals(3, a);
|
||||||
|
assertEquals(4, b);
|
||||||
|
assertEquals(1, NewOuterTest.this.a);
|
||||||
|
assertEquals(2, NewOuterTest.b);
|
||||||
|
assertEquals(2, NewOuterTest.this.b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shadowingTest() {
|
||||||
|
NewOuterTest outer = new NewOuterTest();
|
||||||
|
NewOuterTest.InnerClass inner = outer.new InnerClass();
|
||||||
|
inner.whenShadowing_thenCorrect();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -223,4 +223,4 @@ public class StringTest {
|
||||||
|
|
||||||
assertEquals("200", String.valueOf(l));
|
assertEquals("200", String.valueOf(l));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue