I have a long local text file and I would like to write a method that will read it and add the first word of each line into an ArrayList of Strings.
I have a very primitive understanding of basic I/O operations. Right now, from what I understand, I would need to have some kind of InputReader object read each first word and then use something like an OutputStreamBuffer to add each of those words as Strings to the ArrayList in turn. Am I on the right track?
If that is correct, I’m not exactly sure what the correct syntax would be to do it (especially with having the InputStreamReader go to the next line after copying the first word in each line. If I’m not even close, what would you guys do?
Thanks a lot for your help everyone. I hope my description was clear enough for you.
You’re on the right track – here’s my general suggestion for your requirement…
ArrayList<String>for storing your wordsBufferedReaderto read from your file line-by-line usingreadLine();String.split()method or aStringTokenizeror a regex expression)ArrayListusing anadd()method and then read the next line from point 2.There shouldn’t be a need to use an
OutputStreamfor yourArrayList, thats just complicating things.