Skip to content

Commit 84a9b2e

Browse files
author
Eugen Paraschiv
committed
minor cleanup
1 parent 5c6565b commit 84a9b2e

11 files changed

Lines changed: 64 additions & 92 deletions

File tree

‎core-java-io/src/main/java/com/baeldung/java/nio2/watcher/DirectoryWatcherExample.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.nio.file.WatchService;
1111

1212
public class DirectoryWatcherExample {
13-
13+
1414
public static void main(String[] args) throws IOException, InterruptedException {
1515
WatchService watchService = FileSystems.getDefault().newWatchService();
1616
Path path = Paths.get(System.getProperty("user.home"));
@@ -25,5 +25,5 @@ public static void main(String[] args) throws IOException, InterruptedException
2525

2626
watchService.close();
2727
}
28-
28+
2929
}

‎core-java-io/src/test/java/com/baeldung/copyfiles/FileCopierTest.java‎

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,51 @@
1919
import static org.assertj.core.api.Assertions.*;
2020

2121
public class FileCopierTest {
22-
File original = new File("src/test/resources/original.txt");
22+
File original = new File("src/test/resources/original.txt");
2323

24-
@Before
25-
public void init() throws IOException {
26-
if (!original.exists())
27-
Files.createFile(original.toPath());
28-
}
24+
@Before
25+
public void init() throws IOException {
26+
if (!original.exists())
27+
Files.createFile(original.toPath());
28+
}
2929

30-
@Test
31-
public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
32-
File copied = new File("src/test/resources/copiedWithIo.txt");
33-
try (InputStream in = new BufferedInputStream(new FileInputStream(original));
34-
OutputStream out = new BufferedOutputStream(new FileOutputStream(copied))) {
35-
byte[] buffer = new byte[1024];
36-
int lengthRead;
37-
while ((lengthRead = in.read(buffer)) > 0) {
38-
out.write(buffer, 0, lengthRead);
39-
out.flush();
40-
}
41-
}
42-
assertThat(copied).exists();
43-
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
44-
}
30+
@Test
31+
public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
32+
File copied = new File("src/test/resources/copiedWithIo.txt");
33+
try (InputStream in = new BufferedInputStream(new FileInputStream(original)); OutputStream out = new BufferedOutputStream(new FileOutputStream(copied))) {
34+
byte[] buffer = new byte[1024];
35+
int lengthRead;
36+
while ((lengthRead = in.read(buffer)) > 0) {
37+
out.write(buffer, 0, lengthRead);
38+
out.flush();
39+
}
40+
}
41+
assertThat(copied).exists();
42+
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
43+
}
4544

46-
@Test
47-
public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
48-
File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
49-
FileUtils.copyFile(original, copied);
50-
assertThat(copied).exists();
51-
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
52-
}
45+
@Test
46+
public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
47+
File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
48+
FileUtils.copyFile(original, copied);
49+
assertThat(copied).exists();
50+
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
51+
}
5352

54-
@Test
55-
public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException {
56-
Path copied = Paths.get("src/test/resources/copiedWithNio.txt");
57-
Path originalPath = original.toPath();
58-
Files.copy(originalPath, copied, StandardCopyOption.REPLACE_EXISTING);
59-
assertThat(copied).exists();
60-
assertThat(Files.readAllLines(originalPath).equals(Files.readAllLines(copied)));
61-
}
53+
@Test
54+
public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException {
55+
Path copied = Paths.get("src/test/resources/copiedWithNio.txt");
56+
Path originalPath = original.toPath();
57+
Files.copy(originalPath, copied, StandardCopyOption.REPLACE_EXISTING);
58+
assertThat(copied).exists();
59+
assertThat(Files.readAllLines(originalPath).equals(Files.readAllLines(copied)));
60+
}
6261

