Sign in to follow this  

Java File IO problem

This topic is 4076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya I am learning to program in java, but I am having a problem with File IO, my code is:
import java.io.*;

class FileIO{
    public static void main (String[] args){
        try {
            File myFile = new File("MyText.txt");
            FileReader fileReader = new FileReader(myFile);
            BufferedReader reader = new BufferedReader(fileReader);
            
            String line = null;
            
            while ((line= reader.readLine()) != null){
                System.out.println(line);
            }
            reader.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

I am using Netbeans IDE, and the file "MyText.txt" is in both the src folder and the build folder with the classes, yet for some reason I always get an exception thrown about not being able to find the file. Can anyone help with the problem. P.S. - I wrote the text file in the NetBeans IDe yet when I open it in notepad it has those funny boxes instead of newlines, does anyone know how to stop this? P.P.S. - How do you get your text coloured? Atm I am outputting to the command-line and I know it might be difficult to do this, but later I am planning on outputting to a textbox and surely that must have a function for coloured text?

Share this post


Link to post
Share on other sites
Quote:
Original post by Peter Conn
I am using Netbeans IDE, and the file "MyText.txt" is in both the src folder and the build folder with the classes, yet for some reason I always get an exception thrown about not being able to find the file. Can anyone help with the problem.

I don't know Netbeans too well, but there must be a run/launch configuration somewhere where you can set the working directory. By default it's probably the project root, try putting your text file there.

Quote:
P.P.S. - How do you get your text coloured? Atm I am outputting to the command-line and I know it might be difficult to do this, but later I am planning on outputting to a textbox and surely that must have a function for coloured text?

Coloured text to the command line can be done with ANSI colour codes if the command line shell supports it (most do).

If you want to show coloured text in some kind of Swing/AWT text box then the easiest way would be to write it as HTML and use something like a JEditorPane which automatically understands it.

Share this post


Link to post
Share on other sites
you need to spell the file path out like "C:\\Stuff\\Places\\Things\\file.txt"

and be sure to use \\ because you need an escape character when you use
I think there might be another way to define the directory like "..\\file.txt" but Im not positive . . .

if this is just for your personal use, just define the filepath based off your C drive

Share this post


Link to post
Share on other sites

This topic is 4076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this