Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to write a program to copy characters from one file to another in Java?
To copy the contents of one file to other character by character
Example
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFiles {
public static void main(String[] args) throws IOException {
//Creating a File object to hold the source file
File source = new File("D:\ExampleDirectory\SampleFile.txt");
//Creating a File object to hold the destination file
File destination = new File("D:\ExampleDirectory\outputFile.txt");
//Creating an FileInputStream object
FileInputStream inputStream = new FileInputStream(source);
//Creating an FileOutputStream object
FileOutputStream outputStream = new FileOutputStream(destination);
//Creating a buffer to hold the data
int length = (int) source.length();
byte[] buffer = new byte[length];
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
System.out.println("File copied successfully.......");
}
}
Output
File copied successfully.......
Advertisements