Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, the fastest class to read from a txt file [closed]

Tags:

java

io

java-io

I have to read txt files in my program. Im currently using FileReader and BufferedReader. I tried to use Scanner but its slower than FileReader and BufferedReader. Is there any class, which can read files faster ? It must be written in Java Language.

I need to read all words(strings splited by white space) from text file

like image 864
user1736332 Avatar asked Nov 20 '12 18:11

user1736332


1 Answers

Assuming you read all the file in memory, the fastest, from a code writing perspective, is:

List<String> lines = Files.readAllLines(yourFile, charset);

I would expect performance, from an execution perspective, to be as good if not better (this has supposedly been optimised by the team who wrote it).

You can then split or do whatever you need.

like image 93
assylias Avatar answered Oct 22 '22 18:10

assylias