-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplatFile.java
More file actions
107 lines (90 loc) · 2.33 KB
/
SplatFile.java
File metadata and controls
107 lines (90 loc) · 2.33 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
99
100
101
102
103
104
105
106
107
import java.io.*;
import java.nio.channels.*;
import java.net.*;
import java.util.*;
/** @author Gergely Kota
SplatFile overrides File's delete to automatically do recursive deletion.
Also allows a File to be protected from deletion and can copy a file
elsewhere.
*/
public class SplatFile extends File
{
private boolean protect;
private HashMap children = new HashMap();
public SplatFile(File parent, String child)
{
super(parent, child);
}
public SplatFile(String pathname)
{
super(pathname);
}
public SplatFile(String parent, String child)
{
super(parent, child);
}
public SplatFile(URI uri)
{
super(uri);
}
/* ----------- Constructors above here ------------------- */
public boolean copyTo(File f)
{
try
{
FileChannel in = new FileInputStream(this).getChannel();
FileChannel out = new FileOutputStream(f).getChannel();
in.transferTo((int)0, (int)in.size(), out);
in.close();
out.close();
return true;
}
catch(Exception e) {return false;}
}
public boolean deleteAll()
{
File[] files = listFiles();
if(files != null)
for(int i = 0; i < files.length; i++)
{
SplatFile sf = (SplatFile) children.get(new SplatFile(files[i], "").getName());
// if there is no such name in the protected list
if(sf == null)
new SplatFile(files[i], "").deleteAll();
else
sf.deleteAll();
}
if(!protect)
{
System.out.println("Deleting " + getName());
return delete();
}
return false;
}
public void protect()
{
children = new HashMap();
protect = true;
// System.out.println(getName() + " protected");
File[] files = listFiles();
if(files == null)
return;
for(int i = 0; i < files.length; i++)
{
SplatFile sf = new SplatFile(files[i], "");
children.put(sf.getName(), sf);
sf.protect();
}
}
/* -------------------------------------------------------- */
/* -------------------------------------------------------- */
public static void main(String[] args)
{
SplatFile sf = new SplatFile("C:/sp.pdf");
sf.copyTo(new File("copied.pdf"));
// SplatFile save = new SplatFile("c:/gergely.jpg");
// save.copyTo(new File("Delete/gerg.jpg"));
// System.out.println(sf.deleteAll());
// System.out.println(sf.copyTo(new File("Test/gerg.jpg")));
}
}