PDA

View Full Version : BufferedReader doesn't finds my file :S




Zizou91
Oct 24, 2009, 04:32 PM
hi everybody! I'm new in this world of java and I've been trying to make my program get a string from a txt file that i have. Look here is my code:

public class Main {

private static BufferedReader stdIn = new
BufferedReader(new InputStreamReader(System.in));
private static PrintWriter stdOut = new
PrintWriter(System.out,true);
private static PrintWriter fileOut;

private static BufferedReader fileIn;

public static void main(String args[]) throws IOException
{
String a;
fileIn=new BufferedReader(new FileReader("Regitros.txt"));
a=fileIn.readLine();
System.out.println(a);
fileIn.close();
}

This is the error that it shows me:

Exception in thread "main" java.io.FileNotFoundException: Regitros.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)

I have that Registros.txt in the same folder that I have the Main.java but it ain't still working :S any idea or how can I specify the file path, I'm using a Macbook I think that the path to something differ from the paths of windows since in windows there is a C:/ and here there's the Macintosh HD so pleasee help !! for getting my file read.



chown33
Oct 24, 2009, 04:52 PM
How are you compiling your code: command-line, Xcode, something else?

How are you running the compiled class file: command-line, Xcode, something else?


In Java, the current directory is listed in the "user.dir" system property. You can print this value:

System.out.println( System.getProperty( "user.dir" ) );

If it shows a directory other than where your file is located, you have to change what the currentt directory is. How to do that depends on how you're running your Java program.

Zizou91
Oct 24, 2009, 05:11 PM
I'm using netbeans and mm in all my other programs I've been using that System.out.println for showing something on the display, I think that the problem is with the bufferedreader that doesn't find my file :S

chown33
Oct 24, 2009, 05:19 PM
The problem is not the BufferedReader. Read the exception:

Exception in thread "main" java.io.FileNotFoundException: Regitros.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)


The class that has the problem is FileInputStream. There should be additional lines that show the other classes involved.

Read the docs for the FileInputStream or FileReader constructor, and notice what it says about the conditions that will throw exceptions. Basically, if the pathname doesn't refer to a readable file, it will throw a FileNotFoundException.

Since the pathname you gave, "Regitros.txt", is a relative path, it must be located in the current directory. I already gave code that prints the current directory. If you don't see the directory where "Regitros.txt" is located, then you have to use an absolute pathname to where the file is located.

Zizou91
Oct 24, 2009, 05:39 PM
Ohh thanks for that of the directory, it prints me:

/Users/zizou/NetBeansProjects/Javadosier

I tried moving the txt file to that folder but doesn't find the file :S I've tried both using

fileIn=new BufferedReader(new FileReader("/Users/zizou/NetBeansProjects/Javadosier/Regitros.txt"));

and the short one

fileIn=new BufferedReader(new FileReader("Regitros.txt"));

I really don't know what to do :S The things of the exception i don't get them very well :S Doesn't the throws IOexception fixes that problem?? :S

Zizou91
Oct 24, 2009, 06:03 PM
HEYYY!!! I FOUND THE PROBLEMMMM !!!! THANKS ALOT :D you know the only problem was as you told me that maybe it was not a readable file so i chek it out and it was like something wreird on it when i preview it, i think that it's because i used textedit to create that file and because it was a rft file i only changed it's extension writing it at the end of the filename that created some wreird codes inside, then i went to word office and created a normal txt file and it worked THANKS ALOT :D

chown33
Oct 24, 2009, 07:01 PM
I'm glad you solved the pathname and file-contents problems.

Here's how to save as plain text in TextEdit.app.

1. Choose New from File menu.
2. Choose Make Plain Text from Format menu.
3. Type, paste, or drag-n-drop text into the window.

When you save the file, it will save as plain text.

You can also use Format > Make Plain Text on an existing file, then save it as plain text.

Finally, you can default to Plain Text for new documents.

1. Choose Preferences from the TextEdit menu.
2. Click the New Document tab, if it's not already chosen.
3. Click the Plain Text radio button.

I have Plain Text as my default, because I edit many more plain text files than RTF ones.