Write a program to copy from one file to another.
package com.company;
import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main
{
public static void main(String[] args)
{
FileInputStream input_stream = null;
FileOutputStream output_stream = null;
try{
//give the path name of your input and output files
File input_file =new File("C:\\IdeaProjects\\LEarn_java\\src\\com\\company\\kelly.txt");
File output_file =new File("C:\\IdeaProjects\\LEarn_java\\src\\com\\company\\copy.txt");
input_stream = new FileInputStream(input_file);
output_stream = new FileOutputStream(output_file);
byte[] buffer = new byte[1024];
int length;
/*copying the contents from input stream to
* output stream using read and write methods
*/
while ((length = input_stream.read(buffer)) > 0){
output_stream.write(buffer, 0, length);
}
//Closing the input/output file streams
input_stream.close();
output_stream.close();
System.out.println("File copied successfully!!");
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
Comments
Leave a comment