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.

JavaProgramming Languages-Other

Avatar of undefined
Last Comment
Mick Barry

8/22/2022 - Mon

Use line.split() on the following

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
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
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.

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

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.

Your help has saved me hundreds of hours of internet surfing.

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

Mick Barry

the following shows what u need:


to convert list to array use:

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