Skip to content

Commit c6b3e03

Browse files
tapankavasthimaibin
authored andcommitted
BAEL-3215: A Guide to the Java FileReader Class (eugenp#7677)
1 parent b0c1efd commit c6b3e03

File tree

5 files changed

+402
-0
lines changed

5 files changed

+402
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Intellij
2+
.idea/
3+
*.iml
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>core-java-io-2</artifactId>
5+
<version>0.1.0-SNAPSHOT</version>
6+
<name>core-java-io-2</name>
7+
<packaging>jar</packaging>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>parent-java</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
<relativePath>../../parent-java</relativePath>
14+
</parent>
15+
16+
<dependencies>
17+
<!-- utils -->
18+
<dependency>
19+
<groupId>net.sourceforge.collections</groupId>
20+
<artifactId>collections-generic</artifactId>
21+
<version>${collections-generic.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.apache.commons</groupId>
25+
<artifactId>commons-collections4</artifactId>
26+
<version>${commons-collections4.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>commons-io</groupId>
30+
<artifactId>commons-io</artifactId>
31+
<version>${commons-io.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.commons</groupId>
35+
<artifactId>commons-lang3</artifactId>
36+
<version>${commons-lang3.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.apache.commons</groupId>
40+
<artifactId>commons-math3</artifactId>
41+
<version>${commons-math3.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.decimal4j</groupId>
45+
<artifactId>decimal4j</artifactId>
46+
<version>${decimal4j.version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.bouncycastle</groupId>
50+
<artifactId>bcprov-jdk15on</artifactId>
51+
<version>${bouncycastle.version}</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.unix4j</groupId>
55+
<artifactId>unix4j-command</artifactId>
56+
<version>${unix4j.version}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.googlecode.grep4j</groupId>
60+
<artifactId>grep4j</artifactId>
61+
<version>${grep4j.version}</version>
62+
</dependency>
63+
<!-- web -->
64+
<!-- marshalling -->
65+
<dependency>
66+
<groupId>com.fasterxml.jackson.core</groupId>
67+
<artifactId>jackson-databind</artifactId>
68+
<version>${jackson.version}</version>
69+
</dependency>
70+
<!-- logging -->
71+
<dependency>
72+
<groupId>log4j</groupId>
73+
<artifactId>log4j</artifactId>
74+
<version>${log4j.version}</version>
75+
</dependency>
76+
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
77+
<groupId>org.slf4j</groupId>
78+
<artifactId>log4j-over-slf4j</artifactId>
79+
<version>${org.slf4j.version}</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.projectlombok</groupId>
83+
<artifactId>lombok</artifactId>
84+
<version>${lombok.version}</version>
85+
<scope>provided</scope>
86+
</dependency>
87+
<!-- test scoped -->
88+
<dependency>
89+
<groupId>org.assertj</groupId>
90+
<artifactId>assertj-core</artifactId>
91+
<version>${assertj.version}</version>
92+
<scope>test</scope>
93+
</dependency>
94+
<dependency>
95+
<groupId>com.jayway.awaitility</groupId>
96+
<artifactId>awaitility</artifactId>
97+
<version>${avaitility.version}</version>
98+
<scope>test</scope>
99+
</dependency>
100+
<dependency>
101+
<groupId>commons-codec</groupId>
102+
<artifactId>commons-codec</artifactId>
103+
<version>${commons-codec.version}</version>
104+
</dependency>
105+
<dependency>
106+
<groupId>org.javamoney</groupId>
107+
<artifactId>moneta</artifactId>
108+
<version>${moneta.version}</version>
109+
</dependency>
110+
<dependency>
111+
<groupId>org.owasp.esapi</groupId>
112+
<artifactId>esapi</artifactId>
113+
<version>${esapi.version}</version>
114+
</dependency>
115+
<dependency>
116+
<groupId>com.sun.messaging.mq</groupId>
117+
<artifactId>fscontext</artifactId>
118+
<version>${fscontext.version}</version>
119+
</dependency>
120+
<dependency>
121+
<groupId>com.codepoetics</groupId>
122+
<artifactId>protonpack</artifactId>
123+
<version>${protonpack.version}</version>
124+
</dependency>
125+
<dependency>
126+
<groupId>one.util</groupId>
127+
<artifactId>streamex</artifactId>
128+
<version>${streamex.version}</version>
129+
</dependency>
130+
<dependency>
131+
<groupId>io.vavr</groupId>
132+
<artifactId>vavr</artifactId>
133+
<version>${vavr.version}</version>
134+
</dependency>
135+
<dependency>
136+
<groupId>org.openjdk.jmh</groupId>
137+
<artifactId>jmh-core</artifactId>
138+
<version>${jmh-core.version}</version>
139+
</dependency>
140+
<dependency>
141+
<groupId>org.openjdk.jmh</groupId>
142+
<artifactId>jmh-generator-annprocess</artifactId>
143+
<version>${jmh-generator-annprocess.version}</version>
144+
</dependency>
145+
<dependency>
146+
<groupId>org.hsqldb</groupId>
147+
<artifactId>hsqldb</artifactId>
148+
<version>${hsqldb.version}</version>
149+
<scope>runtime</scope>
150+
</dependency>
151+
<!-- https://mvnrepository.com/artifact/org.asynchttpclient/async-http-client -->
152+
<dependency>
153+
<groupId>org.asynchttpclient</groupId>
154+
<artifactId>async-http-client</artifactId>
155+
<version>${async-http-client.version}</version>
156+
</dependency>
157+
<dependency>
158+
<groupId>com.opencsv</groupId>
159+
<artifactId>opencsv</artifactId>
160+
<version>${opencsv.version}</version>
161+
<scope>test</scope>
162+
</dependency>
163+
<!-- Mime Type Resolution Libraries -->
164+
<dependency>
165+
<groupId>org.apache.tika</groupId>
166+
<artifactId>tika-core</artifactId>
167+
<version>${tika.version}</version>
168+
</dependency>
169+
<dependency>
170+
<groupId>net.sf.jmimemagic</groupId>
171+
<artifactId>jmimemagic</artifactId>
172+
<version>${jmime-magic.version}</version>
173+
</dependency>
174+
</dependencies>
175+
176+
<build>
177+
<finalName>core-java-io-2</finalName>
178+
<resources>
179+
<resource>
180+
<directory>src/main/resources</directory>
181+
<filtering>true</filtering>
182+
</resource>
183+
</resources>
184+
<plugins>
185+
<plugin>
186+
<groupId>org.codehaus.mojo</groupId>
187+
<artifactId>exec-maven-plugin</artifactId>
188+
<version>${exec-maven-plugin.version}</version>
189+
<configuration>
190+
<executable>java</executable>
191+
<mainClass>com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed</mainClass>
192+
<arguments>
193+
<argument>-Xmx300m</argument>
194+
<argument>-XX:+UseParallelGC</argument>
195+
<argument>-classpath</argument>
196+
<classpath />
197+
<argument>com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed</argument>
198+
</arguments>
199+
</configuration>
200+
</plugin>
201+
<plugin>
202+
<groupId>org.apache.maven.plugins</groupId>
203+
<artifactId>maven-javadoc-plugin</artifactId>
204+
<version>${maven-javadoc-plugin.version}</version>
205+
<configuration>
206+
<source>${maven.compiler.source}</source>
207+
<target>${maven.compiler.target}</target>
208+
</configuration>
209+
</plugin>
210+
</plugins>
211+
</build>
212+
213+
<profiles>
214+
<profile>
215+
<id>integration</id>
216+
<build>
217+
<plugins>
218+
<plugin>
219+
<groupId>org.codehaus.mojo</groupId>
220+
<artifactId>exec-maven-plugin</artifactId>
221+
222+
<executions>
223+
<execution>
224+
<id>run-benchmarks</id>
225+
<!-- <phase>integration-test</phase> -->
226+
<phase>none</phase>
227+
<goals>
228+
<goal>exec</goal>
229+
</goals>
230+
<configuration>
231+
<classpathScope>test</classpathScope>
232+
<executable>java</executable>
233+
<arguments>
234+
<argument>-classpath</argument>
235+
<classpath />
236+
<argument>org.openjdk.jmh.Main</argument>
237+
<argument>.*</argument>
238+
</arguments>
239+
</configuration>
240+
</execution>
241+
</executions>
242+
</plugin>
243+
</plugins>
244+
</build>
245+
</profile>
246+
</profiles>
247+
248+
<properties>
249+
250+
<!-- util -->
251+
<bouncycastle.version>1.55</bouncycastle.version>
252+
<commons-codec.version>1.10</commons-codec.version>
253+
<commons-math3.version>3.6.1</commons-math3.version>
254+
<decimal4j.version>1.0.3</decimal4j.version>
255+
<commons-collections4.version>4.1</commons-collections4.version>
256+
<collections-generic.version>4.01</collections-generic.version>
257+
<unix4j.version>0.4</unix4j.version>
258+
<grep4j.version>1.8.7</grep4j.version>
259+
<fscontext.version>4.6-b01</fscontext.version>
260+
<protonpack.version>1.13</protonpack.version>
261+
<streamex.version>0.6.5</streamex.version>
262+
<vavr.version>0.9.0</vavr.version>
263+
<opencsv.version>4.1</opencsv.version>
264+
<!-- testing -->
265+
<assertj.version>3.6.1</assertj.version>
266+
<avaitility.version>1.7.0</avaitility.version>
267+
268+
<!-- maven plugins -->
269+
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
270+
<hsqldb.version>2.4.0</hsqldb.version>
271+
<esapi.version>2.1.0.1</esapi.version>
272+
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
273+
<async-http-client.version>2.4.5</async-http-client.version>
274+
<!-- Mime Type Libraries -->
275+
<tika.version>1.18</tika.version>
276+
<jmime-magic.version>0.1.5</jmime-magic.version>
277+
</properties>
278+
279+
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.filereader;
2+
3+
import java.io.*;
4+
5+
public class FileReaderExample {
6+
7+
public static String readAllCharactersOneByOne(Reader reader) throws IOException {
8+
StringBuilder content = new StringBuilder();
9+
int nextChar;
10+
while ((nextChar = reader.read()) != -1) {
11+
content.append((char) nextChar);
12+
}
13+
return String.valueOf(content);
14+
}
15+
16+
public static String readMultipleCharacters(Reader reader, int length) throws IOException {
17+
char[] buffer = new char[length];
18+
int charactersRead = reader.read(buffer, 0, length);
19+
20+
21+
if (charactersRead != -1) {
22+
return new String(buffer, 0, charactersRead);
23+
} else {
24+
return "";
25+
}
26+
}
27+
28+
public static String readFile(String path) {
29+
FileReader fileReader = null;
30+
try {
31+
fileReader = new FileReader(path);
32+
return readAllCharactersOneByOne(fileReader);
33+
} catch (IOException e) {
34+
e.printStackTrace();
35+
} finally {
36+
if (fileReader != null) {
37+
try {
38+
fileReader.close();
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
}
44+
return null;
45+
}
46+
47+
public static String readFileUsingTryWithResources(String path) {
48+
try (FileReader fileReader = new FileReader(path)) {
49+
return readAllCharactersOneByOne(fileReader);
50+
} catch (IOException e) {
51+
e.printStackTrace();
52+
}
53+
return null;
54+
}
55+
56+
57+
}

0 commit comments

Comments
 (0)