Click here to Skip to main content
11,930,069 members (52,358 online)
Rate this:
Please Sign up or sign in to vote.
See more: Java
Im have created a test class and a binary search tree to parse a text file and add it to a search tree.But I'm getting the following errors when reading the text file:
Exception in thread "main" PoblachtNaHEireann.txt (The system cannot find the file specified)
    at Method)
    at<init>(Unknown Source)
    at<init>(Unknown Source)
    at TestRunner.main(

This is my input code snippet:
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("PoblachtNaHEireann.txt")));
			Scanner input = new Scanner("PoblachtNaHEireann.txt");
			while(input.hasNext()) {
			    String nextToken =;
			    //or to process line by line
			    String nextLine = input.nextLine();
			BinarySearchTree bst = new BinarySearchTree(3);
			FileReader input1 = new FileReader("PoblachtNaHEireann.txt");
			BufferedReader bufRead = new BufferedReader(input1);
			String myLine = null;
			ArrayList<string> array = new ArrayList<string>();
			int i = 0;
			while ( (myLine = bufRead.readLine()) != null)
				String[] tmpArray = myLine.split(" ");
				for(int j = 0; j<tmparray.length;>				{
Posted 23-Apr-13 2:52am
Edited 23-Apr-13 3:43am
Prasad Khandekar 23-Apr-13 7:58am
Where is this file located? Try giving absolute path. First line is unnecessary.
johngorman1 23-Apr-13 8:01am
It is located in the project folder.
Richard MacCutchan 23-Apr-13 8:04am
What is the error? You did not include the first line(s) that actually explains what went wrong.
johngorman1 23-Apr-13 8:44am
The error is source not found but I have the file pasted into my source folder.
Prasad Khandekar 23-Apr-13 9:06am
Are u running the class from command line? If so try including ,; in the classpath and place the file in root of the project from where you are execuing the class.
e.g. let's asume that the the class to be tested is in a jar file named testme.jar as it is located in C:\javatests folder.
Then your command will be

c:\javatests>java -cp.;testme.jar

The file will be located in c:\javatests folder.

Richard MacCutchan 23-Apr-13 9:58am
It's the data file that it cannot find; nothing to do with classpath.
Richard MacCutchan 23-Apr-13 9:57am
That message could no be clearer, the system cannot find the file, most probably because it is in a different directory from your java class(es). Use Windows Explorer to move it to the correct place.
johngorman1 23-Apr-13 11:01am
I have placed the text file in the project folder bin.But I'm still getting the same errors.
Richard MacCutchan 23-Apr-13 11:40am
I have just run a few tests, and you need to have the file in the same directory that you start in when you execute the java program. This will most likely be the one above bin.
Sergey Alexandrovich Kryukov 23-Apr-13 11:27am
Not a question, anyway.
Richard MacCutchan 23-Apr-13 11:40am
Really? It seems quite clear to me.
Sergey Alexandrovich Kryukov 23-Apr-13 14:22pm
I would agree with you if you say that you can correctly answer to this post as it was a question. I believe you could. But practically... I would recommend to ignore the posts not formulated as questions, as we are just overwhelmed by them. Why encourage people to be so sloppy? "I'm getting the following error..." so what?..
Thank you for understanding.
Richard MacCutchan 23-Apr-13 15:39pm
Fine, you do it your way. But, telling people that you don't think their query is a valid question is not ignoring them. And also, at the end of the day, no one is overwhelmed, we all answer as many or as few as we like. Unless, somehow you are getting paid by the question.
Sergey Alexandrovich Kryukov 23-Apr-13 17:41pm
Well, I feel this way. I think an extremely sloppy question is answered, in a way, at the expense of other inquirers, those who deserve more attention. Do you see my point?
But of course we should consider each other free to do what we think is right, as soon as we don't do harm.

Thank you for your opinion.
Richard MacCutchan 24-Apr-13 3:48am
How can it be at the expense of anyone, this is a free site and support is offered by volunteers? If I can help with a query then I will try to do so. And if that means I do not get to answer "a more deserving question", then that is no problem as someone else will probably look at it. Maybe if you spent less time adding these comments in what you call "bad questions", you would have more time to answer the good ones.
I don't even know, maybe...
Richard MacCutchan 23-Apr-13 12:02pm
Having looked a bit further than your base problem I can see quite a lot of other things wrong with the above. I may be wrong, but this program looks like the result of a lot of copy and paste operations, rather than you writing the code.
johngorman1 23-Apr-13 15:07pm
Ya its just a rough template at the moment but I'm still falling at the file io.I placed the file in the folder above bin but still no joy.
Richard MacCutchan 23-Apr-13 15:37pm
I said "most likely", not definitely. Because I have no idea what directory you are starting the program from, or indeed whether you are starting it from the command line or not.
johngorman1 23-Apr-13 16:02pm
oh I'm starting it from eclipse debugger not command line.
Richard MacCutchan 23-Apr-13 16:48pm
See solution below.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

OK, I have run this in eclipse with the text file in the directory one above bin, and it works fine. I did notice that your code above has a problem in your setup of your Scanner object.
// you have this, which scans the string "PoblachtNaHEireann.txt"
Scanner input = new Scanner("PoblachtNaHEireann.txt");
// it should be as follows, to read a file
Scanner input = new Scanner(new File("PoblachtNaHEireann.txt"));
johngorman1 23-Apr-13 17:07pm
tried that but still get the same error?
Richard MacCutchan 24-Apr-13 5:00am
Add the following two lines to your program:

File dotsource = new File(".");

That will display the current working directory of your program, and that is where the file should be placed.
johngorman1 24-Apr-13 5:50am
I tried the above snippet before my BufferedReader but I still get the same error.I have placed my text file in project package but its still not finding the file.Could this be a problem with the method I'm reading in this file?
Richard MacCutchan 24-Apr-13 7:42am
I don't think you quite understand the problem. It does not matter whether the file is in your project or not, what is important is its physical location in the Windows filesystem. You need to use Windows Explorer to make sure the file is physically in the correct directory, as described above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100