File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments