3

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.

Community
  • 1
  • 1
Exagon
  • 4,798
  • 6
  • 25
  • 53

1 Answers1

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`? If yes was only purpose of that list to create your other list or do you need it also somewhere else? – Pshemo Nov 12 '15 at 22:30
  • 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
  • 2
    You 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