Java Program on How to Copy One File Data into Other File

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Program 1

// Copy one file into second file
package dataflair;
import java.io.*;
public class TestFileCopy 
{
    public static void main(String[] args) throws IOException 
    {
        File F1=new File("d://dataflair/student.txt");
        File F2=new File("d://dataflair/student12345.txt");
          char ch;
          int n;
        if(F1.isFile())
        {
            FileInputStream fis=null;
            FileOutputStream fos=null;
          try
          {     
            fis=new FileInputStream(F1);
            fos=new FileOutputStream(F2);
            fos.flush();
//            while((n=fis.read())!=-1)
//            {
//                fos.write(n);
//            }    
            byte b[]=new byte[(int)F1.length()];
               fis.read(b);
               fos.write(b);
          }
          finally
          {
              fis.close();
              fos.close();
          }    
            System.out.println("File copied successfully........"); 
        }
        else
            System.out.println("File error file not found........");   
        
    }
 
}

 

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *