posts - 78,  comments - 48,  trackbacks - 0
  1 /*
  2  * Made In GamVan.com
  3  * Created on 2005年3月18日, 下午8:37
  4   */

  5 package  com.gamvan.tools;
  6 import  java.io.BufferedReader;
  7 import  java.io.File;
  8 import  java.io.FileInputStream;
  9 import  java.io.FileOutputStream;
 10 import  java.io.FileWriter;
 11 import  java.io.IOException;
 12 import  java.io.InputStream;
 13 import  java.io.InputStreamReader;
 14 import  java.io.PrintWriter;
 15 import  java.util.StringTokenizer;
 16 public   class  FileOperate  {
 17      private  String message;
 18      public  FileOperate()  {
 19     }

 20
 21      /**
 22      * 读取文本文件内容
 23      *  @param  filePathAndName 带有完整绝对路径的文件名
 24      *  @param  encoding 文本文件打开的编码方式
 25      *  @return  返回文本文件的内容
 26       */

 27      public  String readTxt(String filePathAndName,String encoding)  throws  IOException {
 28      encoding  =  encoding.trim();
 29      StringBuffer str  =   new  StringBuffer( "" );
 30      String st  =   "" ;
 31       try {
 32       FileInputStream fs  =   new  FileInputStream(filePathAndName);
 33       InputStreamReader isr;
 34        if (encoding.equals( "" )) {
 35        isr  =   new  InputStreamReader(fs);
 36       }
else {
 37        isr  =   new  InputStreamReader(fs,encoding);
 38       }

 39       BufferedReader br  =   new  BufferedReader(isr);
 40        try {
 41        String data  =   "" ;
 42         while ((data  =  br.readLine()) != null ) {
 43          str.append(data + "   " ); 
 44        }

 45       }
catch (Exception e) {
 46        str.append(e.toString());
 47       }

 48       st  =  str.toString();
 49      }
catch (IOException es) {
 50       st  =   "" ;
 51      }

 52       return  st;     
 53     }

 54
 55      /**
 56      * 新建目录
 57      *  @param  folderPath 目录
 58      *  @return  返回目录创建后的路径
 59       */

 60      public  String createFolder(String folderPath)  {
 61         String txt  =  folderPath;
 62          try   {
 63             java.io.File myFilePath  =   new  java.io.File(txt);
 64             txt  =  folderPath;
 65              if  ( ! myFilePath.exists())  {
 66                 myFilePath.mkdir();
 67             }

 68         }

 69          catch  (Exception e)  {
 70             message  =   " 创建目录操作出错 " ;
 71         }

 72          return  txt;
 73     }

 74     
 75      /**
 76      * 多级目录创建
 77      *  @param  folderPath 准备要在本级目录下创建新目录的目录路径 例如 c:myf
 78      *  @param  paths 无限级目录参数,各级目录以单数线区分 例如 a|b|c
 79      *  @return  返回创建文件后的路径 例如 c:myfac
 80       */

 81      public  String createFolders(String folderPath, String paths) {
 82         String txts  =  folderPath;
 83          try {
 84             String txt;
 85             txts  =  folderPath;
 86             StringTokenizer st  =   new  StringTokenizer(paths, " | " );
 87              for ( int  i = 0 ; st.hasMoreTokens(); i ++ ) {
 88                     txt  =  st.nextToken().trim();
 89                      if (txts.lastIndexOf( " / " ) !=- 1 )
 90                         txts  =  createFolder(txts + txt);
 91                     }
else {
 92                         txts  =  createFolder(txts + txt + " / " );    
 93                     }

 94             }

 95        }
catch (Exception e) {
 96            message  =   " 创建目录操作出错! " ;
 97        }

 98          return  txts;
 99     }

100
101     
102      /**
103      * 新建文件
104      *  @param  filePathAndName 文本文件完整绝对路径及文件名
105      *  @param  fileContent 文本文件内容
106      *  @return
107       */

