-
January 1st, 2013, 02:51 AM
#1
Program isn't reading my .txt file
Greeting,
I’m using netbeans on a MacBook. I’ve written a small program that reads a text file. However, the program isn’t reading the file. I’m receiving my FileNotFoundExepection error message. Where can I place my out.txt file so that the program is able to locate the file in this simple code below.
public class TextFileInputDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String fileName = "out.txt";
Scanner inputStream = null;
System.out.println("The file " + fileName +
"\ncontains the following lines:\n");
try
{
inputStream = new Scanner(new File(fileName));
}
catch (FileNotFoundException e)
{
System.out.println("Error opening the file " + fileName);
System.exit(0);
}
while (inputStream.hasNextLine());
{
String line = inputStream.nextLine();
System.out.println(line);
}
inputStream.close();
}
}
-
January 1st, 2013, 03:41 PM
#2
Re: Program isn't reading my .txt file
You either need to use a fully qualified path or put it in the current working directory. There is probably a netbeans setting specifying where this is or you can print out the system property "user.dir" or you can print out the absolute path of the File object you are creating in you program and that will show where it expects out.txt to be.ie
Code:
System.out.println(new File("out.txt").getAbsolutePath());
-
January 1st, 2013, 04:37 PM
#3
Re: Program isn't reading my .txt file
Thanks keang, although your method of code didn't work for me, the language in your explanation helped me to further my online research and I can up with this solution:
Click on File menu.
Click on Project Properties.
In the categories, select Run.
In main class you select your current java file.
In Arguments select the file you want to read for e.g. abc.txt or abc.java
And in Working Directory write down the path of folder in which this abc.txt or abc.java lies.
Click OK to close Project Properties.
While running your program don't forget to select your project as Main Project.
Then click F^ on keyboard. i.e. You have to raun your main project instead of just running your current java file. That's it....enjoy!!!!
It is probably your same solution, but until I start understanding the concepts of Java, I'll continue fit a square peg into this round hole...lol. Again Thanks.
-
January 1st, 2013, 05:02 PM
#4
Re: Program isn't reading my .txt file
Glad to hear you have sorted it.
The line of code I provided was for insertion into your program at the start of the main method. It will print out the fully qualified path for the directory = "/out.txt". This is where it expects the file to be.
-
January 1st, 2013, 08:02 PM
#5
Re: Program isn't reading my .txt file
Wow, I inserted that piece of code everywhere except where you just mentioned. Now I've learned two different methods of coding in this forum in less than 24 hours. This site is a blessing.
Thanks keang...once again.
Hey, how long have you been coding Java?
-
January 2nd, 2013, 05:17 AM
#6
Re: Program isn't reading my .txt file
I've been coding in Java pretty much since it first came out in 1996 but was writing code in other languages for about 16 years before that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|