minor doc change

This commit is contained in:
eugenp 2014-07-09 23:42:47 +03:00
parent bc29b044dc
commit 8228e34dc0
1 changed files with 49 additions and 0 deletions

View File

@ -1,16 +1,25 @@
package org.baeldung.java.io;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.CharSequenceReader;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.io.CharSink;
import com.google.common.io.CharSource;
import com.google.common.io.CharStreams;
import com.google.common.io.FileWriteMode;
@SuppressWarnings("unused")
public class JavaReaderToXUnitTest {
@ -43,4 +52,44 @@ public class JavaReaderToXUnitTest {
initialReader.close();
}
// tests - Reader WRITE TO File
@Test
public void givenUsingPlainJava_whenConvertingReaderIntoFile_thenCorrect() throws IOException {
final File sourceFile = new File("src/test/resources/sourceFile.txt");
sourceFile.createNewFile();
final Reader initialReader = new FileReader(sourceFile);
final char[] buffer = new char[(int) sourceFile.length()];
initialReader.read(buffer);
initialReader.close();
final File targetFile = new File("src/test/resources/targetFile.txt");
targetFile.createNewFile();
final Writer targetFileWriter = new FileWriter(targetFile);
targetFileWriter.write(buffer);
targetFileWriter.close();
}
@Test
public void givenUsingGuava_whenConvertingReaderIntoFile_thenCorrect() throws IOException {
final Reader initialReader = CharSource.wrap("IDDQD").openStream();
final File targetFile = new File("src/test/resources/targetFile.txt");
com.google.common.io.Files.touch(targetFile);
final CharSink charSink = com.google.common.io.Files.asCharSink(targetFile, Charset.defaultCharset(), FileWriteMode.APPEND);
charSink.writeFrom(initialReader);
initialReader.close();
}
@Test
public void givenUsingCommonsIO_whenConvertingReaderIntoFile_thenCorrect() throws IOException {
final Reader initialReader = new CharSequenceReader("CharSequenceReader extends Reader");
final File targetFile = new File("src/test/resources/targetFile.txt");
FileUtils.touch(targetFile);
final byte[] buffer = IOUtils.toByteArray(initialReader);
FileUtils.writeByteArrayToFile(targetFile, buffer);
initialReader.close();
}
}