63-
@Test
64-
public void givenGuava_whenCopied_thenCopyExistsWithSameContents() throws IOException {
65-
File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
66-
com.google.common.io.Files.copy(original, copied);
67-
assertThat(copied).exists();
68-
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
69-
}
62+
@Test
63+
public void givenGuava_whenCopied_thenCopyExistsWithSameContents() throws IOException {
64+
File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
65+
com.google.common.io.Files.copy(original, copied);
66+
assertThat(copied).exists();
67+
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
68+
}
7069
}

‎core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.apache.commons.io.FileUtils;
44
import org.hamcrest.CoreMatchers;
55
import org.hamcrest.Matchers;
6-
import org.junit.Assert;
76
import org.junit.Test;
87

98
import java.io.BufferedReader;

‎core-java-io/src/test/java/com/baeldung/file/FilesTest.java‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,22 @@ public void whenAppendToFileUsingGuava_thenCorrect() throws IOException {
4242
CharSink chs = com.google.common.io.Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
4343
chs.write("Spain\r\n");
4444

45-
assertThat(StreamUtils.getStringFromInputStream(
46-
new FileInputStream(fileName)))
47-
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
45+
assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
4846
}
4947

50-
5148
@Test
5249
public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
5350
Files.write(Paths.get(fileName), "Spain\r\n".getBytes(), StandardOpenOption.APPEND);
5451

55-
assertThat(StreamUtils.getStringFromInputStream(
56-
new FileInputStream(fileName)))
57-
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
52+
assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
5853
}
5954

6055
@Test
6156
public void whenAppendToFileUsingFileUtils_thenCorrect() throws IOException {
6257
File file = new File(fileName);
6358
FileUtils.writeStringToFile(file, "Spain\r\n", StandardCharsets.UTF_8, true);
6459

65-
assertThat(StreamUtils.getStringFromInputStream(
66-
new FileInputStream(fileName)))
67-
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
60+
assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
6861
}
6962

7063
@Test
@@ -73,9 +66,7 @@ public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception
7366
fos.write("Spain\r\n".getBytes());
7467
fos.close();
7568

76-
assertThat(StreamUtils.getStringFromInputStream(
77-
new FileInputStream(fileName)))
78-
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
69+
assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
7970
}
8071

8172
@Test
@@ -86,9 +77,6 @@ public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
8677
bw.newLine();
8778
bw.close();
8879

89-
assertThat(
90-
StreamUtils.getStringFromInputStream(
91-
new FileInputStream(fileName)))
92-
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n");
80+
assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n");
9381
}
9482
}

‎core-java-io/src/test/java/com/baeldung/filesystem/jndi/test/LookupFSJNDIIntegrationTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void givenLookupFSJNDI_whengetInitialContextIsNotNull_thenSuccess() {
3535

3636
@Test
3737
public void givenInitialContext_whenLokupFileExists_thenSuccess() {
38-
File file = fsjndi.getFile(FILENAME);
39-
assertNotNull("File exists", file);
38+
File file = fsjndi.getFile(FILENAME);
39+
assertNotNull("File exists", file);
4040
}
4141
}

‎core-java-io/src/test/java/com/baeldung/java/nio2/PathManualTest.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.nio.file.NoSuchFileException;
1010
import java.nio.file.Path;
1111
import java.nio.file.Paths;
12-
import java.util.Date;
1312

1413
import org.junit.Test;
1514

