java文件操作
1 package com.b510;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.FileWriter;
7 import java.io.InputStream;
8 import java.io.PrintWriter;
9
10 /**
11 *
12 * @author Hongten</br>
13 *
14 * 文件的操作
15 *
16 * 參考:http://www.newsmth.net/pc/pccon.php?id=2206&nid=318002
17 *
18 *
19 */
20 public class OperateFiles {
21
22 /**
23 * @param args
24 */
25 public static void main(String[] args) {
26 OperateFiles operateFiles = new OperateFiles();
27 //新建一個文件夾
28 operateFiles.newFolder("c:/hongten");
29 //新建一個文件,同時向里面寫入內容
30 operateFiles.newFile("c:/hongten/Hello.txt", "hello,i'm Hongten.你好");
31 //刪除一個文件
32 operateFiles.deleteFile("c:/hongten/Hello.txt");
33 //刪除一個文件夾
34 operateFiles.deleteFolder("c:/hongten");
35 //復制文件夾
36 operateFiles.copyFolder("c:/hongten", "e:/hongten");
37 //提取文件的擴展名
38 String expandedName=operateFiles.getExpandedName("c:/hongten/Hello.txt");
39 System.out.println(expandedName);
40 //提取文件的路徑
41 System.out.println(operateFiles.getFilePath("c:/hongten/Hello.txt"));
42 }
43
44 /**
45 * 獲得文件的擴展名
46 * @param filePath 文件的路徑 如:c:/hongten/Hello.txt
47 * @return 文件的擴展名 如:txt
48 */
49 public String getExpandedName(String filePath){
50 return filePath.substring(filePath.lastIndexOf(".")+1);
51 }
52 /**
53 * 獲得文件的路徑
54 * @param file 文件的路徑
55 * @return 文件的路徑
56 */
57 public String getFilePath(String file){
58 return file.substring(0,file.lastIndexOf("/"));
59 }
60 /**
61 * 新建一個目錄
62 *
63 * @param folderPath
64 * 新建目錄的路徑 如:c:\\newFolder
65 */
66 public void newFolder(String folderPath) {
67 try {
68 File myFolderPath = new File(folderPath.toString());
69 if (!myFolderPath.exists()) {
70 myFolderPath.mkdir();
71 }
72 } catch (Exception e) {
73 System.out.println("新建目錄操作出錯");
74 e.printStackTrace();
75 }
76 }
77
78 /**
79 * 新建一個文件
80 *
81 * @param filePath
82 * 新建文件的目錄 如:c:\\hongten.java
83 */
84 public void newFile(String filePath) {
85 try {
86 File myFilePathFile = new File(filePath.toString());
87 if (!myFilePathFile.exists()) {
88 myFilePathFile.createNewFile();
89 }
90 } catch (Exception e) {
91 System.out.println("新文件創建失敗");
92 e.printStackTrace();
93 }
94 }
95
96 /**
97 * 新建一個文件,同時向文件中寫入內容
98 *
99 * @param filePath
100 * 新建文件的目錄 如:c:\\hongten.java
101 * @param fileContent
102 * 向文件中寫入的內容
103 */
104 public void newFile(String filePath, String fileContent) {
105 try {
106 newFile(filePath);
107 FileWriter resultFile = new FileWriter(filePath);
108 PrintWriter myFile = new PrintWriter(resultFile);
109 myFile.println(fileContent);
110 resultFile.close();
111 } catch (Exception e) {
112 System.out.println("新建文件操作出錯");
113 e.printStackTrace();
114 }
115 }
116
117 /**
118 * 刪除一個文件
119 *
120 * @param filePath
121 * 要刪除文件的絕對路徑 如:c:\\hongten\\Hello.txt
122 */
123 public void deleteFile(String filePath) {
124 try {
125 File preDelFile = new File(filePath);
126 if (preDelFile.exists()) {
127 preDelFile.delete();
128 } else {
129 System.out.println(filePath + "不存在!");
130 }
131 } catch (Exception e) {
132 System.out.println("刪除文件操作出錯");
133 e.printStackTrace();
134 }
135 }
136
137 /**
138 * 刪除一個文件夾
139 *
140 * @param folderPath
141 * 要刪除的文件夾的絕對路徑 如:c:\\hongten
142 */
143 public void deleteFolder(String folderPath) {
144 try {
145 delAllFiles(folderPath);
146 File preDelFoder = new File(folderPath);
147 if (preDelFoder.exists()) {
148 preDelFoder.delete();
149 } else {
150 System.out.println(folderPath + "不存在!");
151 }
152 } catch (Exception e) {
153 System.out.println("刪除文件操作出錯");
154 e.printStackTrace();
155 }
156 }
157
158 /**
159 * 刪除一個文件夾下的所有文件
160 *
161 * @param folderPath
162 * 要刪除的文件夾的絕對路徑 如:c:\\hongten
163 */
164 public void delAllFiles(String folderPath) {
165 File file = new File(folderPath);
166 if (!file.exists()) {
167 return;
168 }
169 if (!file.isDirectory()) {
170 return;
171 }
172 String[] tempList = file.list();
173 File temp = null;
174 for (int i = 0; i < tempList.length; i++) {
175 if (folderPath.endsWith(File.separator)) {
176 temp = new File(folderPath + tempList[i]);
177 } else {
178 temp = new File(folderPath + File.separator + tempList[i]);
179 }
180 if (temp.isFile()) {
181 temp.delete();
182 }
183 if (temp.isDirectory()) {
184 delAllFiles(folderPath + "/" + tempList[i]);// 先刪除文件夾里面的文件
185 deleteFolder(folderPath + "/" + tempList[i]);// 再刪除空文件夾
186 }
187 }
188 }
189
190 /**
191 * 單個文件的復制
192 *
193 * @param oldPath
194 * 原路徑 如:c:\\Hello.java
195 * @param newPath
196 * 新路徑 如:f:\\Hello.java
197 */
198 public void copyFile(String oldPath, String newPath) {
199 try {
200 int bytesum = 0;
201 int byteread = 0;
202 File oldfile = new File(oldPath);
203 if (oldfile.exists()) { // 文件存在時
204 InputStream inStream = new FileInputStream(oldPath); // 讀入原文件
205 FileOutputStream fs = new FileOutputStream(newPath);
206 byte[] buffer = new byte[1444];
207 // int length = 0;
208 while ((byteread = inStream.read(buffer)) != -1) {
209 bytesum += byteread; // 字節數 文件大小
210 fs.write(buffer, 0, byteread);
211 }
212 inStream.close();
213 }
214 } catch (Exception e) {
215 System.out.println("復制單個文件操作出錯");
216 e.printStackTrace();
217
218 }
219 }
220
221 /**
222 * 文件夾的復制
223 *
224 * @param oldPath
225 * 原文件夾路徑 如: c:\\hello
226 * @param newPath
227 * 新文件夾路徑 如: e:\\hello
228 */
229 public void copyFolder(String oldPath, String newPath) {
230
231 try {
232 (new File(newPath)).mkdirs(); // 如果文件夾不存在 則建立新文件夾
233 File a = new File(oldPath);
234 String[] file = a.list();
235 File temp = null;
236 for (int i = 0; i < file.length; i++) {
237 if (oldPath.endsWith(File.separator)) {
238 temp = new File(oldPath + file[i]);
239 } else {
240 temp = new File(oldPath + File.separator + file[i]);
241 }
242
243 if (temp.isFile()) {
244 FileInputStream input = new FileInputStream(temp);
245 FileOutputStream output = new FileOutputStream(newPath
246 + "/" + (temp.getName()).toString());
247 byte[] b = new byte[1024 * 5];
248 int len;
249 while ((len = input.read(b)) != -1) {
250 output.write(b, 0, len);
251 }
252 output.flush();
253 output.close();
254 input.close();
255 }
256 if (temp.isDirectory()) {// 如果是子文件夾
257 copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
258 }
259 }
260 } catch (Exception e) {
261 System.out.println("復制整個文件夾內容操作出錯");
262 e.printStackTrace();
263
264 }
265 }
266
267 /**
268 * 移動單個文件
269 *
270 * @param oldPath
271 * 源文件路徑 如:c:\\hello.java
272 * @param newPath
273 * 新文件路徑 如:e:\\hello.java
274 */
275 public void moveFile(String oldPath, String newPath) {
276 copyFile(oldPath, newPath);
277 deleteFile(oldPath);
278 }
279
280 /**
281 * 移動文件夾
282 *
283 * @param oldPath
284 * 原文件夾路徑 如:c:\\hongten
285 * @param newPath
286 * 新文件夾路徑 如:e:\\hongten
287 */
288 public void moveFolder(String oldPath, String newPath) {
289 copyFolder(oldPath, newPath);
290 deleteFolder(oldPath);
291 }
292
293 /**
294 * 獲得系統根目錄絕對路徑
295 *
296 * @return
297 */
298 public String getPath() {
299 String sysPath = this.getClass().getResource("/").getPath();
300 // 對路徑進行修改
301 sysPath = sysPath.substring(1, sysPath.length() - 16);
302 return sysPath;
303 }
304
305 }
現在有時間把這些東西整理出來,給大家分享一下……