0

I am trying to save a file line by line as a string array, but I am a beginner and confused.

package com.java24hours;

import java.io.*;

public class ShorteningVelocity {
    public static void main(String[] arguments) throws IOException {

        FileReader SVfile = new FileReader(new File("C:\\Documents\\10-27-15110mMKPSS3.dat"));
        BufferedReader br = new BufferedReader(SVfile);
        String temp = br.readLine();
        while (temp != null) {
            temp = br.readLine(); //reads file line by line 
            System.out.println();
        }
    }
}
2
  • 1
    As a beginner with your questions I'm also confused ... what is your question? Where do you have a problem? Commented Nov 6, 2015 at 17:53
  • 2
    Use Files.readAllLines(). Commented Nov 6, 2015 at 17:54

3 Answers 3

2

Java Arrays can't grow in size, so you use a list (from java.util). The lists grow dynamically, so you can use add() as often as you want. As a list can potentially hold everything, you specify what you want it to contain by using List<TypeOfWhatYouWantToStore>. The List class is called a generic class because of that.

Converting it to an array (because that is what you wanted) is a bit strange. You allocate the array with the size of the list and then you use toArray on the list. This returns the list, converted as an array. It must take the array as a parameter, because the compiler needs that when compiling with generics.

package com.java24hours;

import java.io.*;
import java.util.*;

public class ShorteningVelocity {
    public static void main(String[] arguments) throws IOException {

        FileReader SVfile = new FileReader(new File("C:\\Documents\\10-27-15110mMKPSS3.dat"));
        BufferedReader br = new BufferedReader(SVfile);
        String temp = br.readLine();
        List<String> tmpList = new ArrayList<>();
        while (temp != null) {
            tmpList.add(temp);
            temp = br.readLine(); //reads file line by line 
        }
        String[] myArray = new String[tmpList.size()];
        myArray = tmpList.toArray(myArray);
    }
}

EDIT: Using Files.readAllLines() (see comments on your question) is easier and potentially faster, but using this loop, you can see more of what's going on.

EDIT 2: It is more common use this loop:

        String temp;
        List<String> tmpList = new ArrayList<>();
        while ((temp = br.readLine()) != null) { 
            tmpList.add(temp);
        }

The loop is now doing the following:

  • Read br.readLine() into temp
  • If temp is null, leave the loop
  • If temp is not null, add it to the list

You can also print the array by doing:

for (String str : myArray) {
    System.out.println(str);
}
Sign up to request clarification or add additional context in comments.

3 Comments

"It is more common to swap the lines inside the loop" It is "more common" to add null as the last element to the list? You should use a proper while loop instead.
Oh right.... fixing it
@Tom should be okay now
0
public static void main(String[] args) {
        try {
            FileWriter fw = new FileWriter("C:\\Documents\\10-27-15110mMKPSS3-out.dat");
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter outFile = new PrintWriter(bw);

            FileReader SVfile = new FileReader(new File("C:\\Documents\\10-27-15110mMKPSS3.dat"));
            BufferedReader br = new BufferedReader(SVfile);
            String temp;
            while ((temp = br.readLine()) != null) {
                //reads file line by line 
                System.out.println();
                outFile.println(temp);
            }
            outFile.close();
        } catch (IOException e) {
        }
    }

2 Comments

This is great thank you!!
ChibiChibi, if my answer is the best, can you check my answer please?
0
public static void main(String[] args){
    List<String> tmpList = new ArrayList<>();
    try {
        FileReader fileReader = new FileReader(new File("D:\\input.txt"));
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String temp;

        while ((temp = bufferedReader.readLine()) != null) {
            tmpList.add(temp);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String[] myArray = new String[tmpList.size()];
    myArray = tmpList.toArray(myArray);
    for(int i = 0; i< myArray.length ; i ++){
        System.out.println(myArray[i]);
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.