Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
XML
Exception in thread "main" java.io.FileNotFoundException: PoblachtNaHEireann.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at TestRunner.main(TestRunner.java:20)


This is my input code snippet:
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("PoblachtNaHEireann.txt")));
		
                try 
		{
			Scanner input = new Scanner("PoblachtNaHEireann.txt");
			
			processLineByLine(input);
			
			while(input.hasNext()) {
			    String nextToken = input.next();
			    if(!nextToken.isEmpty())
			    	wordList.add(nextToken);
			    	
			    //or to process line by line
			    String nextLine = input.nextLine();
			}
			input.close();
			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;>				{
					array.add(tmpArray[j].toUpperCase());
					bst.insertCharacters(tmpArray[j].toUpperCase());
				}
			    i++;</string></string>
Posted
Updated 23-Apr-13 2:43am
v5
Comments
Prasad Khandekar 23-Apr-13 7:58am    
Where is this file located? Try giving absolute path. First line is unnecessary.
[no name] 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.
[no name] 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 com.foo.TestClass

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

Regards,

1 solution

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.
Java
// 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"));
 
Share this answer
 
Comments
[no name] 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(".");
System.out.println(dotsource.getCanonicalPath());

That will display the current working directory of your program, and that is where the file should be placed.
[no name] 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900