-2

Looking for best practice to read a file line by line which has >10 records and storing it in ArrayList.

My program was able to read to 3.5k records and ignoring rest of the records.

URL cityurl = ClassLoader.getSystemResource(citypath);
citybr = new BufferedReader(new FileReader(cityurl.getFile()));
for (String city = citybr.readLine(); city != null; city = citybr.readLine()) {
    citycountryairport.add(citybr.readLine());
}

Thanks in advance!!

venkatj
  • 1
  • 4
  • It might be helpful if you identify what you don't like about your current solution. Is it running out of memory? Something else? – StvnBrkdll Sep 18 '16 at 15:51
  • 2
    Possible duplicate of [Read large files in Java](http://stackoverflow.com/questions/2356137/read-large-files-in-java) – Michał Szewczyk Sep 18 '16 at 15:55
  • Hint: read about java naming conventions. Variables go camelCase! – GhostCat Sep 18 '16 at 15:56
  • You can eliminate duplicate code by changing loop to `for (String city; (city = citybr.readLine()) != null; )`. That also more closely resembles the more common `while ((city = citybr.readLine()) != null)` loop used for `readLine()` processing. – Andreas Sep 18 '16 at 16:10

1 Answers1

1

BufferedReader is a good choice for reading large files because it buffers the file and thus avoids loading the whole file into memory, see BufferedReader Doc.

Each time you call

readLine();

The next line of the file is read, in your code change :

citycountryairport.add(citybr.readLine());

to :

citycountryairport.add(city);

otherwise the lines read by the line

city = citybr.readLine()

will not be added to your list because you never add the String city to your list.

MrCola
  • 33
  • 6
  • A little more explanation might be good, but very nice observation, and welcome to StackOverflow. – Andreas Sep 18 '16 at 16:06
  • Thanks! I've made some edits, hopefully OP understands – MrCola Sep 18 '16 at 16:24
  • @Mwanza, My code isn't throwing any exception but it is printing null and ignoring rest of the records in case I use readLine() method.. but your suggestion worked perfect!! why is this happening with direct approach? a little more explanation will be nice. Thank you!! – venkatj Sep 18 '16 at 16:47
  • The records were not being ignored, you were reading the records using – MrCola Sep 18 '16 at 16:58
  • The records were not being ignored, remember that each time `readLine()` is called, the _next line_ of the file (if any) is read. For each iteration of your loop, you were reading a line using `city = citybr.readLine()`, the contents of this line were saved in the _city_ variable, you were not adding the city String to your list. You then called `citycountryairport.add(citybr.readLine())` , which read in the following line _and_ saved it to you list, hope this helps, maybe have a look at [for loop docs](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html), you're welcome :-) – MrCola Sep 18 '16 at 17:06