雪山飛狐

          雪山飛狐

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            1 Posts :: 2 Stories :: 0 Comments :: 0 Trackbacks
          1. /** 
          2.      * 新建目錄 
          3.      * @param folderPath 目錄 
          4.      * @return 返回目錄創(chuàng)建后的路徑 
          5.      */  
          6.     public String createFolder(String folderPath){  
          7.         String txt = folderPath;  
          8.         File myFilePath = new File(txt);  
          9.         txt = folderPath;  
          10.         if(!myFilePath.exists()){  
          11.             myFilePath.mkdir();  
          12.         }  
          13.         return txt;       
          14.     } 


          1. /** 
          2.      * 讀取文本文件內(nèi)容 
          3.      * @param filePathAndName 帶有完整絕對(duì)路徑的文件名 
          4.      * @param encoding 文本文件打開(kāi)的編碼方式 
          5.      * @return 返回文本文件的內(nèi)容 
          6.      */  
          7.     public String readTxt(String filePathAndName,String encoding){  
          8.         encoding = encoding.trim();  
          9.         StringBuffer str = new StringBuffer("");  
          10.         String st = "";  
          11.         try {  
          12.             FileInputStream fs = new FileInputStream(filePathAndName);  
          13.             InputStreamReader isr ;  
          14.             if (encoding.equals("")){  
          15.                 isr = new InputStreamReader(fs);  
          16.             }else{  
          17.                 isr = new InputStreamReader(fs,encoding);  
          18.             }  
          19.             BufferedReader br = new BufferedReader(isr);  
          20.             String data = "";  
          21.             while((data = br.readLine())!=null){  
          22.                 str.append(data+" ");  
          23.             }  
          24.             st = str.toString();  
          25.         } catch (FileNotFoundException e) {  
          26.             e.printStackTrace();  
          27.         } catch (UnsupportedEncodingException e) {  
          28.             e.printStackTrace();  
          29.         } catch (IOException e) {  
          30.             e.printStackTrace();  
          31.         }  
          32.         return st;  
          33.           
          34.     } 

          1. /** 
          2.      * 刪除文件夾 
          3.      * @param folderPath 文件夾完整絕對(duì)路徑 
          4.      * @return 
          5.      */  
          6.     public void delFolder(String folderPath) {  
          7.         try {  
          8.             delAllFile(folderPath); //刪除完里面所有內(nèi)容  
          9.             String filePath = folderPath;  
          10.             filePath = filePath.toString();  
          11.             java.io.File myFilePath = new java.io.File(filePath);  
          12.             myFilePath.delete(); //刪除空文件夾  
          13.         }  
          14.         catch (Exception e) {  
          15.             message = ("刪除文件夾操作出錯(cuò)");  
          16.         }  
          17.     } 

          多級(jí)目錄創(chuàng)建
          1. /** 
          2.      * 多 級(jí)目錄創(chuàng)建 
          3.      * @param folderPath 準(zhǔn) 備要在本級(jí)目錄下創(chuàng)建新目錄的目錄路徑 例如 c:myf 
          4.      * @param paths 無(wú)限級(jí)目錄參數(shù),各級(jí)目錄以單數(shù)線區(qū)分 例如 a|b|c 
          5.      * @return 返回創(chuàng)建文件后的路徑 例如 c:myfac 
          6.      */  
          7.     public String createFolders(String folderPath,String paths){  
          8.         String txts = folderPath;  
          9.         String txt ;   
          10.         //如果需要無(wú)限極創(chuàng)建目錄,以"|"分開(kāi) 例"aa|bb|cc|"  
          11.         StringTokenizer st = new StringTokenizer(paths,"|");  
          12.         for(int i=0;st.hasMoreTokens();i++){  
          13.             txt = st.nextToken().trim();  
          14.             if(txts.lastIndexOf("/")!= -1){  
          15.                 txts = createFolder(txts+txt);  
          16.             }else{  
          17.                 txts = createFolder(txts+txt+"/");  
          18.             }  
          19.         }  
          20.         return txts;  
          21.     } 

          Java代碼
          1. /** 
          2.      * 新 建文件 
          3.      * @param filePathAndName 文 本文件完整絕對(duì)路徑及文件名 
          4.      * @param fileContent 文 本文件內(nèi)容 
          5.      * @return 
          6.      */  
          7.     public void createFile(String filePathAndName,String fileContent){  
          8.         try{  
          9.         String filePath = filePathAndName;  
          10.         filePath = filePath.toString();  
          11.         File myFilePath = new File(filePath);  
          12.         if(!myFilePath.exists()){  
          13.             myFilePath.createNewFile();  
          14.         }  
          15.         FileWriter resultFile = new FileWriter(myFilePath);  
          16.         PrintWriter myFile = new PrintWriter(resultFile);  
          17.         String strContent = fileContent;  
          18.         myFile.println(strContent);  
          19.         myFile.close();  
          20.         resultFile.close();  
          21.         }catch (IOException e) {  
          22.   
          23.         }  
          24.     } 


          1. /** 
          2.      * 有編碼方式的文件創(chuàng)建 
          3.      * @param filePathAndName 文本文件完整絕對(duì)路徑及文件名 
          4.      * @param fileContent 文本文件內(nèi)容 
          5.      * @param encoding 編碼方式 例如 GBK 或者 UTF-8 
          6.      * @return 
          7.      */  
          8.     public void createFile(String filePathAndName, String fileContent, String encoding) {  
          9.        
          10.         try {  
          11.             String filePath = filePathAndName;  
          12.             filePath = filePath.toString();  
          13.             File myFilePath = new File(filePath);  
          14.             if (!myFilePath.exists()) {  
          15.                 myFilePath.createNewFile();  
          16.             }  
          17.             PrintWriter myFile = new PrintWriter(myFilePath,encoding);  
          18.             String strContent = fileContent;  
          19.             myFile.println(strContent);  
          20.             myFile.close();  
          21.         }  
          22.         catch (Exception e) {  
          23.             message = "創(chuàng)建文件操作出錯(cuò)";  
          24.         }  
          25.     }  

          1. /** 
          2.      * 刪除文件 
          3.      * @param filePathAndName 文本文件完整絕對(duì)路徑及文件名 
          4.      * @return Boolean 成功刪除返回true遭遇異常返回false 
          5.      */  
          6.     public boolean delFile(String filePathAndName) {  
          7.      boolean bea = false;  
          8.         try {  
          9.             String filePath = filePathAndName;  
          10.             File myDelFile = new File(filePath);  
          11.             if(myDelFile.exists()){  
          12.              myDelFile.delete();  
          13.              bea = true;  
          14.             }else{  
          15.              bea = false;  
          16.              message = (filePathAndName+"  
          17. 刪 除文件操作出錯(cuò)");  
          18.             }  
          19.         }  
          20.         catch (Exception e) {  
          21.             message = e.toString();  
          22.         }  
          23.         return bea;  
          24.     } 


          1. /** 
          2.      * 刪除文件夾 
          3.      * @param folderPath 文件夾完整絕對(duì)路徑 
          4.      * @return 
          5.      */  
          6.     public void delFolder(String folderPath) {  
          7.         try {  
          8.             delAllFile(folderPath); //刪除完里面所有內(nèi)容  
          9.             String filePath = folderPath;  
          10.             filePath = filePath.toString();  
          11.             java.io.File myFilePath = new java.io.File(filePath);  
          12.             myFilePath.delete(); //刪除空文件夾  
          13.         }  
          14.         catch (Exception e) {  
          15.             message = ("刪除文件夾操作出錯(cuò)");  
          16.         }  
          17.     } 

          1. /** 
          2.      * 刪除指定文件夾下所有文件 
          3.      * @param path 文件夾完整絕對(duì)路徑 
          4.      * @return 
          5.      * @return 
          6.      */  
          7.     public boolean delAllFile(String path) {  
          8.      boolean bea = false;  
          9.         File file = new File(path);  
          10.         if (!file.exists()) {  
          11.             return bea;  
          12.         }  
          13.         if (!file.isDirectory()) {  
          14.             return bea;  
          15.         }  
          16.         String[] tempList = file.list();  
          17.         File temp = null;  
          18.         for (int i = 0; i < tempList.length; i++) {  
          19.             if (path.endsWith(File.separator)) {  
          20.                 temp = new File(path + tempList[i]);  
          21.             }else{  
          22.                 temp = new File(path + File.separator + tempList[i]);  
          23.             }  
          24.             if (temp.isFile()) {  
          25.                 temp.delete();  
          26.             }  
          27.             if (temp.isDirectory()) {  
          28.                 delAllFile(path+"/"+ tempList[i]);//先刪除文件夾里面的文件  
          29.                 delFolder(path+"/"+ tempList[i]);//再刪除空文件夾  
          30.                 bea = true;  
          31.             }  
          32.         }  
          33.         return bea;  
          34.     } 


          1. /** 
          2.      * 復(fù)制單個(gè)文件 
          3.      * @param oldPathFile 準(zhǔn)備復(fù)制的文件源 
          4.      * @param newPathFile 拷貝到新絕對(duì)路徑帶文件名 
          5.      * @return 
          6.      */  
          7.     public void copyFile(String oldPathFile, String newPathFile) {  
          8.         try {  
          9.             int bytesum = 0;  
          10.             int byteread = 0;  
          11.             File oldfile = new File(oldPathFile);  
          12.             if (oldfile.exists()) { //文件存在時(shí)  
          13.                 InputStream inStream = new FileInputStream(oldPathFile); //讀入原文件  
          14.                 FileOutputStream fs = new FileOutputStream(newPathFile);  
          15.                 byte[] buffer = new byte[1444];  
          16.                 while((byteread = inStream.read(buffer)) != -1){  
          17.                     bytesum += byteread; //字節(jié)數(shù) 文件大小  
          18.                     System.out.println(bytesum);  
          19.                     fs.write(buffer, 0, byteread);  
          20.                 }  
          21.                 inStream.close();  
          22.             }  
          23.         }catch (Exception e) {  
          24.             message = ("復(fù)制單個(gè)文件操作出錯(cuò)");  
          25.         }  
          26.     } 


          1. /** 
          2.      * 復(fù)制整個(gè)文件夾的內(nèi)容 
          3.      * @param oldPath 準(zhǔn)備拷貝的目錄 
          4.      * @param newPath 指定絕對(duì)路徑的新目錄 
          5.      * @return 
          6.      */  
          7.     public void copyFolder(String oldPath, String newPath) {  
          8.         try {  
          9.             new File(newPath).mkdirs(); //如果文件夾不存在 則建立新文件夾  
          10.             File a=new File(oldPath);  
          11.             String[] file=a.list();  
          12.             File temp=null;  
          13.             for (int i = 0; i < file.length; i++) {  
          14.                 if(oldPath.endsWith(File.separator)){  
          15.                     temp=new File(oldPath+file[i]);  
          16.                 }else{  
          17.                     temp=new File(oldPath+File.separator+file[i]);  
          18.                 }  
          19.                 if(temp.isFile()){  
          20.                     FileInputStream input = new FileInputStream(temp);  
          21.                     FileOutputStream output = new FileOutputStream(newPath + "/" +  
          22.                     (temp.getName()).toString());  
          23.                     byte[] b = new byte[1024 * 5];  
          24.                     int len;  
          25.                     while ((len = input.read(b)) != -1) {  
          26.                         output.write(b, 0, len);  
          27.                     }  
          28.                     output.flush();  
          29.                     output.close();  
          30.                     input.close();  
          31.                 }  
          32.                 if(temp.isDirectory()){//如果是子文件夾  
          33.                     copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);  
          34.                 }  
          35.             }  
          36.         }catch (Exception e) {  
          37.             message = "復(fù)制整個(gè)文件夾內(nèi)容操作出錯(cuò)";  
          38.         }  
          39.     }



          1. /** 
          2.      * 移動(dòng)文件 
          3.      * @param oldPath 
          4.      * @param newPath 
          5.      * @return 
          6.      */  
          7.     public void moveFile(String oldPath, String newPath) {  
          8.         copyFile(oldPath, newPath);  
          9.         delFile(oldPath);  
          10.     } 


          1. /** 
          2.      * 移動(dòng)目錄 
          3.      * @param oldPath 
          4.      * @param newPath 
          5.      * @return 
          6.      */  
          7.     public void moveFolder(String oldPath, String newPath) {  
          8.         copyFolder(oldPath, newPath);  
          9.         delFolder(oldPath);  
          10.     } 









          posted on 2010-04-20 19:47 犀利哥 閱讀(99) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 郑州市| 新乡市| 澄城县| 黎城县| 上林县| 阳原县| 阜南县| 湄潭县| 赤壁市| 灵台县| 喀喇沁旗| 方山县| 确山县| 沾益县| 金平| 喀什市| 漾濞| 朝阳县| 垣曲县| 抚宁县| 轮台县| 金阳县| 宁海县| 冀州市| 六盘水市| 巴东县| 沈阳市| 特克斯县| 德钦县| 莲花县| 永康市| 永胜县| 潞西市| 德州市| 泰州市| 南和县| 山丹县| 北流市| 漳浦县| 鸡西市| 金溪县|