Skip to content

Commit 63ba342

Browse files
authored
Merge pull request codec-akash#39 from prakhyatkarri/master
Added CopyFile Java program to copy file contents
2 parents 8836993 + 2c5c6ad commit 63ba342

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

CopyFile.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.io.FileInputStream;
2+
import java.io.FileOutputStream;
3+
import java.io.IOException;
4+
5+
/**
6+
* This file will copy contents of a file from one file to another file.
7+
*/
8+
public class CopyFile {
9+
public static void main(String args[]) throws IOException {
10+
FileInputStream inputStream = null;
11+
FileOutputStream outputStream = null;
12+
13+
// Provide File locations
14+
String inputFileLocation = "input.txt";
15+
String outputFileLocation = "output.txt";
16+
17+
try {
18+
// Create Input and Output streams
19+
inputStream = new FileInputStream("C:\\Users\\prakh\\git\\workspace\\Workspace\\workspace\\angular.json");
20+
outputStream = new FileOutputStream("C:\\Users\\prakh\\git\\workspace\\Workspace\\workspace\\angular_output.json");
21+
22+
// Read the data from Input file in bytes and write each byte to output file
23+
int dataByte;
24+
while ((dataByte = inputStream.read()) != -1) {
25+
outputStream.write(dataByte);
26+
}
27+
} finally {
28+
// Always close the streams to avoid memory leaks
29+
closeResources(inputStream, outputStream);
30+
}
31+
}
32+
33+
/**
34+
* Close the Streams
35+
*
36+
* @param inputStream
37+
* @param outputStream
38+
*/
39+
private static void closeResources(FileInputStream inputStream, FileOutputStream outputStream) throws IOException {
40+
if (inputStream != null) {
41+
inputStream.close();
42+
inputStream = null;
43+
}
44+
if (outputStream != null) {
45+
outputStream.close();
46+
outputStream = null;
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)