[BAEL-19088] - Move articles out of core-java-io part 3
This commit is contained in:
-69
@@ -1,69 +0,0 @@
|
||||
package com.baeldung.copyfiles;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class FileCopierIntegrationTest {
|
||||
File original = new File("src/test/resources/original.txt");
|
||||
|
||||
@Before
|
||||
public void init() throws IOException {
|
||||
if (!original.exists())
|
||||
Files.createFile(original.toPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
|
||||
File copied = new File("src/test/resources/copiedWithIo.txt");
|
||||
try (InputStream in = new BufferedInputStream(new FileInputStream(original)); OutputStream out = new BufferedOutputStream(new FileOutputStream(copied))) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int lengthRead;
|
||||
while ((lengthRead = in.read(buffer)) > 0) {
|
||||
out.write(buffer, 0, lengthRead);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
assertThat(copied).exists();
|
||||
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
|
||||
File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
|
||||
FileUtils.copyFile(original, copied);
|
||||
assertThat(copied).exists();
|
||||
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException {
|
||||
Path copied = Paths.get("src/test/resources/copiedWithNio.txt");
|
||||
Path originalPath = original.toPath();
|
||||
Files.copy(originalPath, copied, StandardCopyOption.REPLACE_EXISTING);
|
||||
assertThat(copied).exists();
|
||||
assertThat(Files.readAllLines(originalPath).equals(Files.readAllLines(copied)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuava_whenCopied_thenCopyExistsWithSameContents() throws IOException {
|
||||
File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
|
||||
com.google.common.io.Files.copy(original, copied);
|
||||
assertThat(copied).exists();
|
||||
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
|
||||
}
|
||||
}
|
||||
-100
@@ -1,100 +0,0 @@
|
||||
package com.baeldung.directories;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class NewDirectoryUnitTest {
|
||||
|
||||
private static final File TEMP_DIRECTORY = new File(System.getProperty("java.io.tmpdir"));
|
||||
|
||||
@Before
|
||||
public void beforeEach() {
|
||||
File newDirectory = new File(TEMP_DIRECTORY, "new_directory");
|
||||
File nestedInNewDirectory = new File(newDirectory, "nested_directory");
|
||||
File existingDirectory = new File(TEMP_DIRECTORY, "existing_directory");
|
||||
File existingNestedDirectory = new File(existingDirectory, "existing_nested_directory");
|
||||
File nestedInExistingDirectory = new File(existingDirectory, "nested_directory");
|
||||
|
||||
nestedInNewDirectory.delete();
|
||||
newDirectory.delete();
|
||||
nestedInExistingDirectory.delete();
|
||||
existingDirectory.mkdir();
|
||||
existingNestedDirectory.mkdir();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnexistingDirectory_whenMkdir_thenTrue() {
|
||||
File newDirectory = new File(TEMP_DIRECTORY, "new_directory");
|
||||
assertFalse(newDirectory.exists());
|
||||
|
||||
boolean directoryCreated = newDirectory.mkdir();
|
||||
|
||||
assertTrue(directoryCreated);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingDirectory_whenMkdir_thenFalse() {
|
||||
File newDirectory = new File(TEMP_DIRECTORY, "new_directory");
|
||||
newDirectory.mkdir();
|
||||
assertTrue(newDirectory.exists());
|
||||
|
||||
boolean directoryCreated = newDirectory.mkdir();
|
||||
|
||||
assertFalse(directoryCreated);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnexistingNestedDirectories_whenMkdir_thenFalse() {
|
||||
File newDirectory = new File(TEMP_DIRECTORY, "new_directory");
|
||||
File nestedDirectory = new File(newDirectory, "nested_directory");
|
||||
assertFalse(newDirectory.exists());
|
||||
assertFalse(nestedDirectory.exists());
|
||||
|
||||
boolean directoriesCreated = nestedDirectory.mkdir();
|
||||
|
||||
assertFalse(directoriesCreated);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnexistingNestedDirectories_whenMkdirs_thenTrue() {
|
||||
File newDirectory = new File(System.getProperty("java.io.tmpdir") + File.separator + "new_directory");
|
||||
File nestedDirectory = new File(newDirectory, "nested_directory");
|
||||
assertFalse(newDirectory.exists());
|
||||
assertFalse(nestedDirectory.exists());
|
||||
|
||||
boolean directoriesCreated = nestedDirectory.mkdirs();
|
||||
|
||||
assertTrue(directoriesCreated);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingParentDirectories_whenMkdirs_thenTrue() {
|
||||
File newDirectory = new File(TEMP_DIRECTORY, "existing_directory");
|
||||
newDirectory.mkdir();
|
||||
File nestedDirectory = new File(newDirectory, "nested_directory");
|
||||
assertTrue(newDirectory.exists());
|
||||
assertFalse(nestedDirectory.exists());
|
||||
|
||||
boolean directoriesCreated = nestedDirectory.mkdirs();
|
||||
|
||||
assertTrue(directoriesCreated);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingNestedDirectories_whenMkdirs_thenFalse() {
|
||||
File existingDirectory = new File(TEMP_DIRECTORY, "existing_directory");
|
||||
File existingNestedDirectory = new File(existingDirectory, "existing_nested_directory");
|
||||
assertTrue(existingDirectory.exists());
|
||||
assertTrue(existingNestedDirectory.exists());
|
||||
|
||||
boolean directoriesCreated = existingNestedDirectory.mkdirs();
|
||||
|
||||
assertFalse(directoriesCreated);
|
||||
}
|
||||
|
||||
}
|
||||
-96
@@ -1,96 +0,0 @@
|
||||
package com.baeldung.file;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.util.StreamUtils;
|
||||
|
||||
public class FilesClearDataUnitTest {
|
||||
|
||||
public static final String FILE_PATH = "src/test/resources/fileexample.txt";
|
||||
|
||||
@Before
|
||||
@After
|
||||
public void setup() throws IOException {
|
||||
PrintWriter writer = new PrintWriter(FILE_PATH);
|
||||
writer.print("This example shows how we can delete the file contents without deleting the file");
|
||||
writer.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingFile_whenDeleteContentUsingPrintWritter_thenEmptyFile() throws IOException {
|
||||
PrintWriter writer = new PrintWriter(FILE_PATH);
|
||||
writer.print("");
|
||||
writer.close();
|
||||
assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingFile_whenDeleteContentUsingPrintWritterWithougObject_thenEmptyFile() throws IOException {
|
||||
new PrintWriter(FILE_PATH).close();
|
||||
assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingFile_whenDeleteContentUsingFileWriter_thenEmptyFile() throws IOException {
|
||||
new FileWriter(FILE_PATH, false).close();
|
||||
|
||||
assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingFile_whenDeleteContentUsingFileOutputStream_thenEmptyFile() throws IOException {
|
||||
new FileOutputStream(FILE_PATH).close();
|
||||
|
||||
assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingFile_whenDeleteContentUsingFileUtils_thenEmptyFile() throws IOException {
|
||||
FileUtils.write(new File(FILE_PATH), "", Charset.defaultCharset());
|
||||
|
||||
assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingFile_whenDeleteContentUsingNIOFiles_thenEmptyFile() throws IOException {
|
||||
BufferedWriter writer = Files.newBufferedWriter(Paths.get(FILE_PATH));
|
||||
writer.write("");
|
||||
writer.flush();
|
||||
|
||||
assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingFile_whenDeleteContentUsingNIOFileChannel_thenEmptyFile() throws IOException {
|
||||
FileChannel.open(Paths.get(FILE_PATH), StandardOpenOption.WRITE).truncate(0).close();
|
||||
|
||||
assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingFile_whenDeleteContentUsingGuava_thenEmptyFile() throws IOException {
|
||||
File file = new File(FILE_PATH);
|
||||
byte[] empty = new byte[0];
|
||||
com.google.common.io.Files.write(empty, file);
|
||||
|
||||
assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
package com.baeldung.file;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.CharSink;
|
||||
import com.google.common.io.FileWriteMode;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.util.StreamUtils;
|
||||
|
||||
public class FilesManualTest {
|
||||
|
||||
public static final String fileName = "src/main/resources/countries.properties";
|
||||
|
||||
@Before
|
||||
@After
|
||||
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 = com.google.common.io.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");
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendToFileUsingFileUtils_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");
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
@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\r\n");
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.file;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.files.ListFiles;
|
||||
|
||||
public class ListFilesUnitTest {
|
||||
|
||||
private ListFiles listFiles = new ListFiles();
|
||||
private String DIRECTORY = "src/test/resources/listFilesUnitTestFolder";
|
||||
private static final int DEPTH = 1;
|
||||
private Set<String> EXPECTED_FILE_LIST = new HashSet<String>() {
|
||||
{
|
||||
add("test.xml");
|
||||
add("employee.json");
|
||||
add("students.json");
|
||||
add("country.txt");
|
||||
}
|
||||
};
|
||||
|
||||
@Test
|
||||
public void givenDir_whenUsingJAVAIO_thenListAllFiles() throws IOException {
|
||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingJavaIO(DIRECTORY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDir_whenWalkingTree_thenListAllFiles() throws IOException {
|
||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalk(DIRECTORY,DEPTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDir_whenWalkingTreeWithVisitor_thenListAllFiles() throws IOException {
|
||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalkAndVisitor(DIRECTORY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDir_whenUsingDirectoryStream_thenListAllFiles() throws IOException {
|
||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingDirectoryStream(DIRECTORY));
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.filesystem.jndi.test;
|
||||
package com.baeldung.filesystem.jndi;
|
||||
|
||||
import com.baeldung.filesystem.jndi.LookupFSJNDI;
|
||||
import org.junit.Test;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java.mimetype;
|
||||
package com.baeldung.mimetype;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.java.io;
|
||||
package com.baeldung.readfile;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.Ignore;
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java8;
|
||||
package com.baeldung.size;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -18,7 +18,7 @@ public class JavaFileSizeUnitTest {
|
||||
@Before
|
||||
public void init() {
|
||||
final String separator = File.separator;
|
||||
filePath = String.join(separator, new String[] { "src", "test", "resources", "testFolder", "sample_file_1.in" });
|
||||
filePath = String.join(separator, new String[] { "src", "test", "resources", "size", "sample_file_1.in" });
|
||||
}
|
||||
|
||||
@Test
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.java8;
|
||||
package com.baeldung.size;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -25,7 +25,7 @@ public class JavaFolderSizeUnitTest {
|
||||
@Before
|
||||
public void init() {
|
||||
final String separator = File.separator;
|
||||
path = String.format("src%stest%sresources%stestFolder", separator, separator, separator);
|
||||
path = String.format("src%stest%sresources%ssize", separator, separator, separator);
|
||||
}
|
||||
|
||||
@Test
|
||||
-52
@@ -1,52 +0,0 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FileCopyUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingStream_thenCopyFile() throws IOException {
|
||||
final File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_stream.txt");
|
||||
final File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_stream.txt");
|
||||
FileCopy.copyFileUsingStream(src, dest);
|
||||
|
||||
assertTrue(dest.exists());
|
||||
dest.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFiles_thenCopyFile() throws IOException {
|
||||
final File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_files.txt");
|
||||
final File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_files.txt");
|
||||
FileCopy.copyFileUsingJavaFiles(src, dest);
|
||||
|
||||
assertTrue(dest.exists());
|
||||
dest.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingChannel_thenCopyFile() throws IOException {
|
||||
final File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_channel.txt");
|
||||
final File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_channel.txt");
|
||||
FileCopy.copyFileUsingChannel(src, dest);
|
||||
|
||||
assertTrue(dest.exists());
|
||||
dest.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingApache_thenCopyFile() throws IOException {
|
||||
final File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_apache.txt");
|
||||
final File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_apache.txt");
|
||||
FileCopy.copyFileUsingApacheCommonsIO(src, dest);
|
||||
|
||||
assertTrue(dest.exists());
|
||||
dest.delete();
|
||||
}
|
||||
|
||||
}
|
||||
-58
@@ -1,58 +0,0 @@
|
||||
package org.baeldung.core.exceptions;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class FileNotFoundExceptionUnitTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FileNotFoundExceptionUnitTest.class);
|
||||
|
||||
private String fileName = Double.toString(Math.random());
|
||||
|
||||
@Test(expected = BusinessException.class)
|
||||
public void raiseBusinessSpecificException() throws IOException {
|
||||
try {
|
||||
readFailingFile();
|
||||
} catch (FileNotFoundException ex) {
|
||||
throw new BusinessException("BusinessException: necessary file was not present.");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createFile() throws IOException {
|
||||
try {
|
||||
readFailingFile();
|
||||
} catch (FileNotFoundException ex) {
|
||||
try {
|
||||
new File(fileName).createNewFile();
|
||||
readFailingFile();
|
||||
} catch (IOException ioe) {
|
||||
throw new RuntimeException("BusinessException: even creation is not possible.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void logError() throws IOException {
|
||||
try {
|
||||
readFailingFile();
|
||||
} catch (FileNotFoundException ex) {
|
||||
LOG.error("Optional file " + fileName + " was not found.");
|
||||
}
|
||||
}
|
||||
|
||||
private void readFailingFile() throws IOException {
|
||||
BufferedReader rd = new BufferedReader(new FileReader(new File(fileName)));
|
||||
rd.readLine();
|
||||
// no need to close file
|
||||
}
|
||||
|
||||
private class BusinessException extends RuntimeException {
|
||||
BusinessException(String string) {
|
||||
super(string);
|
||||
}
|
||||
}
|
||||
}
|
||||
-258
@@ -1,258 +0,0 @@
|
||||
package org.baeldung.java.io;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
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.base.Charsets;
|
||||
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 {
|
||||
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
private static final int DEFAULT_SIZE = 1500000;
|
||||
|
||||
// tests - sandbox
|
||||
|
||||
// tests - Reader to String
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenConvertingReaderIntoStringV1_thenCorrect() throws IOException {
|
||||
final Reader reader = new StringReader("With Java 1");
|
||||
int intValueOfChar;
|
||||
String targetString = "";
|
||||
while ((intValueOfChar = reader.read()) != -1) {
|
||||
targetString += (char) intValueOfChar;
|
||||
}
|
||||
reader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenConvertingReaderIntoStringV2_thenCorrect() throws IOException {
|
||||
final Reader initialReader = new StringReader("With Java 1");
|
||||
final char[] arr = new char[8 * 1024];
|
||||
final StringBuilder buffer = new StringBuilder();
|
||||
int numCharsRead;
|
||||
while ((numCharsRead = initialReader.read(arr, 0, arr.length)) != -1) {
|
||||
buffer.append(arr, 0, numCharsRead);
|
||||
}
|
||||
initialReader.close();
|
||||
final String targetString = buffer.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGuava_whenConvertingReaderIntoString_thenCorrect() throws IOException {
|
||||
final Reader initialReader = CharSource.wrap("With Google Guava").openStream();
|
||||
final String targetString = CharStreams.toString(initialReader);
|
||||
initialReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCommonsIO_whenConvertingReaderIntoString_thenCorrect() throws IOException {
|
||||
final Reader initialReader = new StringReader("With Apache Commons");
|
||||
final String targetString = IOUtils.toString(initialReader);
|
||||
initialReader.close();
|
||||
}
|
||||
|
||||
// tests - Reader WRITE TO File
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenWritingReaderContentsToFile_thenCorrect() throws IOException {
|
||||
final Reader initialReader = new StringReader("Some text");
|
||||
|
||||
int intValueOfChar;
|
||||
final StringBuilder buffer = new StringBuilder();
|
||||
while ((intValueOfChar = initialReader.read()) != -1) {
|
||||
buffer.append((char) intValueOfChar);
|
||||
}
|
||||
initialReader.close();
|
||||
|
||||
final File targetFile = new File("src/test/resources/targetFile.txt");
|
||||
targetFile.createNewFile();
|
||||
|
||||
final Writer targetFileWriter = new FileWriter(targetFile);
|
||||
targetFileWriter.write(buffer.toString());
|
||||
targetFileWriter.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGuava_whenWritingReaderContentsToFile_thenCorrect() throws IOException {
|
||||
final Reader initialReader = new StringReader("Some text");
|
||||
|
||||
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_whenWritingReaderContentsToFile_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();
|
||||
}
|
||||
|
||||
// tests - Reader to byte[]
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenConvertingReaderIntoByteArray_thenCorrect() throws IOException {
|
||||
final Reader initialReader = new StringReader("With Java");
|
||||
|
||||
final char[] charArray = new char[8 * 1024];
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
int numCharsRead;
|
||||
while ((numCharsRead = initialReader.read(charArray, 0, charArray.length)) != -1) {
|
||||
builder.append(charArray, 0, numCharsRead);
|
||||
}
|
||||
final byte[] targetArray = builder.toString().getBytes();
|
||||
|
||||
initialReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGuava_whenConvertingReaderIntoByteArray_thenCorrect() throws IOException {
|
||||
final Reader initialReader = CharSource.wrap("With Google Guava").openStream();
|
||||
|
||||
final byte[] targetArray = CharStreams.toString(initialReader).getBytes();
|
||||
|
||||
initialReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCommonsIO_whenConvertingReaderIntoByteArray_thenCorrect() throws IOException {
|
||||
final StringReader initialReader = new StringReader("With Commons IO");
|
||||
|
||||
final byte[] targetArray = IOUtils.toByteArray(initialReader);
|
||||
|
||||
initialReader.close();
|
||||
}
|
||||
|
||||
// tests - Reader to InputStream
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenConvertingReaderIntoInputStream_thenCorrect() throws IOException {
|
||||
final Reader initialReader = new StringReader("With Java");
|
||||
|
||||
final char[] charBuffer = new char[8 * 1024];
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
int numCharsRead;
|
||||
while ((numCharsRead = initialReader.read(charBuffer, 0, charBuffer.length)) != -1) {
|
||||
builder.append(charBuffer, 0, numCharsRead);
|
||||
}
|
||||
final InputStream targetStream = new ByteArrayInputStream(builder.toString().getBytes());
|
||||
|
||||
initialReader.close();
|
||||
targetStream.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGuava_whenConvertingReaderIntoInputStream_thenCorrect() throws IOException {
|
||||
final Reader initialReader = new StringReader("With Guava");
|
||||
|
||||
final InputStream targetStream = new ByteArrayInputStream(CharStreams.toString(initialReader).getBytes());
|
||||
|
||||
initialReader.close();
|
||||
targetStream.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCommonsIO_whenConvertingReaderIntoInputStream() throws IOException {
|
||||
final Reader initialReader = new StringReader("With Commons IO");
|
||||
|
||||
final InputStream targetStream = IOUtils.toInputStream(IOUtils.toString(initialReader));
|
||||
|
||||
initialReader.close();
|
||||
targetStream.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCommonsIO_whenConvertingReaderIntoInputStream_thenCorrect() throws IOException {
|
||||
String initialString = "With Commons IO";
|
||||
final Reader initialReader = new StringReader(initialString);
|
||||
|
||||
final InputStream targetStream = IOUtils.toInputStream(IOUtils.toString(initialReader));
|
||||
|
||||
final String finalString = IOUtils.toString(targetStream);
|
||||
assertThat(finalString, equalTo(initialString));
|
||||
|
||||
initialReader.close();
|
||||
targetStream.close();
|
||||
}
|
||||
|
||||
// tests - Reader to InputStream with encoding
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenConvertingReaderIntoInputStreamWithCharset() throws IOException {
|
||||
final Reader initialReader = new StringReader("With Java");
|
||||
|
||||
final char[] charBuffer = new char[8 * 1024];
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
int numCharsRead;
|
||||
while ((numCharsRead = initialReader.read(charBuffer, 0, charBuffer.length)) != -1) {
|
||||
builder.append(charBuffer, 0, numCharsRead);
|
||||
}
|
||||
final InputStream targetStream = new ByteArrayInputStream(builder.toString().getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
initialReader.close();
|
||||
targetStream.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGuava_whenConvertingReaderIntoInputStreamWithCharset_thenCorrect() throws IOException {
|
||||
final Reader initialReader = new StringReader("With Guava");
|
||||
|
||||
final InputStream targetStream = new ByteArrayInputStream(CharStreams.toString(initialReader).getBytes(Charsets.UTF_8));
|
||||
|
||||
initialReader.close();
|
||||
targetStream.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCommonsIO_whenConvertingReaderIntoInputStreamWithEncoding() throws IOException {
|
||||
final Reader initialReader = new StringReader("With Commons IO");
|
||||
|
||||
final InputStream targetStream = IOUtils.toInputStream(IOUtils.toString(initialReader), Charsets.UTF_8);
|
||||
|
||||
initialReader.close();
|
||||
targetStream.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCommonsIO_whenConvertingReaderIntoInputStreamWithEncoding_thenCorrect() throws IOException {
|
||||
String initialString = "With Commons IO";
|
||||
final Reader initialReader = new StringReader(initialString);
|
||||
final InputStream targetStream = IOUtils.toInputStream(IOUtils.toString(initialReader), Charsets.UTF_8);
|
||||
|
||||
String finalString = IOUtils.toString(targetStream, Charsets.UTF_8);
|
||||
assertThat(finalString, equalTo(initialString));
|
||||
|
||||
initialReader.close();
|
||||
targetStream.close();
|
||||
}
|
||||
|
||||
}
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
package org.baeldung.java.io;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class JavaXToByteArrayUnitTest {
|
||||
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
// tests - X to Byte Array
|
||||
|
||||
}
|
||||
-147
@@ -1,147 +0,0 @@
|
||||
package org.baeldung.java.io;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
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.ByteSource;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.common.io.CharSource;
|
||||
|
||||
public class JavaXToReaderUnitTest {
|
||||
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
// tests - String to Reader
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenConvertingStringIntoReader_thenCorrect() throws IOException {
|
||||
final String initialString = "With Plain Java";
|
||||
final Reader targetReader = new StringReader(initialString);
|
||||
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGuava_whenConvertingStringIntoReader_thenCorrect() throws IOException {
|
||||
final String initialString = "With Google Guava";
|
||||
final Reader targetReader = CharSource.wrap(initialString).openStream();
|
||||
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCommonsIO_whenConvertingStringIntoReader_thenCorrect() throws IOException {
|
||||
final String initialString = "With Apache Commons IO";
|
||||
final Reader targetReader = new CharSequenceReader(initialString);
|
||||
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
// tests - byte array to Reader
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenConvertingByteArrayIntoReader_thenCorrect() throws IOException {
|
||||
final byte[] initialArray = "Hello world!".getBytes();
|
||||
final Reader targetReader = new StringReader(new String(initialArray));
|
||||
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava2_whenConvertingByteArrayIntoReader_thenCorrect() throws IOException {
|
||||
final byte[] initialArray = "Hello world!".getBytes();
|
||||
final Reader targetReader = new InputStreamReader(new ByteArrayInputStream(initialArray));
|
||||
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGuava_whenConvertingByteArrayIntoReader_thenCorrect() throws IOException {
|
||||
final byte[] initialArray = "With Guava".getBytes();
|
||||
final String bufferString = new String(initialArray);
|
||||
final Reader targetReader = CharSource.wrap(bufferString).openStream();
|
||||
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCommonsIO_whenConvertingByteArrayIntoReader_thenCorrect() throws IOException {
|
||||
final byte[] initialArray = "With Commons IO".getBytes();
|
||||
final Reader targetReader = new CharSequenceReader(new String(initialArray));
|
||||
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
// tests - File to Reader
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenConvertingFileIntoReader_thenCorrect() throws IOException {
|
||||
final File initialFile = new File("src/test/resources/initialFile.txt");
|
||||
initialFile.createNewFile();
|
||||
final Reader targetReader = new FileReader(initialFile);
|
||||
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGuava_whenConvertingFileIntoReader_thenCorrect() throws IOException {
|
||||
final File initialFile = new File("src/test/resources/initialFile.txt");
|
||||
com.google.common.io.Files.touch(initialFile);
|
||||
final Reader targetReader = com.google.common.io.Files.newReader(initialFile, Charset.defaultCharset());
|
||||
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCommonsIO_whenConvertingFileIntoReader_thenCorrect() throws IOException {
|
||||
final File initialFile = new File("src/test/resources/initialFile.txt");
|
||||
FileUtils.touch(initialFile);
|
||||
FileUtils.write(initialFile, "With Commons IO");
|
||||
final byte[] buffer = FileUtils.readFileToByteArray(initialFile);
|
||||
final Reader targetReader = new CharSequenceReader(new String(buffer));
|
||||
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
// tests - InputStream to Reader
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenConvertingInputStreamIntoReader_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = new ByteArrayInputStream("With Java".getBytes());
|
||||
final Reader targetReader = new InputStreamReader(initialStream);
|
||||
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGuava_whenConvertingInputStreamIntoReader_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = ByteSource.wrap("With Guava".getBytes()).openStream();
|
||||
final byte[] buffer = ByteStreams.toByteArray(initialStream);
|
||||
final Reader targetReader = CharSource.wrap(new String(buffer)).openStream();
|
||||
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCommonsIO_whenConvertingInputStreamIntoReader_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = IOUtils.toInputStream("With Commons IO");
|
||||
final byte[] buffer = IOUtils.toByteArray(initialStream);
|
||||
final Reader targetReader = new CharSequenceReader(new String(buffer));
|
||||
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
}
|
||||
-186
@@ -1,186 +0,0 @@
|
||||
package org.baeldung.writetofile;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.FileLock;
|
||||
import java.nio.channels.OverlappingFileLockException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JavaWriteToFileUnitTest {
|
||||
|
||||
private String fileName = "src/test/resources/test_write.txt";
|
||||
private String fileName1 = "src/test/resources/test_write_1.txt";
|
||||
private String fileName2 = "src/test/resources/test_write_2.txt";
|
||||
private String fileName3 = "src/test/resources/test_write_3.txt";
|
||||
private String fileName4 = "src/test/resources/test_write_4.txt";
|
||||
private String fileName5 = "src/test/resources/test_write_5.txt";
|
||||
|
||||
@Test
|
||||
public void whenWriteStringUsingBufferedWritter_thenCorrect() throws IOException {
|
||||
final String str = "Hello";
|
||||
final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3));
|
||||
writer.write(str);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo() throws IOException {
|
||||
final String str = "World";
|
||||
final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3, true));
|
||||
writer.append(' ');
|
||||
writer.append(str);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWritingStringToFile_whenUsingPrintWriter_thenCorrect() throws IOException {
|
||||
final FileWriter fileWriter = new FileWriter(fileName);
|
||||
final PrintWriter printWriter = new PrintWriter(fileWriter);
|
||||
printWriter.print("Some String");
|
||||
printWriter.printf("Product name is %s and its price is %d $", "iPhone", 1000);
|
||||
printWriter.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWritingStringToFile_whenUsingFileOutputStream_thenCorrect() throws IOException {
|
||||
final String str = "Hello";
|
||||
final FileOutputStream outputStream = new FileOutputStream(fileName3);
|
||||
final byte[] strToBytes = str.getBytes();
|
||||
outputStream.write(strToBytes);
|
||||
outputStream.close();
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Test
|
||||
public void givenWritingToFile_whenUsingDataOutputStream_thenCorrect() throws IOException {
|
||||
final String value = "Hello";
|
||||
final FileOutputStream fos = new FileOutputStream(fileName1);
|
||||
final DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
|
||||
outStream.writeUTF(value);
|
||||
outStream.close();
|
||||
|
||||
String result;
|
||||
final FileInputStream fis = new FileInputStream(fileName1);
|
||||
final DataInputStream reader = new DataInputStream(fis);
|
||||
result = reader.readUTF();
|
||||
reader.close();
|
||||
|
||||
assertEquals(value, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenWritingToSpecificPositionInFile_thenCorrect() throws IOException {
|
||||
final int data1 = 2014;
|
||||
final int data2 = 1500;
|
||||
writeToPosition(fileName2, data1, 4);
|
||||
assertEquals(data1, readFromPosition(fileName2, 4));
|
||||
writeToPosition(fileName2, data2, 4);
|
||||
assertEquals(data2, readFromPosition(fileName2, 4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTryToLockFile_thenItShouldBeLocked() throws IOException {
|
||||
final RandomAccessFile stream = new RandomAccessFile(fileName4, "rw");
|
||||
final FileChannel channel = stream.getChannel();
|
||||
|
||||
FileLock lock = null;
|
||||
try {
|
||||
lock = channel.tryLock();
|
||||
} catch (final OverlappingFileLockException e) {
|
||||
stream.close();
|
||||
channel.close();
|
||||
}
|
||||
stream.writeChars("test lock");
|
||||
lock.release();
|
||||
|
||||
stream.close();
|
||||
channel.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWritingToFile_whenUsingFileChannel_thenCorrect() throws IOException {
|
||||
final RandomAccessFile stream = new RandomAccessFile(fileName5, "rw");
|
||||
final FileChannel channel = stream.getChannel();
|
||||
final String value = "Hello";
|
||||
final byte[] strBytes = value.getBytes();
|
||||
final ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
|
||||
buffer.put(strBytes);
|
||||
buffer.flip();
|
||||
channel.write(buffer);
|
||||
stream.close();
|
||||
channel.close();
|
||||
|
||||
final RandomAccessFile reader = new RandomAccessFile(fileName5, "r");
|
||||
assertEquals(value, reader.readLine());
|
||||
reader.close();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenWriteToTmpFile_thenCorrect() throws IOException {
|
||||
final String toWrite = "Hello";
|
||||
final File tmpFile = File.createTempFile("test", ".tmp");
|
||||
final FileWriter writer = new FileWriter(tmpFile);
|
||||
writer.write(toWrite);
|
||||
writer.close();
|
||||
|
||||
final BufferedReader reader = new BufferedReader(new FileReader(tmpFile));
|
||||
assertEquals(toWrite, reader.readLine());
|
||||
reader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingJava7_whenWritingToFile_thenCorrect() throws IOException {
|
||||
final String str = "Hello";
|
||||
|
||||
final Path path = Paths.get(fileName3);
|
||||
final byte[] strToBytes = str.getBytes();
|
||||
|
||||
Files.write(path, strToBytes);
|
||||
|
||||
final String read = Files.readAllLines(path, Charset.defaultCharset()).get(0);
|
||||
assertEquals(str, read);
|
||||
}
|
||||
|
||||
// UTIL
|
||||
|
||||
// use RandomAccessFile to write data at specific position in the file
|
||||
private void writeToPosition(final String filename, final int data, final long position) throws IOException {
|
||||
final RandomAccessFile writer = new RandomAccessFile(filename, "rw");
|
||||
writer.seek(position);
|
||||
writer.writeInt(data);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
// use RandomAccessFile to read data from specific position in the file
|
||||
private int readFromPosition(final String filename, final long position) throws IOException {
|
||||
int result = 0;
|
||||
final RandomAccessFile reader = new RandomAccessFile(filename, "r");
|
||||
reader.seek(position);
|
||||
result = reader.readInt();
|
||||
reader.close();
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
@@ -1,2 +0,0 @@
|
||||
files will be copied here and then deleted
|
||||
remove `file.delete()` to see the files here
|
||||
@@ -1 +0,0 @@
|
||||
apache
|
||||
@@ -1 +0,0 @@
|
||||
channel
|
||||
@@ -1 +0,0 @@
|
||||
files
|
||||
@@ -1 +0,0 @@
|
||||
stream
|
||||
@@ -1 +0,0 @@
|
||||
This example shows how we can delete the file contents without deleting the file
|
||||
@@ -1 +0,0 @@
|
||||
With Commons IO
|
||||
@@ -1 +0,0 @@
|
||||
This is a sample txt file for unit test ListFilesUnitTest
|
||||
-1
@@ -1 +0,0 @@
|
||||
{}
|
||||
-1
@@ -1 +0,0 @@
|
||||
{}
|
||||
@@ -1 +0,0 @@
|
||||
<xml></xml>
|
||||
@@ -1 +0,0 @@
|
||||
Hello World
|
||||
@@ -1 +0,0 @@
|
||||
Some textSome text
|
||||
@@ -1 +0,0 @@
|
||||
Some StringProduct name is iPhone and its price is 1000 $
|
||||
Binary file not shown.
Binary file not shown.
@@ -1 +0,0 @@
|
||||
Hello World
|
||||
Binary file not shown.
@@ -1 +0,0 @@
|
||||
Hello
|
||||
Reference in New Issue
Block a user