aspose file tools*
The moose likes I/O and Streams and the fly likes File Not Found Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "File Not Found Error" Watch "File Not Found Error" New topic
Author

File Not Found Error

Steve De Costa
Ranch Hand

Joined: Jun 04, 2007
Posts: 33
I am a beginner who just started reviewing.refreshing my java skills again. I tried coding a simple thing that would read input from a file and print the output.....


I am using eclipse (which I am also finding a little complicated) and I the code and output are given below. I am currently in the workspace where my code and file are located. Instead of typing in the entire pathname, I tried just the file name as well and I am still getting this error.

Can anyone please help?


========================================================================



public class File_IO {

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
File myFile = new File("C:\\Documents and Settings\\user\\My Documents\\Finance\\Forex\\Try2\\JavaRestart\\EURUSD.txt");
Scanner tokenizer;
try
{
tokenizer = new Scanner(myFile);
while (tokenizer.hasNext())
{
System.out.println(tokenizer.next());
}
tokenizer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

=====================================================


java.io.FileNotFoundException: C:\Documents and Settings\user\My Documents\Finance\Forex\Try2\JavaRestart\EURUSD.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at File_IO.main(File_IO.java:26)
Steve De Costa
Ranch Hand

Joined: Jun 04, 2007
Posts: 33
Tried the following code as well but I am getting the same problem

===============================================================================// Displays directory listing using regular expressions.
// {Args: "D.*\.java"}
import java.io.*;
import java.util.*;


public class DirList
{


public static void main(String[] args) throws IOException
{
File f = new File("EURUSD.txt");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
String record = null;

try
{
while ( (record=dis.readLine()) != null )
{
System.out.println(record);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
=======================================================================






Exception in thread "main" java.io.FileNotFoundException: EURUSD.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at DirList.main(DirList.java:14)


=====================================================================

but the file EURUSD.txt is there
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Nicknames and "handles" don't cut it. You can change your display name here. Thanks!

As far as your problem goes: FileNotFoundException is awfully clear: as far as the Java program is concerned, the file isn't there. When you're running under an IDE, the problem is generally that the current directory isn't where you think it is. The solution is to use an absolute path (when you did try this, you likely made a slight mistake; try again!) or to find out where the current directory actually is. Try something like

System.out.println(new File(".").getAbsolutePath());

which will tell you exactly.


[Jess in Action][AskingGoodQuestions]
Steve De Costa
Ranch Hand

Joined: Jun 04, 2007
Posts: 33
Thanks,

That helps. Though here is the funny thing. I specified in the workspace that the path should be
C:\Documents and Settings\User\My Documents\Fixed\FoMex\Try2\JavaRestart\

but when I did

System.out.println(new File(".").getAbsolutePath());

I got that the path is

C:\Documents and Settings\User\My Documents\Fixed\FoMex\Try2\JavaRestart\Proj2

why would that be. Is it because the files are located in Proj2 folder?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Not Found Error