Q:

Java - Copy Content of One File to Another File

belongs to collection: Java File Handling Programs

0

Java - Copy Content of One File to Another File

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Copy Content of One File to Another File

// Java - Copy Content of One File to Another File

import java.io.*;

public class CopyFile {
  public static void main(String args[]) throws IOException {
    File fileName = new File("d:/sample.txt");

    FileInputStream inFile = new FileInputStream("d:/sample.txt");
    int fileLength = (int) fileName.length();

    byte Bytes[] = new byte[fileLength];

    System.out.println("File size is: " + inFile.read(Bytes));

    String file1 = new String(Bytes);
    System.out.println("File content is:\n" + file1);

    FileOutputStream outFile = new FileOutputStream("d:/sample-copy.txt");

    for (int i = 0; i < fileLength; i++) {
      outFile.write(Bytes[i]);
    }

    System.out.println("File copied.");
    //close files
    inFile.close();
    outFile.close();
  }
}

Output:

File size is: 22
File content is:
This is a sample file.
File copied.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java File Handling Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to rename an existing file... >>
<< Java - Print File Content, Display File...