# 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.

## 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");

String line = null;

System.out.println(line);
}
}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 on other sites
Quote:
 Original post by Peter ConnI 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 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 on other sites
Quote:
 Original post by samozyou need to spell the file path out like "C:\\Stuff\\Places\\Things\\file.txt"

No, he doesn't. You can open files with relative paths off of the working directory.

##### 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628732
• Total Posts
2984436

• 25
• 11
• 10
• 16
• 14