108      public   void  createFile(String filePathAndName, String fileContent)  {
109      
110          try   {
111             String filePath  =  filePathAndName;
112             filePath  =  filePath.toString();
113             File myFilePath  =   new  File(filePath);
114              if  ( ! myFilePath.exists())  {
115                 myFilePath.createNewFile();
116             }

117             FileWriter resultFile  =   new  FileWriter(myFilePath);
118             PrintWriter myFile  =   new  PrintWriter(resultFile);
119             String strContent  =  fileContent;
120             myFile.println(strContent);
121             myFile.close();
122             resultFile.close();
123         }

124          catch  (Exception e)  {
125             message  =   " 创建文件操作出错 " ;
126         }

127     }

128
129
130      /**
131      * 有编码方式的文件创建
132      *  @param  filePathAndName 文本文件完整绝对路径及文件名
133      *  @param  fileContent 文本文件内容
134      *  @param  encoding 编码方式 例如 GBK 或者 UTF-8
135      *  @return
136       */

137      public   void  createFile(String filePathAndName, String fileContent, String encoding)  {
138      
139          try   {
140             String filePath  =  filePathAndName;
141             filePath  =  filePath.toString();
142             File myFilePath  =   new  File(filePath);
143              if  ( ! myFilePath.exists())  {
144                 myFilePath.createNewFile();
145             }

146             PrintWriter myFile  =   new  PrintWriter(myFilePath,encoding);
147             String strContent  =  fileContent;
148             myFile.println(strContent);
149             myFile.close();
150         }

151          catch  (Exception e)  {
152             message  =   " 创建文件操作出错 " ;
153         }

154     }
 
155
156
157      /**
158      * 删除文件
159      *  @param  filePathAndName 文本文件完整绝对路径及文件名
160      *  @return  Boolean 成功删除返回true遭遇异常返回false
161       */

162      public   boolean  delFile(String filePathAndName)  {
163       boolean  bea  =   false ;
164          try   {
165             String filePath  =  filePathAndName;
166             File myDelFile  =   new  File(filePath);
167              if (myDelFile.exists()) {
168              myDelFile.delete();
169              bea  =   true ;
170             }
else {
171              bea  =   false ;
172              message  =  (filePathAndName + " <br>删除文件操作出错 " );
173             }

174         }

175          catch  (Exception e)  {
176             message  =  e.toString();
177         }

178          return  bea;
179     }

180     
181
182
183      /**
184      * 删除文件夹
185      *  @param  folderPath 文件夹完整绝对路径
186      *  @return
187       */

188      public   void  delFolder(String folderPath)  {
189          try   {
190             delAllFile(folderPath);  // 删除完里面所有内容
191             String filePath  =  folderPath;
192             filePath  =  filePath.toString();
193             java.io.File myFilePath  =   new  java.io.File(filePath);
194             myFilePath.delete();  // 删除空文件夹
195         }

196          catch  (Exception e)  {
197             message  =  ( " 删除文件夹操作出错 " );
198         }

199     }

200     
201     
202      /**
203      * 删除指定文件夹下所有文件
204      *  @param  path 文件夹完整绝对路径
205      *  @return
206      *  @return
207       */

