Code Hungry
Code Hungry

Reputation: 4000

java.io.FileNotFoundException:

I am Trying to Copy Single file from Source to Destination using Java but getting following Error message.

java.io.FileNotFoundException: Following is the method

public void copy_single(String source,String dest,String filename)
{
  try
  { System.out.println(source + "" + filename);
    System.out.println(dest + "" + filename);
    File inputFile = new File(source+""+filename);
    File outputFile = new File(dest+""+filename);
    Process proc0 = Runtime.getRuntime().exec("chmod -R 777 "+inputFile+"");
    proc0.waitFor();
    Process proc1 = Runtime.getRuntime().exec("chmod -R 777 "+outputFile+"");
    proc1.waitFor();
    FileReader in = new FileReader(inputFile);
    FileWriter out = new FileWriter(outputFile);
    int c;
    while ((c = in.read()) != -1)
      out.write(c);
    in.close();
    out.close();
  } catch(Exception e) {
    e.printStackTrace();
    System.out.println("Error: Operation failed!");
  }
}

Output:-

/home/root/Tool/AAputDelta.sh
/home/root/Desktop/Sqa/BaseLine/Engine/AAputDelta.sh
java.io.FileNotFoundException: /home/root/Desktop/Sqa/BaseLine/Engine/AAputDelta.sh (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
at java.io.FileOutputStream.<init>(FileOutputStream.java:145)

How to copy simple file using java.

Upvotes: 0

Views: 9066

Answers (6)

wurflash
wurflash

Reputation: 1

If the file doesn't exist, the filesystem will try to create the file. If creation fails, Java will throw FileNotFountException.

Upvotes: 0

andy
andy

Reputation: 1356

Maybe you need add

File outputFile = new File(dest+""+filename);
if(!outputFile.exist())outputFile.createNewFile();

Upvotes: -1

Reimeus
Reimeus

Reputation: 159754

Suspect that some or all of dest output path may not exist. If this is the case you could use File.mkdirs to build the path.

Also, rather than building the file from strings, would suggest allowing File handle all this, e.g.:

File inputFile = new File(source, filename);
File outputFile = new File(dest, filename);

Upvotes: 2

Ernesto Campohermoso
Ernesto Campohermoso

Reputation: 7371

java.io.FileNotFoundException Means that some file was not found so check the values :

  • source
  • dest
  • filename
  • source+""+filename
  • dest+""+filename

Upvotes: 0

Don
Don

Reputation: 1144

You can use FileUtils from commons-io : http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html

or the Files from Java 7 : http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html

Upvotes: 0

Justin Jasmann
Justin Jasmann

Reputation: 2353

Use apache commons FileUtils. Any of these should be sufficient

FileUtils.copyFile(File srcFile, File destFile) 
FileUtils.copyFile(File srcFile, File destFile, boolean preserveFileDate) 
FileUtils.copyFileToDirectory(File srcFile, File destDir) 
FileUtils.copyFileToDirectory(File srcFile, File destDir, boolean preserveFileDate) 

Upvotes: 1

Related Questions