Dung
Dung

Reputation: 81

Java In and Out

I am newbie to Java. I have this exercice from school to create a BasicIO class and Main class to read and write to a file .

However my code only reads the first sentence and prints it in an infinite loop.

BasicIO.java

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

public class BasicIO 
{

    BasicIO()
    {
        line = null;
    }

    public void readplzthx(String filename) throws IOException
    {
        FileReader f = null;
        BufferedReader rd = null;

        f = new FileReader(filename);
        rd = new BufferedReader(f);

        line = rd.readLine();   
        rd.close();
        f.close();
    }

    public void writeplzthx(String filename)
    {

    }
    //String fn;
    String line;
}

Main.java:

import java.io.File;
import java.io.IOException;

public class Main {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException
    {
        File f = new File("test.txt");
        BasicIO io = new BasicIO();
        //io.readplzthx(f.getAbsolutePath());
        //File f = new File(args[1]);
        io.readplzthx(f.getAbsolutePath());
        do
        {
        //  io.readplzthx(f.getAbsolutePath());
            System.out.println(io.line);
        } while (io.line != null);
    }   

}

Upvotes: 3

Views: 255

Answers (5)

Barry
Barry

Reputation: 2063

The reason that you're having trouble is that you open the file, read in the first line, and close it. You need to keep the file open between calls to readplzthx and clean up after yourself when you're done.

Upvotes: 1

Vivin Paliath
Vivin Paliath

Reputation: 95498

In your readplzthx method, you're opening the file, reading in a line and then closing the file. So that's the first problem. This is why you're only getting one line.

You're setting the class's package-private member line to the line you read from the file. Your do-while loop is based on the condition that io.line is not null. This will always hold true. This is why you're in an infinite loop.

So here are some hints for your BasicIO class:

  • Write explicit methods to open and close the file/stream.
  • Write a state-inspection method that tells you whether you are at the end of the file, or if there is more content to read. You can use this in your while loop.
  • Write a method that returns a line from the currently-open file; you can use this inside your loop.

All you need to do now is put these three pieces together.

Good luck!

Upvotes: 0

Guillaume
Guillaume

Reputation: 5555

You re-load the file at each loop iteration. You should create the reader before the loop, then call reader.readLine() in the loop iteration so that your io.line value isn't always the first line.

Upvotes: 1

nmichaels
nmichaels

Reputation: 50943

    do
    {
    //  io.readplzthx(f.getAbsolutePath());
        System.out.println(io.line);
    } while (io.line != null);

That's your infinite loop. io.line doesn't change inside the loop.

Even when that's fixed, though, you'll still have the same problem. You only want to open the file once, then read from it repeatedly.

Upvotes: 2

NPE
NPE

Reputation: 500167

Three hints:

  • readplzthx shouldn't re-open the file every time it's called;
  • call readplzthx inside the loop so that you read a new line on each iteration;
  • make sure you correctly handle reaching the end of your input file.

Upvotes: 1

Related Questions