-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy.java
More file actions
98 lines (72 loc) · 2.98 KB
/
copy.java
File metadata and controls
98 lines (72 loc) · 2.98 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Copy {
/* Command::
copy nameA nameB where nameA and nameB must be the name of a files and their paths.
Action:
Copies file nameA to file nameB.
Errors:
The user enters:
1. copy Does not type the file names.
2. copy name Did not type a second file name.
3. copy nameA nameB but the file nameA does not exist.
4. copy nameA nameB but the file nameB alreadyexist.
5. copy nameA nameB but nameA is a directory.
6. copy nameA nameB but the file is a directory.
When there is an error println a message and return (not exit).
*/
public Copy(String name) {
f(name);
}
static void f(String name) {
try {
// splitting the data type string to get to names
String[] names = name.split(" ");
//creating a new object for the file class to get the first file name
File f1 = new File(names[0]);
// Prints the
//System.out.println(names[0]);
//creating a new object for the file class to get the second file name
File f2 = new File(names[1]);
//System.out.println(names[1]);
//class creates an InputStream that you can use to read bytes from a file
InputStream in = new FileInputStream(f1);
//class creates an OutputStream that you can use to write bytes to a file
OutputStream out = new FileOutputStream(f2);
// the array of bytes is created and read 1024 bytes at a time.
byte [] buf = new byte[1024];
// declaring the length of an array
int len;
// This loop continues to read the buf array of the txt file and stops when its equal to zero
// len => length of the array gets how much bytes it get backs each time something is read from the file
while ((len = in.read(buf)) > 0) {
// if the value returned is zero then it is end of the file and
//it is written to the output file
out.write(buf,0,len);
}
//Since it is done copying closing the inputStream
in.close();
// closing the outputStream
out.close();
// Prints that the file is successfully copied
System.out.println("File Copied");
// exception found
} catch (FileNotFoundException e) {
// throwing error that the file does not exists
System.out.println("The File does not exits");
// If the input is empty then trying to catch the array Indexoutofboundexception
} catch (ArrayIndexOutOfBoundsException e) {
// throwing error if the input is empty
System.out.println("The input NOT GIVEN");
// If the user does not type the input then trying to catch IOException
} catch (IOException e) {
// throwing error when the user does not type the file name
System.out.println("The user does not type the file name");
}
}
}