KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > io > RenameJob


1 package org.oddjob.io;
2
3 import java.io.File JavaDoc;
4 import java.io.Serializable JavaDoc;
5
6
7 /**
8  * @oddjob.description Rename a file or directory.
9  *
10  * @oddjob.example
11  *
12  * <pre><code>
13  * &lt;rename from="data/some.txt" to="work/other.txt"/&gt;
14  * </code><pre>
15  *
16  */

17
18 public class RenameJob implements Runnable JavaDoc, Serializable JavaDoc {
19     private static final long serialVersionUID = 20060117;
20     
21     /**
22      * @oddjob.property
23      * @oddjob.description A name, can be any text.
24      * @oddjob.required No.
25      */

26     private String JavaDoc name;
27     
28     /**
29      * @oddjob.property
30      * @oddjob.description The from file.
31      * @oddjob.required Yes unless input supplied.
32      */

33     private File JavaDoc from;
34     
35     /**
36      * @oddjob.property
37      * @oddjob.description The to file.
38      * @oddjob.required Yes unless output supplied.
39      */

40     private File JavaDoc to;
41         
42     /**
43      * Get the name.
44      *
45      * @return The name.
46      */

47     public String JavaDoc getName() {
48         return name;
49     }
50     
51     /**
52      * Set the name
53      *
54      * @param name The name.
55      */

56     public void setName(String JavaDoc name) {
57         this.name = name;
58     }
59     
60     /**
61      * Get the from file.
62      *
63      * @return The from file.
64      */

65     public File JavaDoc getFrom() {
66         return from;
67     }
68     
69     /**
70      * Set the from file.
71      *
72      * @param The from file.
73      */

74     public void setFrom(File JavaDoc file) {
75         this.from = file;
76     }
77
78     /**
79      * Get the to file.
80      *
81      * @return The to file.
82      */

83     public File JavaDoc getTo() {
84         return to;
85     }
86     
87     /**
88      * Set the to file.
89      *
90      * @param The to file.
91      */

92     public void setTo(File JavaDoc file) {
93         this.to = file;
94     }
95
96
97     /*
98      * (non-Javadoc)
99      * @see java.lang.Runnable#run()
100      */

101     public void run() {
102         if (from == null) {
103             throw new NullPointerException JavaDoc("From file must be specified.");
104         }
105         if (to == null) {
106             throw new NullPointerException JavaDoc("To file must be specified.");
107         }
108         if (!from.exists()) {
109             throw new RuntimeException JavaDoc("[" + from + "], no such file or directory.");
110         }
111         if (!from.renameTo(to)) {
112             throw new RuntimeException JavaDoc("Rename failed.");
113         }
114     }
115
116     /*
117      * (non-Javadoc)
118      * @see java.lang.Object#toString()
119      */

120     public String JavaDoc toString() {
121         if (name == null) {
122             return "Rename";
123         }
124         return name;
125     }
126 }
127
Popular Tags