黑馬程序員—這是一個能夠鍵盤錄入路徑去批量修改指定文件夾下所有指定文件內容的方法

------- <a href="http://www.itheima.com" target="blank">android培訓</a>、<a href="http://www.itheima.com" target="blank">java培訓</a>、期待與您交流! ----------

package xiugaiqi;
import java.io.*;
public class wenbenxiugai {


public static void main(String[] args) throws Exception{
System.out.println("請輸入文件所在的路徑");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String path=br.readLine();
System.out.println("請輸入替換前的內容和替換後的內容,回車區分。");
BufferedReader thnr =new BufferedReader(new InputStreamReader(System.in)); 
String nr=thnr.readLine();
String tr=thnr.readLine();
cf(path,nr,tr); 
}
public static void cf(String path,String nr ,String tr)throws Exception
{
File f=null;

    File root = new File(path);  
    //判斷路徑  
    if(! root.exists() && root.isDirectory()){  
        System.out.println("路徑有誤!");  
    }  
    File[] files = root.listFiles();
    System.out.println("總路徑"+root.getAbsolutePath());
    for(int i=0;i<files.length;i++)
    {
          f = files[i];
          String name = f.getName();
          if(f.isDirectory())
          {
    System.out.println(name+"................"+f.getAbsolutePath());  //文件名
    ga.gaiming(f.getAbsolutePath(),nr,tr);
    cf(f.getAbsolutePath(),nr,tr);
          }
    }        
}


}


 class ga {
public static void gaiming(String ypath,String nr,String tr)throws Exception  {


//String huancunpath=br.readLine();
File root = new File(ypath+"\\中哥");  
if(!root.exists())
{
root.mkdir();
}
String huancunpath=root.getAbsolutePath();
        cf(ypath,huancunpath,nr,tr);
copyy(huancunpath,ypath);//"D:\\File\\c"預存路徑
deletefile(huancunpath);
}
//查找文件
public static File cf(String ph,String hc,String nr,String tr)throws Exception
{
File f=null;

//String path=br.readLine();
   File root = new File(ph);  
   //判斷路徑  
   if(! root.exists() && root.isDirectory()){  
       System.out.println("路徑有誤!");  
   }  
   File[] files = root.listFiles(  
   
       new FilenameFilter(){  
           //文件名稱過濾  
           public boolean accept(File dir, String name) {  
                   return name.endsWith(".txt");  
                   
           }  
       }  
       
   );


     
   for(int i=0;i<files.length;i++)
   {
         f = files[i];
         String name = f.getName();
   // System.out.println(name);  //文件名
    gaiFile(files[i],name,hc,nr,tr);
   
   }
   return f;
}
//替換文件的內容




public static void gaiFile(File ff, String name,String hc,String nr ,String tr)throws Exception
 { 
String line; 
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(ff))); //"D:\\File\\b"原路徑。
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(hc+"\\"+name))); //加\\是因爲文件夾無法讀取這些字符。
//就不會有最後一個字符加到name的前頭的情況了。
StringBuilder sb=new StringBuilder(); 
while((line=br.readLine())!=null)

System.out.println(name+".........."+line);//顯示文件名及文件名的內容。
sb.append(line+"\r\n");//SB沒有換行就加載進去了,所以加一個換行"\r\n"。否則結果在一行裏。

}


xn(  sb,nr,tr);//新內容。
bw.write(sb.toString(),0,sb.length()); 

bw.newLine(); 
sb.delete(0, sb.length());
br.close();  
bw.flush(); 
bw.close();
 }


 
//新內容
public static void xn( StringBuilder sb,String nr ,String tr)throws Exception
 {          
 


int index=0; 
while((index=sb.indexOf(nr,index))!=-1)

sb.replace(index,index+nr.length(),tr); 
index+=tr.length(); 
}
 }
 
//將暫存位置的文件複製到原來的文件夾中。

 public static void copyy(String originDirectory,String targetDirectory){
              File origindirectory = new File(originDirectory);   //源路徑File實例"D:\\file\\c"緩存路徑。
              File targetdirectory = new File(targetDirectory);  //目標路徑File實例"D:\\file\\b"
              if(!origindirectory.isDirectory() || !targetdirectory.isDirectory()){    //判斷是不是正確的路徑
                          System.out.println("不是正確的目錄!");
                          return;
              }
              File[] fileList = origindirectory.listFiles();  //目錄中的所有文件
              for(File file : fileList){
                        if(!file.isFile())   //判斷是不是文件
                        continue;
                        System.out.println(file.getName());
                        try{
                                 FileInputStream fin = new FileInputStream(file);
                                 BufferedInputStream bin = new BufferedInputStream(fin);
                                 PrintStream pout = new PrintStream(targetdirectory.getAbsolutePath()+"/"+file.getName());
                                 BufferedOutputStream bout = new BufferedOutputStream(pout);
                                 int count;
                                 while((count = bin.available())!= 0){
                                            int c = bin.read();  //從輸入流中讀一個字節
                                            bout.write(((char)c));  //將字節(字符)寫到輸出流中     
                                 }
                                 bout.close();
                                 pout.close();
                                 bin.close();
                                 fin.close();
                        }catch(IOException e){
                                 e.printStackTrace();//這種異常在控制檯上輸出非常詳細的異常信息。
                        }
             }
            System.out.println("複製成功啦哈哈哈哈哈哈");
}  
 
 
 public static boolean deletefile(String delpath) throws Exception {  
 try {  
 
  File file = new File(delpath);  
  // 當且僅當此抽象路徑名錶示的文件存在且 是一個目錄時,返回 true  
  if (!file.isDirectory()) {  
   file.delete();  
  } else if (file.isDirectory()) {  
   String[] filelist = file.list();  
   for (int i = 0; i < filelist.length; i++) {  
    File delfile = new File(delpath + "\\" + filelist[i]);  
    if (!delfile.isDirectory()) {  
     delfile.delete();  
     System.out  
       .println(delfile.getAbsolutePath() + "刪除文件成功");  
    } else if (delfile.isDirectory()) {  
     deletefile(delpath + "\\" + filelist[i]);  //如果這不是文件,而是一個文件夾,那麼這個文件夾就按照文件的程序來處理,刪除。
    }  
   }  
   System.out.println(file.getAbsolutePath()+"刪除成功");  
   file.delete();  
  }  
 
 } catch (FileNotFoundException e) {  
  System.out.println("deletefile() Exception:" + e.getMessage());  
 }  
 return true;  
}   
}

------- <a href="http://www.itheima.com" target="blank">android培訓</a>、<a href="http://www.itheima.com" target="blank">java培訓</a>、期待與您交流! ----------

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