Reputation: 11
Whenever I am compiling the code it is showing this error. It is a simple java code to copy a file. I have saved the text file in src
and in workspace both. I tried in both ways by giving directory of txt file but i am getting the same error:
Exception in thread "main" java.io.FileNotFoundException: src\input.txt (The system cannot find the path specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.io.FileInputStream.<init>(Unknown Source) at CopyFile.main(CopyFile.java:15)
Here is the code:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public static void main(String [] args) throws IOException{
FileInputStream in = null;
FileOutputStream out = null;
try
{
in = new FileInputStream(" src/input.txt");
out = new FileOutputStream("src/output.txt");
int c;
while ((c=in.read()) != -1)
{
out.write(c);
}
} finally
{
if (in != null)
{
in.close();
}
else if (out != null)
{
out.close();
}
}
}
}
Upvotes: 1
Views: 122
Reputation: 10604
The space in:
in = new FileInputStream(" src/input.txt");
is probably messing up your path, even if the file really is in src/input.txt
. Delete the space, and check to make sure that the file is actually where you think it is.
Upvotes: 2
Reputation: 46657
This is not a compilation error, it is a runtime error.
You should double-check the working directory in which the application executes (since src/input.txt
is a relative path). Oftentimes, using Eclipse or other IDEs it is not what you might expect (i.e. relative to project source, or binaries, ...) but it can be configured in the project settings.
Upvotes: 0