konhon

忘掉過去,展望未來。找回自我,超越自我。
逃避不一定躲的过, 面对不一定最难过, 孤单不一定不快乐, 得到不一定能长久, 失去不一定不再拥有, 可能因为某个理由而伤心难过, 但我却能找个理由让自己快乐.

Google

BlogJava 首页 新随笔 联系 聚合 管理
  203 Posts :: 0 Stories :: 61 Comments :: 0 Trackbacks

  2 /** 
  3  * //FileOperate.java
  4  * 文件的各种操作
  5  * 杨彩 http://blog.sina.com.cn/m/yangcai
  6  * 文件操作 1.0
  7  */ 
  8  
  9 //package common; 
 10  
 11 import java.io.*
 12  
 13 public class FileOperate
 14 {
 15  static boolean exitnow=false;
 16  static String aa,bb; 
 17   public FileOperate() { 
 18   } 
 19  
 20   /** 
 21    * 新建目录 
 22    */ 
 23   public void newFolder(String folderPath) { 
 24     try 
 25     { 
 26       String filePath = folderPath; 
 27       filePath = filePath.toString(); 
 28       File myFilePath = new File(filePath); 
 29       if(!myFilePath.exists()) 
 30       { 
 31         myFilePath.mkdir();
 32       }
 33       System.out.println("新建目录操作 成功执行"); 
 34     } 
 35     catch(Exception e)
 36     { 
 37       System.out.println("新建目录操作出错"); 
 38       e.printStackTrace(); 
 39     } 
 40   } 
 41  
 42   /** 
 43    * 新建文件 
 44    */ 
 45   public void newFile(String filePathAndName, String fileContent)
 46   { 
 47  
 48     try
 49     { 
 50       String filePath = filePathAndName; 
 51       filePath = filePath.toString(); 
 52       File myFilePath = new File(filePath); 
 53       if (!myFilePath.exists())
 54       { 
 55         myFilePath.createNewFile(); 
 56       } 
 57       FileWriter resultFile = new FileWriter(myFilePath); 
 58       PrintWriter myFile = new PrintWriter(resultFile); 
 59       String strContent = fileContent; 
 60       myFile.println(strContent); 
 61       resultFile.close(); 
 62       System.out.println("新建文件操作 成功执行"); 
 63     } 
 64     catch (Exception e) { 
 65       System.out.println("新建目录操作出错"); 
 66       e.printStackTrace(); 
 67  
 68     } 
 69  
 70   } 
 71  
 72   /** 
 73    * 删除文件 
 74    */ 
 75   public void delFile(String filePathAndName) { 
 76     try { 
 77       String filePath = filePathAndName; 
 78       filePath = filePath.toString(); 
 79       File myDelFile = new File(filePath); 
 80       myDelFile.delete(); 
 81       System.out.println("删除文件操作 成功执行"); 
 82     } 
 83     catch (Exception e) { 
 84       System.out.println("删除文件操作出错"); 
 85       e.printStackTrace(); 
 86  
 87     } 
 88  
 89   } 
 90  
 91   /** 
 92    * 删除文件夹 
 93    */ 
 94   public void delFolder(String folderPath)
 95   { 
 96     try
 97     { 
 98       delAllFile(folderPath); //删除完里面所有内容 
 99       String filePath = folderPath; 
100       filePath = filePath.toString(); 
101       File myFilePath = new File(filePath); 
102       myFilePath.delete(); //删除空文件夹 
103       System.out.println("删除文件夹操作 成功执行"); 
104     } 
105     catch (Exception e)
106     { 
107       System.out.println("删除文件夹操作出错"); 
108       e.printStackTrace(); 
109  
110     } 
111  
112   } 
113  
114   /** 
115    * 删除文件夹里面的所有文件 
116    * @param path String 文件夹路径 如 c:/fqf 
117    */ 
118   public void delAllFile(String path)
119   { 
120     File file = new File(path); 
121     if(!file.exists())
122     { 
123       return
124     } 
125     if(!file.isDirectory())
126     { 
127       return
128     } 
129     String[] tempList = file.list(); 
130     File temp = null
131     for (int i = 0; i < tempList.length; i++)
132     { 
133       if(path.endsWith(File.separator))
134       { 
135         temp = new File(path + tempList[i]); 
136       } 
137       else
138       { 
139         temp = new File(path + File.separator + tempList[i]); 
140       } 
141       if (temp.isFile())
142       { 
143         temp.delete(); 
144       } 
145       if (temp.isDirectory())
146       { 
147         delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件 
148         delFolder(path+"/"+ tempList[i]);//再删除空文件夹 
149       } 
150     }
151           System.out.println("删除文件操作 成功执行");  
152   } 
153  
154   /** 
155    * 复制单个文件 
156    * @param oldPath String 原文件路径 如:c:/fqf.txt 
157    * @param newPath String 复制后路径 如:f:/fqf.txt 
158    */ 
159   public void copyFile(String oldPath, String newPath) { 
160     try { 
161       int bytesum = 0
162       int byteread = 0
163       File oldfile = new File(oldPath); 
164       if (oldfile.exists())
165       { //文件存在时 
166         InputStream inStream = new FileInputStream(oldPath); //读入原文件 
167         FileOutputStream fs = new FileOutputStream(newPath); 
168         byte[] buffer = new byte[1444]; 
169         int length; 
170         while ( (byteread = inStream.read(buffer)) != -1) { 
171           bytesum += byteread; //字节数 文件大小 
172           System.out.println(bytesum); 
173           fs.write(buffer, 0, byteread); 
174         } 
175         inStream.close(); 
176       }
177             System.out.println("删除文件夹操作 成功执行");  
178     } 
179     catch (Exception e) { 
180       System.out.println("复制单个文件操作出错"); 
181       e.printStackTrace(); 
182  
183     } 
184  
185   } 
186  
187   /** 
188    * 复制整个文件夹内容 
189    * @param oldPath String 原文件路径 如:c:/fqf 
190    * @param newPath String 复制后路径 如:f:/fqf/ff 
191    */ 
192   public void copyFolder(String oldPath, String newPath) { 
193  
194     try
195     { 
196       (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹 
197       File a=new File(oldPath); 
198       String[] file=a.list(); 
199       File temp=null
200       for (int i = 0; i < file.length; i++)
201       { 
202         if(oldPath.endsWith(File.separator))
203         { 
204           temp=new File(oldPath+file[i]); 
205         } 
206         else
207           temp=new File(oldPath+File.separator+file[i]); 
208         } 
209  
210         if(temp.isFile())
211         { 
212           FileInputStream input = new FileInputStream(temp); 
213           FileOutputStream output = new FileOutputStream(newPath + "/" + 
214               (temp.getName()).toString()); 
215           byte[] b = new byte[1024 * 5]; 
216           int len; 
217           while ( (len = input.read(b)) != -1)
218           { 
219             output.write(b, 0, len); 
220           } 
221           output.flush(); 
222           output.close(); 
223           input.close(); 
224         } 
225         if(temp.isDirectory())
226         {//如果是子文件夹 
227           copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); 
228         } 
229       }
230             System.out.println("复制文件夹操作 成功执行");  
231     } 
232     catch (Exception e) { 
233       System.out.println("复制整个文件夹内容操作出错"); 
234       e.printStackTrace(); 
235  
236     } 
237  
238   } 
239  
240   /** 
241    * 移动文件到指定目录 
242    * @param oldPath String 如:c:/fqf.txt 
243    * @param newPath String 如:d:/fqf.txt 
244    */ 
245   public void moveFile(String oldPath, String newPath) { 
246     copyFile(oldPath, newPath); 
247     delFile(oldPath); 
248  
249   } 
250  
251   /** 
252    * 移动文件到指定目录 
253    * @param oldPath String 如:c:/fqf.txt 
254    * @param newPath String 如:d:/fqf.txt 
255    */ 
256   public void moveFolder(String oldPath, String newPath) { 
257     copyFolder(oldPath, newPath); 
258     delFolder(oldPath); 
259  
260   }
261   
262   public static void main(String args[])
263   {
264    System.out.println("使用此功能请按[1]  功能一:新建目录");
265    System.out.println("使用此功能请按[2]  功能二:新建文件");
266    System.out.println("使用此功能请按[3]  功能三:删除文件");
267    System.out.println("使用此功能请按[4]  功能四:删除文件夹");
268    System.out.println("使用此功能请按[5]  功能五:删除文件夹里面的所有文件");
269    System.out.println("使用此功能请按[6]  功能六:复制文件");
270    System.out.println("使用此功能请按[7]  功能七:复制文件夹的所有内容");
271    System.out.println("使用此功能请按[8]  功能八:移动文件到指定目录");
272    System.out.println("使用此功能请按[9]  功能九:移动文件夹到指定目录");
273    System.out.println("使用此功能请按[10] 退出程序");
274    
275  while(!exitnow)
276  {
277     FileOperate fo=new FileOperate();
278     try
279     {
280     BufferedReader Bin=new BufferedReader(new InputStreamReader(System.in));
281     String a=Bin.readLine();
282     int b=Integer.parseInt(a);
283     
284     switch(b) 
285     {
286      case 1:System.out.println("你选择了功能一  请输入目录名");  
287         aa=Bin.readLine();
288         fo.newFolder(aa);
289         break;
290      case 2:System.out.println("你选择了功能二  请输入文件名");  
291         aa=Bin.readLine();
292         System.out.println("请输入在"+aa+"中的内容");
293         bb=Bin.readLine();
294         fo.newFile(aa,bb);
295         break;
296      case 3:System.out.println("你选择了功能三  请输入文件名");  
297         aa=Bin.readLine();
298         fo.delFile(aa);
299         break;
300      case 4:System.out.println("你选择了功能四  请输入文件名");  
301         aa=Bin.readLine();
302         fo.delFolder(aa);
303         break;
304      case 5:System.out.println("你选择了功能五  请输入文件名");  
305         aa=Bin.readLine();
306         fo.delAllFile(aa);
307         break;   
308      case 6:System.out.println("你选择了功能六  请输入文件名");  
309         aa=Bin.readLine();
310         System.out.println("请输入目标文件名"); 
311         bb=Bin.readLine();
312         fo.copyFile(aa,bb);
313         break;
314      case 7:System.out.println("你选择了功能七  请输入源文件名");  
315         aa=Bin.readLine();
316         System.out.println("请输入目标文件名"); 
317         bb=Bin.readLine();
318         fo.copyFolder(aa,bb);
319         break;        
320      case 8:System.out.println("你选择了功能八  请输入源文件名");  
321         aa=Bin.readLine();
322         System.out.println("请输入目标文件名"); 
323         bb=Bin.readLine();
324         fo.moveFile(aa,bb);
325         break;
326        case 9:System.out.println("你选择了功能九  请输入源文件名");  
327         aa=Bin.readLine();
328         System.out.println("请输入目标文件名"); 
329         bb=Bin.readLine();
330         fo.moveFolder(aa,bb);
331         break;        
332      case 10:exitnow=true;
333          System.out.println("程序结束,请退出");
334         break;
335      default:System.out.println("输入错误.请输入1-10之间的数");               
336      }
337     
338     
339     System.out.println("请重新选择功能");
340     
341     
342     }
343     catch(Exception e)
344     {
345     System.out.println("输入错误字符或程序出错");
346     }
347     
348  }   
349  } 
350 }
posted on 2007-01-30 06:25 konhon 优华 阅读(1150) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: