krankzzz
krankzzz

Reputation: 53

Looping every single lines with readLine();

I am currently stuck with my current code of printing my file out with only 1 line of code in it. I am trying to loop through every single line with readLine() but uanble to achieve it. Stuck with looping either the first row or last row of line in the file.

The purpose of this is to print out exactly the same file with this program but printing it out as a folder with other different files.

try
{
    String sCurrentLine;

    br = new BufferedReader(new FileReader("C:\\testsample.csv"));

    if((sCurrentLine = br.readLine()) != null)
    {
        String info = br.readLine();
        resultString += sCurrentLine.toString();

    }
            this.WriteToFile(resultString);

}

Upvotes: 0

Views: 51

Answers (2)

Masudul
Masudul

Reputation: 21961

You are reading only two lines. To read all lines you need while loop.

while((sCurrentLine = br.readLine()) != null)
{
    //String info = br.readLine();- > Remove this line.
    resultString += sCurrentLine.toString();

}

Upvotes: 1

Evan Williams
Evan Williams

Reputation: 406

    String info = br.readLine();

is not helping you. And you need while, not if

while((sCurrentLine = br.readLine()) != null)
{
    resultString += sCurrentLine.toString();

}

Upvotes: 3

Related Questions