-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwoParameter.java
84 lines (73 loc) · 2.71 KB
/
TwoParameter.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sample;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TwoParameter extends Command {
public String firstParameter;
public String secondParameter;
public String realCmd;
@Override
public void executeCommand(String Cmd) {
if(verifyCommand(Cmd)){
if("cp".equals(realCmd)){
System.out.println("reached cp");
FileInputStream instream = null;
FileOutputStream outstream = null;
try{
File infile = new File(System.getProperty("user.dir") + "\\"+ firstParameter);
File outfile = new File(System.getProperty("user.dir") + "\\"+ secondParameter);
instream = new FileInputStream(infile);
outstream = new FileOutputStream(outfile);
byte[] buffer = new byte[1024];
int length;
while ((length = instream.read(buffer)) > 0){
outstream.write(buffer, 0, length);
}
instream.close();
outstream.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
} else if("mv".equals(realCmd)) {
File Source = new File (firstParameter);
File Target = new File (secondParameter);
Path source = Paths.get(System.getProperty("usefirstParameterr.dir") + firstParameter);
try {
Files.move(source, source.resolveSibling(secondParameter));
} catch (IOException ex) {
Logger.getLogger(TwoParameter.class.getName()).log(Level.SEVERE, null, ex);
}
}
} else{
System.out.println("Please write valid command");
}
}
@Override
public void splitCommand(String Cmd) {
String[] params;
params = Cmd.split("\\s+");
realCmd = params[0];
firstParameter = params[1];
secondParameter = params[2];
}
@Override
public boolean verifyCommand(String Cmd) {
splitCommand(Cmd);
if("cp".equals(realCmd) || "mv".equals(realCmd) ){
return true;
}
return false;
}
}