KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > memoire > vainstall > VAFile


1 /**
2  * @creation 28/02/05
3 */

4 package com.memoire.vainstall;
5
6 import java.io.File JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.net.URI JavaDoc;
9
10 /**
11  * Extension of java.io.File that overcomes a problem in File.canWrite
12  * on Windows XP. Breifly, new File("C:\\Program Files").canWrite()
13  * always returns false on Win XP even if the directory is writable.
14  * This class overrides canWrite. The new method is used
15  * only if the platform is Windows and the file is a directory.
16  * Otherwise, the parental method is used.
17  * @author Dick Repasky
18 */

19
20 public class VAFile extends File JavaDoc {
21
22     public VAFile(File JavaDoc parent, String JavaDoc child) {
23         super(parent, child);
24     }
25
26     public VAFile(String JavaDoc pathname) {
27         super(pathname);
28     }
29
30     public VAFile(String JavaDoc parent, String JavaDoc child) {
31         super(parent, child);
32     }
33
34     public VAFile(URI JavaDoc uri) {
35         super(uri);
36     }
37
38     public VAFile(File JavaDoc file) {
39         super(file.getAbsolutePath());
40     }
41
42     public boolean canWrite() {
43         boolean answer = super.canWrite();
44         if (! answer && Setup.IS_WIN && isDirectory()) {
45             answer = canWriteDirectory();
46         }
47         return answer;
48     }
49
50     public File JavaDoc getParentFile() {
51         return new VAFile(super.getParentFile());
52     }
53
54     private boolean canWriteDirectory() {
55         boolean answer = false;
56         File JavaDoc f=null;
57         try {
58             f=createTempFile("idw", "tmp", this);
59             answer = true;
60         } catch (IOException JavaDoc ioe) { } // do nothing
61
finally{
62             if(f!=null)f.delete();
63         }
64         return answer;
65     }
66 }
67
Popular Tags