Skip to content

Commit c250fa5

Browse files
Merge remote-tracking branch 'origin/master'
2 parents a9dff7f + 115220b commit c250fa5

59 files changed

Lines changed: 2173 additions & 27 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World from fileTest.txt!!!
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.file;
2+
3+
import org.hamcrest.Matchers;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.io.BufferedReader;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
import java.net.URL;
12+
import java.net.URLConnection;
13+
14+
import static org.hamcrest.CoreMatchers.containsString;
15+
16+
public class FileOperationsTest {
17+
18+
@Test
19+
public void givenFileName_whenUsingClassloader_thenFileData() throws IOException {
20+
String expectedData = "Hello World from fileTest.txt!!!";
21+
22+
ClassLoader classLoader = getClass().getClassLoader();
23+
InputStream inputStream = classLoader.getResourceAsStream("fileTest.txt");
24+
String data = readFromInputStream(inputStream);
25+
26+
Assert.assertThat(data, containsString(expectedData));
27+
}
28+
29+
@Test
30+
public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData() throws IOException {
31+
String expectedData = "Hello World from fileTest.txt!!!";
32+
33+
Class clazz = FileOperationsTest.class;
34+
InputStream inputStream = clazz.getResourceAsStream("/fileTest.txt");
35+
String data = readFromInputStream(inputStream);
36+
37+
Assert.assertThat(data, containsString(expectedData));
38+
}
39+
40+
@Test
41+
public void givenURLName_whenUsingURL_thenFileData() throws IOException {
42+
String expectedData = "Baeldung";
43+
44+
URL urlObject = new URL("http://www.baeldung.com/");
45+
URLConnection urlConnection = urlObject.openConnection();
46+
47+
InputStream inputStream = urlConnection.getInputStream();
48+
String data = readFromInputStream(inputStream);
49+
50+
Assert.assertThat(data, containsString(expectedData));
51+
}
52+
53+
@Test
54+
public void givenFileName_whenUsingJarFile_thenFileData() throws IOException {
55+
String expectedData = "BSD License";
56+
57+
Class clazz = Matchers.class;
58+
InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt");
59+
String data = readFromInputStream(inputStream);
60+
61+
Assert.assertThat(data, containsString(expectedData));
62+
}
63+
64+
private String readFromInputStream(InputStream inputStream) throws IOException {
65+
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
66+
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
67+
StringBuilder resultStringBuilder = new StringBuilder();
68+
String line;
69+
while ((line = bufferedReader.readLine()) != null) {
70+
resultStringBuilder.append(line);
71+
resultStringBuilder.append("\n");
72+
}
73+
bufferedReader.close();
74+
inputStreamReader.close();
75+
inputStream.close();
76+
return resultStringBuilder.toString();
77+
}
78+
}

‎core-java-8/src/test/java/com/baeldung/java8/JavaFolderSizeTest.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class JavaFolderSizeTest {
2121
@Before
2222
public void init() {
2323
final String separator = File.separator;
24-
path = "src" + separator + "test" + separator + "resources";
24+
path = String.format("src%stest%sresources", separator, separator);
2525
}
2626

2727
@Test
@@ -79,7 +79,9 @@ public void whenGetFolderSizeUsingGuava_thenCorrect() {
7979
final File folder = new File(path);
8080

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

8486
assertEquals(expectedSize, size);
8587
}
@@ -101,13 +103,11 @@ private long getFolderSize(final File folder) {
101103
long length = 0;
102104
final File[] files = folder.listFiles();
103105

104-
final int count = files.length;
105-
106-
for (int i = 0; i < count; i++) {
107-
if (files[i].isFile()) {
108-
length += files[i].length();
106+
for (File file : files) {
107+
if (file.isFile()) {
108+
length += file.length();
109109
} else {
110-
length += getFolderSize(files[i]);
110+
length += getFolderSize(file);
111111
}
112112
}
113113
return length;

‎core-java/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@
123123
<version>${mockito.version}</version>
124124
<scope>test</scope>
125125
</dependency>
126+
127+
<dependency>
128+
<groupId>commons-codec</groupId>
129+
<artifactId>commons-codec</artifactId>
130+
<version>1.10</version>
131+
</dependency>
126132

127133
</dependencies>
128134

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.java.regex;
2+
3+
public class Result {
4+
private boolean found = false;
5+
private int count = 0;
6+
7+
public Result() {
8+
9+
}
10+
11+
public boolean isFound() {
12+
return found;
13+
}
14+
15+
public void setFound(boolean found) {
16+
this.found = found;
17+
}
18+
19+
public int getCount() {
20+
return count;
21+
}
22+
23+
public void setCount(int count) {
24+
this.count = count;
25+
}
26+
27+
}

0 commit comments

Comments
 (0)