Develop a Java program to copy the contents of one file to another fileusing FileInputStream and FileOutputStream classes.
Question
Develop a Java program to copy the contents of one file to another fileusing FileInputStream and FileOutputStream classes.
Solution
Sure, here is a simple Java program that uses FileInputStream and FileOutputStream to copy the contents of one file to another:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) {
FileInputStream instream = null;
FileOutputStream outstream = null;
try {
File infile = new File("C:\\Users\\User\\Desktop\\input.txt");
File outfile = new File("C:\\Users\\User\\Desktop\\output.txt");
instream = new FileInputStream(infile);
outstream = new FileOutputStream(outfile);
byte[] buffer = new byte[1024];
int length;
/*copying the contents from input stream to
* output stream using read and write methods
*/
while ((length = instream.read(buffer)) > 0) {
outstream.write(buffer, 0, length);
}
//Closing the input/output file streams
instream.close();
outstream.close();
System.out.println("File copied successfully!!");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
This program reads the contents of the file at the path "C:\Users\User\Desktop\input.txt" and writes it to a new file at the path "C:\Users\User\Desktop\output.txt". If the operation is successful, it prints "File copied successfully!!". If an IOException occurs, it prints the stack trace of the exception.
Please replace "C:\Users\User\Desktop\input.txt" and "C:\Users\User\Desktop\output.txt" with the paths of your source and destination files respectively.
Similar Questions
Write a short note on FilelnputStream and FileOutputStream class.
Select all correct methods to copy the source files's content to the destination file. (2 Points)*2 pointsShutill.copy()shutill.copy2(sourcepath, despath)shutill.copy(fileobj)shutill.copyfile()
orrect answerWhat is the output of this program? (Assume 'inputoutput.java' file exists in the current directory)import java.io.*;class filesinputoutput { public static void main(String args[]) throws FileNotFoundException, IOException { InputStream obj = new FileInputStream("inputoutput.java"); System.out.print(obj.available()); obj.close(); }}
What is the correct code to read a text file in Java?
Handling Exceptions in File I/O Operations Instruction: 1. Create a Java program that reads data from a text file named "input.txt" and copies it to another text file named "output.txt". 2. Implement exception handling to deal with potential I/O errors, such as file not found, permission denied, or input/output errors. 3. Use FileReader, BufferedReader for reading from the input file, and FileWriter, BufferedWriter for writing to the output file. 4. Display informative error messages in case of exceptions.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.