‎core-java-io/src/test/java/com/baeldung/java/nio2/attributes/BasicAttribsIntegrationTest.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class BasicAttribsIntegrationTest {
2020

2121
private static final Logger LOG = LoggerFactory.getLogger(BasicAttribsIntegrationTest.class);
2222

23-
2423
private static final String HOME = System.getProperty("user.home");
2524
private static BasicFileAttributes basicAttribs;
2625

‎core-java-io/src/test/java/com/baeldung/java8/JavaFolderSizeUnitTest.java‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attr
5555
@Test
5656
public void whenGetFolderSizeUsingJava8_thenCorrect() throws IOException {
5757
final Path folder = Paths.get(path);
58-
final long size = Files.walk(folder)
59-
.filter(p -> p.toFile()
60-
.isFile())
61-
.mapToLong(p -> p.toFile()
62-
.length())
63-
.sum();
58+
final long size = Files.walk(folder).filter(p -> p.toFile().isFile()).mapToLong(p -> p.toFile().length()).sum();
6459

6560
assertEquals(EXPECTED_SIZE, size);
6661
}
@@ -77,12 +72,8 @@ public void whenGetFolderSizeUsingApacheCommonsIO_thenCorrect() {
7772
public void whenGetFolderSizeUsingGuava_thenCorrect() {
7873
final File folder = new File(path);
7974

80-
final Iterable<File> files = com.google.common.io.Files.fileTreeTraverser()
81-
.breadthFirstTraversal(folder);
82-
final long size = StreamSupport.stream(files.spliterator(), false)
83-
.filter(File::isFile)
84-
.mapToLong(File::length)
85-
.sum();
75+
final Iterable<File> files = com.google.common.io.Files.fileTreeTraverser().breadthFirstTraversal(folder);
76+
final long size = StreamSupport.stream(files.spliterator(), false).filter(File::isFile).mapToLong(File::length).sum();
8677

8778
assertEquals(EXPECTED_SIZE, size);
8879
}

‎core-java-io/src/test/java/com/baeldung/mappedbytebuffer/MappedByteBufferUnitTest.java‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818

1919
public class MappedByteBufferUnitTest {
2020

21-
2221
@Test
2322
public void givenFileChannel_whenReadToTheMappedByteBuffer_thenShouldSuccess() throws Exception {
24-
//given
23+
// given
2524
CharBuffer charBuffer = null;
2625
Path pathToRead = getFileURIFromResources("fileToRead.txt");
2726

28-
//when
27+
// when
2928
try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToRead, EnumSet.of(StandardOpenOption.READ))) {
3029
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
3130

@@ -34,28 +33,27 @@ public void givenFileChannel_whenReadToTheMappedByteBuffer_thenShouldSuccess() t
3433
}
3534
}
3635

37-
//then
36+
// then
3837
assertNotNull(charBuffer);
3938
assertEquals(charBuffer.toString(), "This is a content of the file");
4039
}
4140

4241
@Test
4342
public void givenPath_whenWriteToItUsingMappedByteBuffer_thenShouldSuccessfullyWrite() throws Exception {
44-
//given
43+
// given
4544
CharBuffer charBuffer = CharBuffer.wrap("This will be written to the file");
4645
Path pathToWrite = getFileURIFromResources("fileToWriteTo.txt");
4746

48-
//when
49-
try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToWrite,
50-
EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))) {
47+
// when
48+
try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToWrite, EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))) {
5149
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, charBuffer.length());
5250

5351
if (mappedByteBuffer != null) {
5452
mappedByteBuffer.put(Charset.forName("utf-8").encode(charBuffer));
5553
}
5654
}
5755

58-
//then
56+
// then
5957
List<String> fileContent = Files.readAllLines(pathToWrite);
6058
assertEquals(fileContent.get(0), "This will be written to the file");
6159

‎core-java-io/src/test/java/org/baeldung/java/io/JavaInputStreamToXUnitTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public final void whenConvertingInputStreamToFile_thenCorrect4() throws IOExcept
210210

211211
FileUtils.copyInputStreamToFile(initialStream, targetFile);
212212
}
213-
213+
214214
@Test
215215
public final void givenUsingPlainJava_whenConvertingAnInputStreamToString_thenCorrect() throws IOException {
216216
String originalString = randomAlphabetic(8);
@@ -225,7 +225,7 @@ public final void givenUsingPlainJava_whenConvertingAnInputStreamToString_thenCo
225225

226226
buffer.flush();
227227
byte[] byteArray = buffer.toByteArray();
228-
228+
229229
String text = new String(byteArray, StandardCharsets.UTF_8);
230230
assertThat(text, equalTo(originalString));
231231
}

0 commit comments

Comments
 (0)