1 package us.codecraft.webmagic.utils;
2
3 import java.io.File;
4
5
6
7
8
9
10
11 public class FilePersistentBase {
12
13 protected String path;
14
15 public static String PATH_SEPERATOR = "/";
16
17 static {
18 String property = System.getProperties().getProperty("file.separator");
19 if (property != null) {
20 PATH_SEPERATOR = property;
21 }
22 }
23
24 public void setPath(String path) {
25 if (!path.endsWith(PATH_SEPERATOR)) {
26 path += PATH_SEPERATOR;
27 }
28 this.path = path;
29 }
30
31 public File getFile(String fullName) {
32 checkAndMakeParentDirecotry(fullName);
33 return new File(fullName);
34 }
35
36 public void checkAndMakeParentDirecotry(String fullName) {
37 int index = fullName.lastIndexOf(PATH_SEPERATOR);
38 if (index > 0) {
39 String path = fullName.substring(0, index);
40 File file = new File(path);
41 if (!file.exists()) {
42 file.mkdirs();
43 }
44 }
45 }
46
47 public String getPath() {
48 return path;
49 }
50 }