-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyonefiletoanother.java
More file actions
30 lines (27 loc) · 1010 Bytes
/
Copy pathCopyonefiletoanother.java
File metadata and controls
30 lines (27 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy
{
public static void main(String[] args)
{
String srcFile = "src.txt";
String destFile = "dest.txt";
try (FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile, true))
{
int b;
while ((b = in.read()) != -1)
{
out.write(b);
}
System.out.println("File copied successfully.");
}
catch (IOException e)
{
System.out.println("An error occurred while copying the file.");
e.printStackTrace();
}
}
}
//Here, the FileOutputStream is constructed with the true flag, which indicates that the data from the source file should be appended to the end of the destination file, rather than overwriting it.