208      public   boolean  delAllFile(String path)  {
209       boolean  bea  =   false ;
210         File file  =   new  File(path);
211          if  ( ! file.exists())  {
212              return  bea;
213         }

214          if  ( ! file.isDirectory())  {
215              return  bea;
216         }

217         String[] tempList  =  file.list();
218         File temp  =   null ;
219          for  ( int  i  =   0 ; i  <  tempList.length; i ++ {
220              if  (path.endsWith(File.separator))  {
221                 temp  =   new  File(path  +  tempList[i]);
222             }
else {
223                 temp  =   new  File(path  +  File.separator  +  tempList[i]);
224             }

225              if  (temp.isFile())  {
226                 temp.delete();
227             }

228              if  (temp.isDirectory())  {
229                 delAllFile(path + " / " +  tempList[i]); // 先删除文件夹里面的文件
230                 delFolder(path + " / " +  tempList[i]); // 再删除空文件夹
231                 bea  =   true ;
232             }

233         }

234          return  bea;
235     }

236
237
238      /**
239      * 复制单个文件
240      *  @param  oldPathFile 准备复制的文件源
241      *  @param  newPathFile 拷贝到新绝对路径带文件名
242      *  @return
243       */

244      public   void  copyFile(String oldPathFile, String newPathFile)  {
245          try   {
246              int  bytesum  =   0 ;
247              int  byteread  =   0 ;
248             File oldfile  =   new  File(oldPathFile);
249              if  (oldfile.exists())  // 文件存在时
250                 InputStream inStream  =   new  FileInputStream(oldPathFile);  // 读入原文件
251                 FileOutputStream fs  =   new  FileOutputStream(newPathFile);
252                  byte [] buffer  =   new   byte [ 1444 ];
253                  while ((byteread  =  inStream.read(buffer))  !=   - 1 ) {
254                     bytesum  +=  byteread;  // 字节数 文件大小
255                     System.out.println(bytesum);
256                     fs.write(buffer,  0 , byteread);
257                 }

258                 inStream.close();
259             }

260         }
catch  (Exception e)  {
261             message  =  ( " 复制单个文件操作出错 " );
262         }

263     }

264     
265
266      /**
267      * 复制整个文件夹的内容
268      *  @param  oldPath 准备拷贝的目录
269      *  @param  newPath 指定绝对路径的新目录
270      *  @return
271       */

272      public   void  copyFolder(String oldPath, String newPath)  {
273          try   {
274              new  File(newPath).mkdirs();  // 如果文件夹不存在 则建立新文件夹
275             File a = new  File(oldPath);
276             String[] file = a.list();
277             File temp = null ;
278              for  ( int  i  =   0 ; i  <  file.length; i ++ {
279                  if (oldPath.endsWith(File.separator)) {
280                     temp = new  File(oldPath + file[i]);
281                 }
else {
282                     temp = new  File(oldPath + File.separator + file[i]);
283                 }

284                  if (temp.isFile()) {
285                     FileInputStream input  =   new  FileInputStream(temp);
286                     FileOutputStream output  =   new  FileOutputStream(newPath  +   " / "   +
287                     (temp.getName()).toString());
288                      byte [] b  =   new   byte [ 1024   *   5 ];
289                      int  len;
290                      while  ((len  =  input.read(b))  !=   - 1 {
291                         output.write(b,  0 , len);
292                     }

293                     output.flush();
294                     output.close();
295                     input.close();
296                 }

297                  if (temp.isDirectory()) { // 如果是子文件夹
298                     copyFolder(oldPath + " / " + file[i],newPath + " / " + file[i]);
299                 }

300             }

301         }
catch  (Exception e)  {
302             message  =   " 复制整个文件夹内容操作出错 " ;
303         }

304     }

305
306
307      /**
308      * 移动文件
309      *  @param  oldPath
310      *  @param  newPath
311      *  @return
312       */

313      public   void  moveFile(String oldPath, String newPath)  {
314         copyFile(oldPath, newPath);
315         delFile(oldPath);
316     }

317     
318
319      /**
320      * 移动目录
321      *  @param  oldPath
322      *  @param  newPath
323      *  @return
324       */

325      public   void  moveFolder(String oldPath, String newPath)  {
326         copyFolder(oldPath, newPath);
327         delFolder(oldPath);
328     }

329      public  String getMessage() {
330          return   this .message;
331     }

332 }

333
334
335
336   
posted on 2006-07-27 13:03 黑咖啡 阅读(316) 评论(0)  编辑  收藏 所属分类: Tec Article

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


网站导航:
 

<2006年7月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

留言簿(2)

随笔分类(67)

文章分类(43)

Good Article

Good Blogs

Open Source

最新随笔

最新评论

阅读排行榜

评论排行榜