关于java中 renameTo()方法的不成功

来源:互联网 发布:淘宝手机店铺收藏 编辑:程序博客网 时间:2024/05/27 16:43

本文转载自csdn文章:关于java中 renameTo()方法的不成功

今天为了给文件重新命名,怎么着也不能成功

[java] view plain copy
  1. if(f.renameTo(new File(path + "/" +newname)))  
  2.     System.out.println(path + "/" +newname + "has renamed");          
后来各种百度,有说文件名不能带变量的,有说地址不能出现“_”  "-"  ":"等符号的,我都试了,最后发现时因为我之前用到的BefferedReader进行读操作之后,没有关闭,导致不能重新命名,但是比较蛋疼的就是他并没有报错,就是不能重新命名,最后我把BefferedReader关了就好使了

函数如下

[java] view plain copy
  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6.   
  7. public class stringtest {  
  8. public static void main(String args[]) throws IOException, FileNotFoundException{  
  9.           
  10.         String[] keywords={"事故","死亡","受伤","失踪","损失","伤亡","灾区","救灾","灾害","受灾","救灾","救援"};                    //事故关键字  
  11.         int wordCount = 0;                          //一共多少起带关键字的文件  
  12.         String path = "D:/renminribaoData/from1to10000";           //文件夹地址  
  13.           
  14.           
  15.           
  16.         File file = new File(path);               
  17.         String fileNames[] = file.list();  
  18.           
  19.           
  20.         for(int i = 0 ;i< 5;i++){  
  21.             BufferedReader br = new BufferedReader(new FileReader(path + "/" + fileNames[i]));  
  22.             System.out.println("正在匹配" + fileNames[i]);   //读取BufferedRead的内容称String型  
  23.             String temp = "";  
  24.             String s = "";  
  25.             String newname = "";  
  26.             while((temp = br.readLine()) != null)  
  27.             {  
  28.               s = s + temp;  
  29.             }  
  30.             for(int j = 0 ; j < keywords.length; j++){  
  31.                 if(s.contains(keywords[j])){          
  32.                     System.out.println(fileNames[i] + "含有关键字");  
  33.                     File f = new File(path + "/" + fileNames[i]);  
  34. //                  System.out.println(path + "\\" + fileNames[i]);  
  35. //                  if(!fileNames[i].contains("Y_")) System.out.println("exist!");  
  36. //                  else System.out.println("no!");  
  37. //                  br.close();                                就是这个地方  一定不能忘记关掉啊  
  38.                     if(f.exists()&& !fileNames[i].contains("Y") )     
  39.                         newname = "Y" + fileNames[i];  
  40.                         System.out.println(newname);  
  41.                         if(f.renameTo(new File(path + "/" +newname)))  
  42.                             System.out.println(path + "/" +newname + "has renamed");                          
  43.                         wordCount++;  
  44.                         break;  
  45.                 }  
  46.                 else{  
  47.                     //如果不带关键字进行的操作  
  48.                 }  
  49.             }  
  50.         }  
  51.         System.out.println("一共有" + wordCount + "个带有关键字的文件");  
  52.           
  53.     }  
  54. }  
1 0
原创粉丝点击