-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCopyVisitor.java
89 lines (77 loc) · 1.77 KB
/
CopyVisitor.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
85
86
87
88
89
package net.za.acraig.movefiles;
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 CopyVisitor extends Visitor
{
private File _destDir;
private boolean _move = false; // whether to move files or copy them
private String _result = "Podcasts listened to this week:\n"; // concatenation of copied filenames
public CopyVisitor(File dest)
{
_destDir = dest;
}
public void setMoveMode(boolean move)
{
_move = move;
}
protected void processFile(final File file)
{
// Thread copyThread = new Thread()
// {
// public void run()
// {
// copyFile(file);
// if (_move)
// file.delete();
// }
// };
// copyThread.start();
boolean copyOk = copyFile(file);
if (copyOk)
_result += file.getName() + "\n";
if (copyOk && _move)
file.delete();
}
private boolean copyFile(File file)
{
try
{
String destname = _destDir.getAbsolutePath() + File.separator + file.getName();
File dest = new File(destname);
if (dest.exists())
return false;
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
out.close();
boolean succesfulCopy = (file.length() == dest.length());
return succesfulCopy;
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public String getResult()
{
return _result;
}
}