user1058210
user1058210

Reputation: 1689

Reading in text file in Java

I wrote some code to read in a text file and to return an array with each line stored in an element. I can't for the life of me work out why this isn't working...can anyone have a quick look? The output from the System.out.println(line); is null so I'm guessing there's a problem reading the line in, but I can't see why. Btw, the file i'm passing to it definitely has something in it!

public InOutSys(String filename) {
    try {
        file = new File(filename);
        br = new BufferedReader(new FileReader(file));
        bw = new BufferedWriter(new FileWriter(file));
    } catch (Exception e) {
        e.printStackTrace();
    }
}



public String[] readFile() { 

    ArrayList<String> dataList = new ArrayList<String>();   // use ArrayList because it can expand automatically
    try {
        String line;

        // Read in lines of the document until you read a null line
        do {
            line = br.readLine();
            System.out.println(line);
            dataList.add(line);
        } while (line != null && !line.isEmpty());
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    //  Convert the ArrayList into an Array
    String[] dataArr = new String[dataList.size()];
    dataArr = dataList.toArray(dataArr);

    // Test
    for (String s : dataArr)
        System.out.println(s);

    return dataArr; // Returns an array containing the separate lines of the
    // file
}

Upvotes: 1

Views: 2001

Answers (4)

user605331
user605331

Reputation: 3788

I'm not sure if you're looking for a way to improve your provided code or just for a solution for "Reading in text file in Java" as the title said, but if you're looking for a solution I'd recommend using apache commons io to do it for you. The readLines method from FileUtils will do exactly what you want.

If you're looking to learn from a good example, FileUtils is open source, so you can take a look at how they chose to implement it by looking at the source.

Upvotes: 1

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236170

There are several possible causes for your problem:

  • The file path is incorrect
  • You shouldn't try to read/write the same file at the same time
  • It's not such a good idea to initialize the buffers in the constructor, think of it - some method might close the buffer making it invalid for subsequent calls of that or other methods
  • The loop condition is incorrect

Better try this approach for reading:

try {
    String line = null;
    BufferedReader br = new BufferedReader(new FileReader(file));
    while ((line = br.readLine()) != null) {
        System.out.println(line);
        dataList.add(line);
    }
} finally {
    if (br != null)
        br.close();
}

Upvotes: 0

julian0zzx
julian0zzx

Reputation: 263

First, you open a FileWriter once after opening a FileReader using new FileWriter(file), which open a file in create mode. So it will be an empty file after you run your program.

Second, is there an empty line in your file? if so, !line.isEmpty() will terminate your do-while-loop.

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 692291

You're using a FileWriter to the file you're reading, so the FileWriter clears the content of the file. Don't read and write to the same file concurrently.

Also:

  • don't assume a file contains a line. You shouldn't use a do/while loop, but rather a while loop;
  • always close steams, readers and writers in a finally block;
  • catch(Exception) is a bad practice. Only catch the exceptions you want, and can handle. Else, let them go up the stack.

Upvotes: 1

Related Questions