HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaCritical

Reading a plain text file in Java

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
readingjavatextfileplain

Problem

It seems there are different ways to read and write data of files in Java.

I want to read ASCII data from a file. What are the possible ways and their differences?

Solution

ASCII is a TEXT file so you would use Readers for reading. Java also supports reading from a binary file using InputStreams. If the files being read are huge then you would want to use a BufferedReader on top of a FileReader to improve read performance.

Go through this article on how to use a Reader

I'd also recommend you download and read this wonderful (yet free) book called Thinking In Java

In Java 7:

new String(Files.readAllBytes(...))


(docs)
or

Files.readAllLines(...)


(docs)

In Java 8:

Files.lines(..).forEach(...)


(docs)

Code Snippets

new String(Files.readAllBytes(...))
Files.readAllLines(...)
Files.lines(..).forEach(...)

Context

Stack Overflow Q#4716503, score: 647

Revisions (0)

No revisions yet.