Hi, so my problem is I don't know how to read more than one line from a text file.

say I have string.txt

and it contains:

Hello
Java
Hello
World
Welcome

how am I going to read every line of the string.txt? I tried using FileReader and BufferedReader but it only reads the first line using readLine(). And it should work even if I add more lines in the txt file.


Here's what I have so far..

public void readData ()
    {
        try
        {
            FileReader dataIn = new FileReader ("string.txt");
            BufferedReader buffDataIn = new BufferedReader (dataIn);

            while (true)
            {
                words = buffDataIn.readLine ();
                if (words == null)                
                    break;
                
            }                

            c.println ("The current strings in the files are \"" + words + "\"");

        }
        catch (IOException e)
        {
            new Message ("Error");
        }
    }

Recommended Answers

All 14 Replies

What you have written there should read multiple lines just fine, but you're replacing 'words' each time, so it is going to end up null at the end.

Right so I changed it to do this and it works

c.println ("The current words in the files are:");
            c.println ();

            while ((words = buffDataIn.readLine ()) != null)
            {
                c.println (words);
            }

            c.println ();

Now my problem is I don't know how to split the Strings ('words')per line.

"Split" how? If you just need to store each line separately, use an ArrayList.

If you need to further split up multiple words in a line, look at String.split().

So I haven't learned about ArrayList yet, but I tried it but it errors.

while ((words = buffDataIn.readLine ()) != null)
            {
                c.println (words);
                ArrayList list = new ArrayList ();
                list.add (words);
                c.println (list);
            }

You would want to define the list outside of your loop, since you will add to it each line read.

You'd need to clarify "errors" a bit if you want more advice on that one.

while ((words = buffDataIn.readLine ()) != null)
            {
                c.println (words);

            }

            ArrayList list = new ArrayList ();
            list.add (words);
            c.println (list);

So I tried putting outside the loop. But there's an error on the print command saying "No applicable overload for the method named "println" was found in type "hsa.ConsoleParent". Perhaps you wanted the overloaded version "void println(boolean $1);" instead?"

So your console output class has no method to print out an entire ArrayList. You'll need to iterate through the list and print each element then.

How do I do that? Do I just make a loop to print them?

Yes, if you need to print them then just loop through the collection and print each.

I would point out that printing them to the console doesn't really have anything to do with reading them. I'd make a separate method for output to maintain a separation of responsibilities. readData() should just read data.

Right, thanks a lot. Now my problem is how do I write into a file line by line.

Say I want to write the following words: hello world hello java text file

I want to write it in a text file like this:

hello
world
hello
java
text
file

sort of like println? but write them instead..

You can use PrintWriter or FileWriter for that.

but when I use write() it's only on one line so like say it will be helloworldhellojavatextfile

Look over the APIs for those two classes. There are methods to print new lines.

BufferedWriter is another option.

Oh sorry, I was just looking at PrintWriter, BufferedWriter has newLine(). Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.