文件和文件夾的操作

01.package OALogic.sql.data;  
02. 
03.import java.io.*;   
04. 
05.public class FileOperate {   
06.   public FileOperate() {   
07.   }   
08.     
09.   public static void main(String args[]){  
10.       newFolder("D:/100");  
11. 
12.   }  

13.   /*
 //可獲得當前項目的根目錄
 ClassLoader.getSystemClassLoader().getSystemResource("//").getPath()
 */

14.   /**  
15.     * 新建目錄  
16.     * @param folderPath String 目錄路徑 c:/fqf  
17.     * @return boolean  
18.     */
19.   public static void newFolder(String folderPath) {   
20.       try {   
21.           String filePath = folderPath;   
22.           filePath = filePath.toString();   
23.           java.io.File myFilePath = new java.io.File(filePath);   
24.           if (!myFilePath.exists()) {   
25.               myFilePath.mkdir();
26.           }   
27.       }   
28.       catch (Exception e) {   
29.           System.out.println("新建目錄操作出錯");   
30.           e.printStackTrace();   
31.       }   
32.   }   
33. 
34.   /**  
35.     * 新建文件 
36.     * @param filePathAndName String 文件目錄和名稱c:/fqf.txt  
37.     * @param fileContent String   
38.     * @return boolean  
39.     */    
40.   public static void newFile(String filePathAndName, String fileContent) {   
41. 
42.       try {   
43.           String filePath = filePathAndName;   
44.           filePath = filePath.toString();   
45.           File myFilePath = new File(filePath);   
46.           if (!myFilePath.exists()) {   
47.               myFilePath.createNewFile();   
48.           }   
49.           FileWriter resultFile = new FileWriter(myFilePath);   
50.           PrintWriter myFile = new PrintWriter(resultFile);   
51.           String strContent = fileContent;   
52.           myFile.println(strContent);   
53.           resultFile.close();   
54. 
55.       }   
56.       catch (Exception e) {   
57.           System.out.println("新建目錄操作出錯");   
58.           e.printStackTrace();   
59. 
60.       }   
61. 
62.   }   
63. 
 /**  
65.     * 刪除文件
66.     * @param filePathAndName String 文件路徑和名稱c:/fqf.txt  
67.     * @param fileContent String  
68.     * @return boolean  
69.     */   
70.   public static void delFile(String filePathAndName) {   
71.       try {   
72.           String filePath = filePathAndName;   
73.           filePath = filePath.toString();   
74.           java.io.File myDelFile = new java.io.File(filePath);   
75.           myDelFile.delete();   
77.       }   
78.       catch (Exception e) {   
79.           System.out.println("刪除文件操作出錯");   
80.           e.printStackTrace();   
81. 
82.       }   
83. 
84.   }   
85. 
86.  /**  
87.     * 刪除文件夾  
88.     * @param filePathAndName String 文件夾路徑和名稱:/fqf  
89.     * @param fileContent String  
90.     * @return boolean  
91.     */   
92.   public static void delFolder(String folderPath) {   
93.       try {   
94.           delAllFile(folderPath); //刪除完裏面所有內容
95.           String filePath = folderPath;   
96.           filePath = filePath.toString();   
97.           java.io.File myFilePath = new java.io.File(filePath);   
98.           myFilePath.delete();  //刪除空文件夾  
99. 
100.       }   
101.       catch (Exception e) {   
102.           System.out.println("刪除文件夾操作出錯");   
103.           e.printStackTrace();   
104. 
105.       }   
106. 
107.   }   
108. 
108. 
109.   /**  
110.     * 刪除總個文件夾  
111.     * @param path String 文件夾所在路徑 c:/fqf  
112.     */    
113.   public static void delAllFile(String path) {   
114.       File file = new File(path);   
115.       if (!file.exists()) {   
116.           return;   
117.       }   
118.       if (!file.isDirectory()) {   
119.           return;   
120.       }   
121.       String[] tempList = file.list();   
122.       File temp = null;   
123.       for (int i = 0; i < tempList.length; i++) {   
124.           if (path.endsWith(File.separator)) {   
125.               temp = new File(path + tempList[i]);   
126.           }   
127.           else {   
128.               temp = new File(path + File.separator + tempList[i]);   
129.           }   
130.           if (temp.isFile()) {   
131.               temp.delete();   
132.           }   
133.           if (temp.isDirectory()) {   
134.               delAllFile(path+"/"+ tempList[i]);//先刪除文件夾裏面的文件
135.               delFolder(path+"/"+ tempList[i]);//再刪除空文件夾
136.           }   
137.       }   
138.   }   
139. 
140.   /**  
141.     * 複製文件 
142.     * @param oldPath String 原文件路徑c:/fqf.txt  
143.     * @param newPath String 新文件路徑f:/fqf.txt  
144.     * @return boolean  
145.     */     
146.   public static void copyFile(String oldPath, String newPath) {   
147.       try {   
148.           int bytesum = 0;   
149.           int byteread = 0;   
150.           File oldfile = new File(oldPath);   
151.           if (oldfile.exists()) { //文件存在時 
152.               InputStream inStream = new FileInputStream(oldPath); // ¶ÁÈëÔ­Îļþ
153.               FileOutputStream fs = new FileOutputStream(newPath);   
154.               byte[] buffer = new byte[1444];   
155.               int length;   
156.               while ( (byteread = inStream.read(buffer)) != -1) {   
157.                   bytesum += byteread; //字節數 文件大小   
158.                   System.out.println(bytesum);   
159.                   fs.write(buffer, 0, byteread);   
160.               }   
161.               inStream.close();   
162.           }   
163.       }   
164.       catch (Exception e) {   
165.           System.out.println("複製單個文件操作出錯");   
166.           e.printStackTrace();   
167. 
168.       }   
169. 
170.   }   
171. 
172.   /**  
173.     * 複製文件夾
174.     * @param oldPath String 原文件夾所在位置c:/fqf  
175.     * @param newPath String 要複製到的位置f:/fqf/ff  
176.     * @return boolean  
177.     */     
178.   public static void copyFolder(String oldPath, String newPath) {   
179. 
180.       try {   
181.           (new File(newPath)).mkdirs();  //如果文件夾不存在 則建立新文件夾   
182.           File a=new File(oldPath);   
183.           String[] file=a.list();   
184.           File temp=null;   
185.           for (int i = 0; i < file.length; i++) {   
186.               if(oldPath.endsWith(File.separator)){   
187.                   temp=new File(oldPath+file[i]);   
188.               }   
189.               else{   
190.                   temp=new File(oldPath+File.separator+file[i]);   
191.               }   
192. 
193.               if(temp.isFile()){   
194.                   FileInputStream input = new FileInputStream(temp);   
195.                   FileOutputStream output = new FileOutputStream(newPath + "/" +   
196.                           (temp.getName()).toString());   
197.                   byte[] b = new byte[1024 * 5];   
198.                   int len;   
199.                   while ( (len = input.read(b)) != -1) {   
200.                       output.write(b, 0, len);   
201.                   }   
202.                   output.flush();   
203.                   output.close();   
204.                   input.close();   
205.               }   
206.               if(temp.isDirectory()){//如果是子文件夾
207.                   copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);   
208.               }   
209.           }   
210.       }   
211.       catch (Exception e) {   
212.           System.out.println("複製文件夾出錯");   
213.           e.printStackTrace();   
214. 
215.       }   
216. 
217.   }   
218. 
219.   /**  
220.     * 移動文件 
221.     * @param oldPath String 原路徑:/fqf.txt  
222.     * @param newPath String 新路徑:/fqf.txt  
223.     */   
224.   public static void moveFile(String oldPath, String newPath) {   
225.       copyFile(oldPath, newPath);   
226.       delFile(oldPath);   
227. 
228.   }   
229. /**  
231.     * 移動文件夾  
232.     * @param oldPath String 原路徑c:/fqf.txt  
233.     * @param newPath String 新路徑d:/fqf.txt  
234.     */    
235.   public static void moveFolder(String oldPath, String newPath) {   
236.       copyFolder(oldPath, newPath);   
237.       delFolder(oldPath);   
238. 
239.   }   
240.}  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章