Click here to Skip to main content
14,983,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want copy one file contains in another file but here cannot get the file file is exist but there is shows file not exist

What I have tried:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
class CopyFileContentsInOtherFile {
 
 public static void main(String[] args) {
 
  try
  {
   File f1=new File(args[0]);
   File f2=new File(args[1]);
 
   if(f1.exists()==false)
   {
    System.out.println("Can't Copy Source File Not Exists");
    System.exit(0);
   }
   if(f2.exists()==true)
   {
    System.out.println("Can't Copy Destination File Exists");
    System.exit(0);
   }
 
   FileInputStream fis=new FileInputStream(f1);
   FileOutputStream fos=new FileOutputStream(f2);
 
   int x;
   while((x=fis.read())!=-1) {
    fos.write(x);
   }
   
   System.out.println("Contents of file '"+f1+"' are copied in '"+f2+"' successful...");
   fis.close();
   fos.close();
  }
  catch(Exception ee) { 
   System.out.println(ee);
  }
 }
 
}
Posted
Updated 3-Dec-20 8:11am
Comments
Richard MacCutchan 4-Dec-20 5:10am
   
I just built this and it runs perfectly. You must have done something wrong.

1 solution

Assuming what you mean by "cannot get the file file is exist but there is shows file not exist" is the source file does not exist, well, it comes down to you not providing the appropriate command line arguments to tell the code which file to copy and where to copy it to.

You're either proving the filename of a file that doesn't exist in the "current directory", or you're not properly providing a fully qualified path. You could also be proving a path that has spaces in it without enclosing the entire path in " characters.

If this isn't the problem, you're going to have to give the exact message you're getting from the code, any error messages, and the line on which it occurs.
   
v2

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