Avatar of LocoTechCJ
LocoTechCJFlag for United States of America asked on

Java - Read file line by line

Using Java, I would like to read a plain text file line by line and populate an array, each line of the file representing an item in the array.  I have read several solutions here and online, but can't quite find the right way to do it.  Thanks in advance for the help.

-LTCJ
JavaProgramming Languages-Other

Avatar of undefined
Last Comment
Mick Barry

8/22/2022 - Mon
CEHJ

Use line.split() on the following
ASKER CERTIFIED SOLUTION
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
shmert

Just an optimization note: if you don't need all the text at once, it might make more sense to use a BufferedReader to read the file line by line.  That way, you don't need to load the entire file into memory (which might be an issue if the file is very large).  In addition, you can start processing the file right away, without waiting for the whole file to read.

Look at the BufferedReader.readLine() method for more info
ASKER
LocoTechCJ

I like the idea of using a list.  I'm trying it out, running into a few quirks.  I'll let you know how it goes in just a little bit.

LTCJ
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
LocoTechCJ

Well, the line-by-line read works.  I'll open another question for other problems. Thanks!  

LTCJ
Mick Barry

the following shows what u need:

http://www.objects.com.au/java/qa/908090699.html

to convert list to array use:

String[] array = (String[]) lines.toArray(new String[0]);