Read a text file line by line into strings

23,875

Solution 1

You should use an ArrayList.

File file = new File(fileName);
Scanner input = new Scanner(file);
List<String> list = new ArrayList<String>();

while (input.hasNextLine()) {
    list.add(input.nextLine());
}

Then you can access to one specific element of your list from its index as next:

System.out.println(list.get(0));

which will give you the first line (ie: Purlplemonkeys)

Solution 2

You can read text file to list:

List<String> lst = Files.readAllLines(Paths.get("C:\\test.txt"));

and then access each line as you want

P.S. Files - java.nio.file.Files

Solution 3

If you use Java 7 or later

List<String> lines = Files.readAllLines(new File(fileName));

for(String line : lines){
   // Do whatever you want
   System.out.println(line);
}

Solution 4

Sinse JDK 7 is quite easy to read a file into lines:

List<String> lines = Files.readAllLines(new File("text.txt").toPath())

String p1 = lines.get(0);
String p2 = lines.get(1);

Solution 5

You can use apache.commons.io.LineIterator

LineIterator it = FileUtils.lineIterator(file, "UTF-8");
 try {
   while (it.hasNext()) {
     String line = it.nextLine();
     // do something with line
   }
 } finally {
   it.close();
 }

One can also validate line by overriding boolean isValidLine(String line) method. refer doc

Share:
23,875
Cardinal System
Author by

Cardinal System

I am a passionate self-taught software developer with 7 years of experience in Java and a very natural affinity for software in general. I love what I do, and I love to learn! I was employed at MinebeaMitsumi as a part-time programmer in 2022 and I am completing my education in 2023.

Updated on July 09, 2022

Comments

  • Cardinal System
    Cardinal System almost 2 years

    How do I read the contents of a text file line by line into String without using a BufferedReader?

    For example, I have a text file that looks like this inside:

    Purlplemonkeys
    greenGorilla
    

    I would want to create two strings, then use something like this

    File file = new File(System.getProperty("user.dir") + "\Textfile.txt");
    String str = new String(file.nextLine());
    String str2 = new String(file.nextLine());
    

    That way it assigns str the value of "Purlplemonkeys", and str2 the value of "greenGorilla".

  • user1231232141214124
    user1231232141214124 over 8 years
    I don't think thats what he was looking for. line will be overwritten every time so its kind of pointless to even declare it. Why not just println(input.nextLine()) ? String line = ... is kind of redundant.
  • Jan
    Jan over 8 years
    You don't need to know it's an ArrayList. Coding List<String> arrayList = new ArrayList<String>(); is cleaner
  • Cardinal System
    Cardinal System over 8 years
    I'm using Eclipse. Next to arrayList.add(Input.nextLine()); there is an error that says Multiple markers at this line - Syntax error on token ")", ; expected - implements java.awt.event.ActionListener.actionPerformed - Syntax error on token "(", ; expected and next to File file = new File(fileName); there is an error that says fileName cannot be resolved to a variable This also causes errors in other parts of my class.
  • Cardinal System
    Cardinal System over 8 years
    Also, where do I put the path leading to the txt file?
  • Kevin Mee
    Kevin Mee over 8 years
    You put the filepath where fileName is. Like this: File file = new File("ThisIsYourFilePath.txt");
  • Cardinal System
    Cardinal System about 6 years
    My personal favorite approach :)
  • Saikat
    Saikat about 6 years
    The method readAllLines(Path) in the type Files is not applicable for the arguments (File)