i have a huge file with Strings. All of the lines should be added to a List in my code. Whats the fastest way to do this? i found this but i dont know how to use it to read line per line.
Asked
Active
Viewed 346 times
3
-
in the file are about 10.000.000 lines btw – Exagon Nov 12 '15 at 22:02
-
1Possible duplicate of [Read large files in Java](http://stackoverflow.com/questions/2356137/read-large-files-in-java) – Mage Xy Nov 12 '15 at 22:06
-
Are you saying you want all the lines to be read into a list? Have you got enough memory for that? – Klitos Kyriacou Nov 12 '15 at 22:07
-
Yes i have enough memory – Exagon Nov 12 '15 at 22:07
-
You will not be able to load everything in a List. You will get out of memory. – PM 77-1 Nov 12 '15 at 22:07
-
Using the example you posted, you can use `reader.readLine()` to read lines. – Evan LaHurd Nov 12 '15 at 22:08
-
110 million is not such a huge number, i don't understand the worries about memory. At 100 chars per line that's still just 1 GB. – Marko Topolnik Nov 12 '15 at 22:10
1 Answers
5
I suspect that the built-in solution should be fast enough:
List<String> allLines = Files.readAllLines(Paths.get("location/of/your/file"));
(assuming your JVM has enough memory)

David Conrad
- 15,432
- 2
- 42
- 54

Pshemo
- 122,468
- 25
- 185
- 269
-
this helped a lot thank you. what if i have to call a method on each of this Strings in th list (a Constructor) Class(String s) is there a fast way to do this in all in one and not to generate this List
before? – Exagon Nov 12 '15 at 22:27 -
@Exagon Are you trying to create `List
` based on `List – Pshemo Nov 12 '15 at 22:30`? If yes was only purpose of that list to create your other list or do you need it also somewhere else? -
yes thats the problem :\ right now i am solving it like this `List
list = Files.readAllLines(Paths.get(s)).parallelStream().map(c -> new MyClass(c)).collect(Collectors.toList())` but this takes ages – Exagon Nov 12 '15 at 22:32 -
2You can also use `Files.lines(Paths.get("location/of/your/file")).parallel()` if you want to create stream which will handle lines at time of reading, without having to create some temporary list. BTW `map(c -> new MyClass(c))` can be replaced with little shorter and for some more readable: `.map(MyClass::new)` – Pshemo Nov 12 '15 at 22:44
-
I doubt that you can speed it up much. Maybe instead of text file consider using database? Reading from text file requires checking each character to see if it is not end of line. DB should at least skip that part (at least I hope so). – Pshemo Nov 12 '15 at 22:54
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94987/discussion-between-exagon-and-pshemo). – Exagon Nov 12 '15 at 22:55
-
@Exagon Sorry, I am not DB expert so that was only an idea. I honestly can't help you much in chat (and I am little preoccupied now so I only answer for comments or very short questions). – Pshemo Nov 12 '15 at 22:58
-
To find out if it's your application that's taking a long time, or just the sheer act of reading a large file, you should time how long it takes just to read the file. E.g. on Windows, `copy filename nul` and on Linux `cp filename /dev/null`. Subtract this time from your application's time to see how much time your application uses. – Klitos Kyriacou Nov 12 '15 at 23:40