Java文件操作类

1 继承JavaCore的File类,封装为统一文件概念的类。

  1 package waf.file;
  2 
  3 import java.io.*;
  4 
  5 
  6 /**
  7  * 
  8  * @author waf.wang
  9  *
 10  */
 11 public class File extends java.io.File
 12 {
 13     /**
 14      * 
 15      */
 16     private static final long serialVersionUID = -4251424686894034621L;
 17     private boolean bOpenForWrite=false;
 18     private boolean bOpenForRead=false;
 19     private boolean isSynchronizedWrite=false;
 20     public File(String pathname) 
 21     {
 22         super(pathname);
 23     }
 24 
 25 
 26     java.io.BufferedReader bufferReader=null;
 27     java.io.DataOutputStream objWriter=null; 
 28     java.io.DataInputStream objReader=null;
 29     public static void main(String[] args) 
 30     {
 31         String[] aa=new waf.file.File("d:\\temp\\src\\").list();
 32         waf.file.File file=new waf.file.File("e:\\asssbc.txt");
 33         file.openForWrite(true);
 34         file.openForRead();
 35         {
 36             String str=file.readLine();
 37             str=file.readLine();
 38             str=file.readLine();
 39             str=file.readLine();
 40             str=file.readLine();
 41             str=file.readLine();
 42             str=file.readLine();
 43             str=file.readLine();
 44         }
 45 
 46         {
 47             file.openForWrite(false);
 48             file.writeByte((byte)300);
 49             file.flush();
 50             file.writeString("abc锟叫癸拷");
 51             file.writeString("\r\n");
 52             file.writeString("abc锟叫癸拷");
 53             file.writeString("\r\n");
 54             file.flush();
 55             file.close();
 56         }
 57         {
 58             String strLine="";
 59             
 60             String strAry[]=null;
 61 
 62             System.out.println(file.readGB());
 63         }
 64         
 65         file.close();
 66 
 67     }
 68 
 69 
 70     
 71     public boolean openForRead()
 72     {
 73         
 74         boolean bRet=false;
 75         try 
 76         {
 77             if(this.exists())
 78             {
 79                 FileInputStream fis=new FileInputStream(this);
 80                 this.bufferReader=new BufferedReader(new InputStreamReader(fis,"utf-8"));
 81                 this.objReader= new DataInputStream(new BufferedInputStream(fis));
 82                 this.bOpenForRead=true;
 83                 bRet=true;
 84             }
 85         }
 86         catch (Exception e) 
 87         {
 88             e.printStackTrace();
 89             bRet=false;
 90         }
 91         return bRet;
 92     }
 93     
 94     public boolean close()
 95     {
 96         try 
 97         {
 98             if(this.objReader!=null)
 99             {
100                 this.objReader.close();
101                 this.bOpenForRead=false;
102             }
103             if(this.objWriter!=null)
104             {
105                 this.objWriter.flush();
106                 this.objWriter.close();
107                 this.bOpenForWrite=false;
108             }        
109         }
110         catch (IOException e) 
111         {
112             
113         }
114         
115         return true;
116     }
117     
118     public String readLine()
119     {
120         String strLine="";
121         if(this.bOpenForRead==false)
122         {
123             this.openForRead();
124         }
125         try 
126         {
127             if(this.canRead())
128             {
129                 strLine=this.bufferReader.readLine();
130             }
131         }
132         catch (IOException e) 
133         {
134             e.printStackTrace();
135         }
136         return strLine;
137     }
138     
139     public String readAll()
140     {
141         String strContent="";
142         
143         strContent=this.readAllByCharset("");
144         
145         return strContent;
146     }
147     
148     public String readGB()
149     {
150         return this.readAllByCharset("GBK");
151     }
152     
153     public String readUTF8()
154     {
155         return this.readAllByCharset("utf-8");
156     }
157     public String readAllByCharset(String strCharset)
158     {
159         String strContent="";
160         
161         byte[] byteData=this.readAllByte();
162         
163         if(byteData!=null)
164         {
165             try 
166             {
167                 if(strCharset.length()==0)
168                 {
169                     strContent=new String(byteData);
170                 }
171                 else
172                 {
173                     strContent=new String(byteData,strCharset);
174                 }
175             }
176             catch(UnsupportedEncodingException e) 
177             {
178                 e.printStackTrace();
179             }
180         }
181         return strContent;
182     }
183     public byte[] readAllByte()
184     {
185         if(this.bOpenForRead==false)
186         {
187             this.openForRead();
188         }
189         byte[] byteData=null;
190         try 
191         {
192             int iFileLen=(int)this.length();
193             
194             byteData=new byte[iFileLen];
195             
196             if(objReader!=null)
197             {
198                 this.objReader.read(byteData);
199             }
200         }
201         catch (IOException e) 
202         {
203             e.printStackTrace();
204         }
205         
206         return byteData;
207     }
208     
209     public boolean openForWrite(boolean bAppend)
210     {
211         boolean bRet=true;
212         try 
213         {
214             this.objWriter=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(this,bAppend)));
215             this.bOpenForWrite=true;
216         } 
217         catch (Exception e) 
218         {
219             e.printStackTrace();
220             bRet=false;
221         }
222         return bRet;
223     }
224     
225     public boolean writeLine(String line)
226     {
227         return this.writeByte((line+"\r\n").getBytes());
228     }
229     public boolean writeWinLine(String line)
230     {
231         return this.writeByte((line+"\r\n").getBytes());
232     }
233     public boolean writeUnixLine(String line)
234     {
235         return this.writeByte((line+"\n").getBytes());
236     }
237     public boolean writeString(String strContent)
238     {
239         return this.writeByte(strContent.getBytes());
240     }
241     
242     public boolean writeLineFlush(String line)
243     {
244         boolean ret=this.writeLine(line);
245         this.flush();
246         return ret;
247     }
248     public boolean writeStringFlush(String line)
249     {
250         boolean ret=this.writeString(line);
251         this.flush();
252         return ret;
253     }    
254     public boolean writeString(String strContent,String strCharset)
255     {
256         byte[] byteData=null;
257         try 
258         {
259             if(strCharset.length()==0)
260             {
261                 byteData = strContent.getBytes();
262             }
263             else
264             {
265                 byteData = strContent.getBytes(strCharset);
266             }
267         }
268         catch (UnsupportedEncodingException e) 
269         {
270             e.printStackTrace();
271         }
272         return this.writeByte(byteData);
273     }
274     
275     public boolean writeByte(byte aByte)
276     {
277         boolean bRet=true;
278         if(this.bOpenForWrite==false)
279         {
280             this.openForWrite(true);
281         }        
282         try 
283         {
284             if(this.canWrite())
285             {
286                 this.objWriter.write(new byte[]{aByte});
287                 bRet=true;
288             }
289             
290         } 
291         catch (IOException e) 
292         {
293             e.printStackTrace();
294             bRet=false;
295         }
296         return bRet;        
297         
298     }    
299     public boolean writeByte(byte[] byteContent)
300     {
301         boolean bRet=true;
302         if(this.bOpenForWrite==false)
303         {
304             this.openForWrite(true);
305         }        
306         try 
307         {
308             if(this.canWrite())
309                 
310             {
311 
312                 if(isSynchronizedWrite)
313                 {
314                     synchronized(objWriter)
315                     {
316                         this.objWriter.write(byteContent);
317                         this.objWriter.flush();
318                         bRet=true;
319                     }
320                 }
321                 else 
322                 {
323                     this.objWriter.write(byteContent);
324                     this.objWriter.flush();
325                     bRet=true;                
326                 }
327 
328             }
329             else 
330             {
331                 bRet=false;
332             }
333             
334         } 
335         catch (IOException e) 
336         {
337             e.printStackTrace();
338             bRet=false;
339         }
340         return bRet;
341     }
342     
343     public boolean writeInt(int i)
344     {
345         boolean bRet=true;
346         if(this.bOpenForWrite==false)
347         {
348             this.openForWrite(true);
349         }        
350         try 
351         {
352             if(this.canWrite())
353             {
354                 this.objWriter.writeInt(i);
355                 bRet=true;
356             }
357             
358         } 
359         catch (IOException e) 
360         {
361             e.printStackTrace();
362             bRet=false;
363         }
364         return bRet;
365     }    
366     
367     public boolean flush()
368     {
369         boolean bRet=false;
370         try 
371         {
372             if(objWriter!=null)
373             {
374                 this.objWriter.flush();
375                 bRet=true;
376             }
377         } 
378         catch (Exception e) 
379         {
380             bRet=false;
381             e.printStackTrace();
382         }
383         
384         return bRet;
385     }
386 
387 
388     public boolean isSynchronizedWrite()
389     {
390         return isSynchronizedWrite;
391     }
392     
393     public void setSynchronizedWrite(boolean isSynchronizedWrite)
394     {
395         this.isSynchronizedWrite = isSynchronizedWrite;
396     } 
397    
398 }
View Code

2 文件Util类

  1 package waf.file;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 import java.io.InputStream;
  9 import java.util.ArrayList;
 10 import java.util.List;
 11 
 12 import org.mozilla.universalchardet.UniversalDetector;
 13 
 14 import waf.convert.Conv;
 15 
 16 /**
 17  * 
 18  * @author waf.wang
 19  *
 20  */
 21 public class FileUtil
 22 {
 23 
 24     public static void main(String[] args) 
 25     {
 26         detectCharset("j:\\10w.csv");
 27         File[] files=FileUtil.listFiles("j:\\article\\jkzg-nhfpc,健康中国,MjM5NjI2ODA4Mw==");
 28         for (File file : files) 
 29         {
 30             if(file.isFile())
 31             {
 32                 System.out.println(detectCharset(file.getAbsolutePath()));
 33             }
 34         }
 35         
 36     }
 37     public static void writeLine(String filePath,String line)
 38     {
 39 
 40         waf.file.File file=new waf.file.File(filePath);
 41         file.openForWrite(true);
 42         file.writeLine(line);
 43         file.flush();
 44         file.close();
 45     }
 46 
 47     public static void write(String filePath,String content,boolean append)
 48     {
 49 
 50         waf.file.File file=new waf.file.File(filePath);
 51         file.openForWrite(append);
 52         file.writeString(content);
 53         file.flush();
 54         file.close();
 55     }
 56     public static void write(String filePath,byte[] bytes,boolean append)
 57     {
 58 
 59         waf.file.File file=new waf.file.File(filePath);
 60         file.openForWrite(append);
 61         file.writeByte(bytes);
 62         file.flush();
 63         file.close();
 64     }
 65     
 66 
 67     public static void writeLine(String filePath,String line,boolean append)
 68     {
 69 
 70         waf.file.File file=new waf.file.File(filePath);
 71         file.openForWrite(append);
 72         file.writeLine(line);
 73         file.flush();
 74         file.close();
 75     }
 76 
 77     public static void writeLines(String filePath,List<String> list)
 78     {
 79         waf.file.File file=new waf.file.File(filePath);
 80         file.openForWrite(true);
 81         for(String line:list)
 82         {
 83             file.writeLine(line);
 84         }
 85         file.flush();
 86         file.close();
 87     }
 88 
 89     public static String getContent(String filePath)
 90     {
 91         return new waf.file.File(filePath).readAll();
 92     }
 93 
 94     public static int getLines(String filePath)
 95     {
 96         int lines=0;
 97         String line=null;
 98         waf.file.File file=new waf.file.File(filePath);
 99         if(file.openForRead())
100         {
101             while((line=file.readLine())!=null)
102             {
103                 lines++;
104                 if(lines>100000&&lines%100000==0)
105                 {
106                     System.out.println(lines);
107                 }
108             }
109 
110         }
111         return lines;
112 
113     }
114 
115     public static List<String> getLineList(String filePath)
116     {
117         List<String> lst=new ArrayList<String>();
118         String line=null;
119         waf.file.File file=new waf.file.File(filePath);
120         if(file.openForRead())
121         {
122             while((line=file.readLine())!=null)
123             {
124                 lst.add(line);
125             }
126 
127         }
128         return lst;
129 
130     }
131 
132     public static void delete(String filePath)
133     {
134         waf.file.File file=new waf.file.File(filePath);
135         if(file.exists())
136         {
137             file.delete();
138         }
139     }
140 
141     public static String readAll(Class cls,String fileName)
142     {
143         String path=waf.system.System.getRunPath(cls)+fileName;
144         return readAll(path);
145     }
146     public static String readAll(String filePath)
147     {
148         String ret="";
149         waf.file.File file=new waf.file.File(filePath);
150         if(file.exists()&&file.openForRead())
151         {
152             ret=file.readAll();
153         }
154         return ret;
155     }
156 
157     public static String readAll(String filePath,String charaset)
158     {
159         String ret="";
160         waf.file.File file=new waf.file.File(filePath);
161         if(file.exists()&&file.openForRead())
162         {
163             ret=file.readAllByCharset(charaset);
164         }
165         return ret;
166     }
167 
168     public static byte[] readAllBytes(String filePath)
169     {
170         byte[] ret=null;
171         waf.file.File file=new waf.file.File(filePath);
172         if(file.exists()&&file.openForRead())
173         {
174             ret=file.readAllByte();
175         }
176         return ret;
177     }
178     
179     public static boolean detectCharsetByWords(String words)
180     {
181         boolean ret=false;
182         if(        words.contains("的") ||
183                 words.contains("了") ||
184                 words.contains("是") ||
185                 words.contains(",") ||
186                 words.contains("。")
187         )
188         {
189             ret=true;
190         }
191         return ret;
192     }
193     public static String detectCharset(String filePath)
194     {
195         String encoding="";
196         String encoding2="";
197         try
198         {
199             InputStream is=new FileInputStream(new File(filePath));
200             BufferedInputStream reader=new BufferedInputStream(is);
201             byte[] buff=new byte[1024];
202             int len=0;
203             // 检测文件编码
204             UniversalDetector detector=new UniversalDetector(null);
205             while((len=reader.read(buff))!=-1&&!detector.isDone())
206             {
207                 detector.handleData(buff,0,len);
208             }
209             detector.dataEnd();
210             encoding=detector.getDetectedCharset();
211             
212             if(encoding==null)
213             {
214                 if(len==-1)
215                 {
216                     encoding="UTF-8";
217                 }
218                 else 
219                 {
220                     String utf8=Conv.bytes2Utf8(buff);
221                     String gbk=Conv.bytes2Gbk(buff);                
222                     if(detectCharsetByWords(utf8))
223                     {
224                         encoding="utf-8";
225                     }
226                     
227                     if(detectCharsetByWords(gbk))
228                     {
229                         encoding="gbk";
230                     }
231                 }
232             }
233             
234 
235             
236             if(encoding==null)
237             {
238                 System.out.println(filePath+"No encoding detected.");
239             }
240 
241 
242             detector.reset();
243             reader.close();
244 
245         }
246         catch(IOException e)
247         {
248             e.printStackTrace();
249         }
250 
251         return encoding;
252     }
253 
254     public static List<String> listSubDirs(String parentDir)
255     {
256         List<String> ret=new ArrayList<String>();
257 
258         java.io.File[] files=new File(parentDir).listFiles();
259         for(java.io.File file:files)
260         {
261             if(file.isDirectory())
262             {
263                 ret.add(file.getAbsolutePath());
264             }
265         }
266         return ret;
267     }
268 
269     public static long length(String filePath)
270     {
271         waf.file.File file=new waf.file.File(filePath);
272         return file.length();
273     }
274 
275     public static boolean exists(String filePath)
276     {
277         waf.file.File file=new waf.file.File(filePath);
278         return file.exists();
279     }
280     
281     public static boolean exists(Class cls,String fileName)
282     {
283         String path=waf.system.System.getRunPath(cls)+fileName;
284         return exists(path);
285     }
286 
287     public static boolean mkdir(String filePath)
288     {
289         boolean ret=true;
290         waf.file.File file=new waf.file.File(filePath);
291         if(!file.exists())
292         {
293             ret=file.mkdirs();
294         }
295         return ret;
296     }
297 
298     public static boolean copyFile(String fileFrom,String fileTo)
299     {
300         return copyFile(fileFrom,fileTo,true,8192);
301     }
302 
303     public static boolean copyFile(InputStream is,String fileTo)
304     {
305         return copyFile(is,fileTo,4906);
306     }
307 
308     public static boolean copyFile(InputStream is,String fileTo,int bufsize)
309     {
310         try
311         {
312             FileOutputStream out=new FileOutputStream(fileTo);
313             byte[] bt=new byte[bufsize];
314             int count;
315             while((count=is.read(bt))>0)
316             {
317                 out.write(bt,0,count);
318             }
319             out.close();
320             return true;
321         }
322         catch(IOException ex)
323         {
324             return false;
325         }
326     }
327 
328     public static boolean copyFile(String fileFrom,String fileTo,boolean overwrite,int bufsize)
329     {
330         try
331         {
332             // waf.file.File destFile=new waf.file.File(fileTo);
333             // if(waf.file.File.exists(fileFrom) && destFile.exists() &&
334             // overwrite)
335             // {
336             // destFile.delete();
337             // }
338 
339             FileInputStream in=new java.io.FileInputStream(fileFrom);
340             // FileOutputStream out = new FileOutputStream(fileTo);
341             // byte[] bt = new byte[bufsize];
342             // int count;
343             // while ((count = in.read(bt)) > 0) {
344             // out.write(bt, 0, count);
345             // }
346             //            
347             // out.close();
348             copyFile(in,fileTo,bufsize);
349             in.close();
350             return true;
351         }
352         catch(IOException ex)
353         {
354             return false;
355         }
356     }
357 
358     public static File[] listFiles(String dir)
359     {
360         File file=new File(dir);
361         return file.listFiles();
362     }
363     public static String[] list(String dir)
364     {
365         return new File(dir).list();
366     }
367 
368     /**
369      * 删除文件
370      * 
371      * @param filePathAndName
372      *            String 文件路径及名称 如c:/fqf.txt
373      * @param fileContent
374      *            String
375      * @return boolean
376      */
377     public static void delFile(String filePathAndName)
378     {
379         try
380         {
381             String filePath=filePathAndName;
382             filePath=filePath.toString();
383             java.io.File myDelFile=new java.io.File(filePath);
384             myDelFile.delete();
385 
386         }
387         catch(Exception e)
388         {
389             System.out.println("删除文件操作出错");
390             e.printStackTrace();
391 
392         }
393 
394     }
395 
396     /**
397      * 删除文件夹
398      * 
399      * @param filePathAndName
400      *            String 文件夹路径及名称 如c:/fqf
401      * @param fileContent
402      *            String
403      * @return boolean
404      */
405     public static void delFolder(String folderPath)
406     {
407         try
408         {
409             delAllFile(folderPath); // 删除完里面所有内容
410             String filePath=folderPath;
411             filePath=filePath.toString();
412             java.io.File myFilePath=new java.io.File(filePath);
413             myFilePath.delete(); // 删除空文件夹
414 
415         }
416         catch(Exception e)
417         {
418             System.out.println("删除文件夹操作出错");
419             e.printStackTrace();
420 
421         }
422 
423     }
424 
425     /**
426      * 删除文件夹里面的所有文件
427      * 
428      * @param path
429      *            String 文件夹路径 如 c:/fqf
430      */
431     public static void delAllFile(String path)
432     {
433         File file=new File(path);
434         if(!file.exists())
435         {
436             return;
437         }
438         if(!file.isDirectory())
439         {
440             return;
441         }
442         String[] tempList=file.list();
443         File temp=null;
444         for(int i=0;i<tempList.length;i++)
445         {
446             if(path.endsWith(java.io.File.separator))
447             {
448                 temp=new File(path+tempList[i]);
449             }
450             else
451             {
452                 temp=new File(path+java.io.File.separator+tempList[i]);
453             }
454             if(temp.isFile())
455             {
456                 temp.delete();
457             }
458             if(temp.isDirectory())
459             {
460                 delAllFile(path+"/"+tempList[i]);// 先删除文件夹里面的文件
461                 delFolder(path+"/"+tempList[i]);// 再删除空文件夹
462             }
463         }
464     }
465 
466     /**
467      * 复制整个文件夹内容
468      * 
469      * @param oldPath
470      *            String 原文件路径 如:c:/fqf
471      * @param newPath
472      *            String 复制后路径 如:f:/fqf/ff
473      * @return boolean
474      */
475     public static void copyFolder(String oldPath,String newPath)
476     {
477 
478         try
479         {
480             (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
481             File a=new File(oldPath);
482             String[] file=a.list();
483             File temp=null;
484             for(int i=0;i<file.length;i++)
485             {
486                 if(oldPath.endsWith(java.io.File.separator))
487                 {
488                     temp=new File(oldPath+file[i]);
489                 }
490                 else
491                 {
492                     temp=new File(oldPath+java.io.File.separator+file[i]);
493                 }
494 
495                 if(temp.isFile())
496                 {
497                     FileInputStream input=new FileInputStream(temp);
498                     FileOutputStream output=new FileOutputStream(newPath+"/"+(temp.getName()).toString());
499                     byte[] b=new byte[1024*5];
500                     int len;
501                     while((len=input.read(b))!=-1)
502                     {
503                         output.write(b,0,len);
504                     }
505                     output.flush();
506                     output.close();
507                     input.close();
508                 }
509                 if(temp.isDirectory())
510                 {// 如果是子文件夹
511                     copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
512                 }
513             }
514         }
515         catch(Exception e)
516         {
517             System.out.println("复制整个文件夹内容操作出错");
518             e.printStackTrace();
519 
520         }
521 
522     }
523 
524     /**
525      * 移动文件到指定目录
526      * 
527      * @param oldPath
528      *            String 如:c:/fqf.txt
529      * @param newPath
530      *            String 如:d:/fqf.txt
531      */
532     public static void moveFile(String oldPath,String newPath)
533     {
534         copyFile(oldPath,newPath);
535         delFile(oldPath);
536 
537     }
538 
539     /**
540      * 移动文件到指定目录
541      * 
542      * @param oldPath
543      *            String 如:c:/fqf.txt
544      * @param newPath
545      *            String 如:d:/fqf.txt
546      */
547     public static void moveFolder(String oldPath,String newPath)
548     {
549         copyFolder(oldPath,newPath);
550         delFolder(oldPath);
551 
552     }
553 }
View Code

3 Ini文件类

  1 package waf.file;
  2 
  3 import java.io.*;
  4 import java.util.Enumeration;
  5 import java.util.HashMap;
  6 import java.util.Map;
  7 import java.util.Properties;
  8 
  9 
 10 /**
 11  * 
 12  * @author waf.wang
 13  *
 14  */
 15 
 16 public class IniFile 
 17 {
 18     private  Map<String,String> confData=new HashMap<String,String>();
 19     
 20     private String filePath="";
 21     
 22     public void load(String filePath)
 23     {
 24         this.filePath=filePath;
 25         this.confData=readIni(filePath);
 26     }
 27     public String getString(String key) 
 28     {
 29         return this.confData.get(key);
 30     }
 31     public Integer getInt(String key) 
 32     {
 33         Integer ret=null;
 34         String value=getString(key);
 35         if(waf.convert.Conv.isInt(value))
 36         {
 37             ret=Integer.parseInt(value);
 38         }
 39         return ret;
 40     }    
 41     
 42     public void writeString(String key,String value)
 43     {
 44         writeIni(this.filePath,key,value);
 45     }
 46     
 47     
 48     
 49     
 50     
 51 //    根据key读取value    
 52     public static String readString(String filePath,String key) 
 53     {    
 54         Properties props = new Properties();    
 55         try 
 56         {    
 57             InputStream in = new BufferedInputStream (new FileInputStream(filePath));    
 58             props.load(in);    
 59             String value = props.getProperty (key);    
 60             return value;    
 61         }
 62         catch (Exception e) 
 63         {    
 64             e.printStackTrace();    
 65             return null;    
 66         }    
 67     }
 68     
 69     public static int readInt(String filePath,String key)
 70     {
 71         return Integer.parseInt(readString(filePath,key));
 72     }
 73 //    读取properties的全部信息
 74  
 75     public static Map<String,String> readIni(String filePath) 
 76     {    
 77         Map<String,String> map=new HashMap<String,String>();
 78         Properties props = new Properties();    
 79         try 
 80         {    
 81             InputStream in = new BufferedInputStream (new FileInputStream(filePath));    
 82             props.load(in);    
 83             Enumeration en = props.propertyNames();    
 84             while (en.hasMoreElements()) 
 85             {    
 86                 String key = (String) en.nextElement();    
 87                 String Property = props.getProperty (key);
 88 //                Property=Property.replace("\t", "");
 89 //                Property=Property.replace(" ", "");
 90                 //Property=Property.trim();
 91                 map.put(key, Property);
 92             } 
 93             in.close();
 94         } 
 95         catch (Exception e) 
 96         {    
 97             e.printStackTrace();    
 98         }
 99         return map;
100     }    
101     
102     
103        public static void writeIni(String filePath,String parameterName,String parameterValue) {    
104               Properties prop = new Properties();    
105              try {    
106                InputStream fis = new FileInputStream(filePath);    
107                     //从输入流中读取属性列表(键和元素对)    
108                      prop.load(fis);    
109                     //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。    
110                     //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。    
111                      OutputStream fos = new FileOutputStream(filePath);    
112                      prop.setProperty(parameterName, parameterValue);    
113                     //以适合使用 load 方法加载到 Properties 表中的格式,    
114                     //将此 Properties 表中的属性列表(键和元素对)写入输出流    
115                      //prop.store(fos, "Update '" + parameterName + "' value");
116                      prop.store(fos,"");
117                  } 
118                  catch (IOException e) 
119                  {    
120                   System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");    
121                  }    
122              }    
123        
124     public static void main(String[] args)
125     {
126         
127     }
128 }
View Code

4 Ini文件管理类

  1 package waf.file;
  2 
  3 import java.util.HashMap;
  4 import java.util.Map;
  5 
  6 /**
  7  * 
  8  * @author waf.wang
  9  *
 10  */
 11 
 12 /**
 13  * 在servlet中不能IniFileManager.load()方式使用,因为隐含使用了同一个静态变量
 14  * 所有的的servlet在同一个进程空间内,静态变量会导致脏数据覆盖
 15  *
 16  */
 17 public class IniFileManager
 18 {
 19     private static IniFileManager instance = null;
 20     private static Map<String,IniFile> iniMap=new HashMap<String,IniFile>();
 21     private static IniFile firstIni=null;
 22 
 23     public static synchronized IniFileManager getInstance() 
 24     {
 25         if(instance==null)
 26         {
 27             instance=new IniFileManager();
 28         }
 29         return instance;
 30     }
 31     
 32     public static void load(String fileCode,String filePath)
 33     {
 34         IniFile ini=new IniFile();
 35         ini.load(filePath);
 36         iniMap.put(fileCode,ini);
 37     }
 38     
 39     public static void load(Class cls,String fileName)
 40     {
 41         String path=waf.system.System.getRunPath(cls)+fileName;
 42         load(path);
 43     }
 44     
 45     public static void load(String filePath)
 46     {
 47         load("",filePath);
 48     }    
 49 
 50     public static String readString(String fileCode,String key) 
 51     {
 52         String ret="";
 53         IniFile ini=iniMap.get(fileCode);
 54         if(ini!=null)
 55         {
 56             ret=ini.getString(key);
 57         }
 58         return ret;
 59     }
 60     public static int readInt(String fileCode,String key) 
 61     {
 62         int ret=0;
 63         IniFile ini=iniMap.get(fileCode);
 64         if(ini!=null)
 65         {
 66             ret=ini.getInt(key);
 67         }
 68         return ret;
 69     }
 70     
 71     public static void writeInt(String key,int value) 
 72     {
 73         int ret=0;
 74         getFirstIniFile();
 75 
 76         if(firstIni!=null)
 77         {
 78             firstIni.writeString(key,""+value);
 79         }
 80     }
 81     
 82     public static void writeString(String key,String value) 
 83     {
 84         int ret=0;
 85         getFirstIniFile();
 86 
 87         if(firstIni!=null)
 88         {
 89             firstIni.writeString(key,value);
 90         }
 91     }
 92     
 93     public static String readString(String key) 
 94     {
 95         String ret="";
 96         
 97         getFirstIniFile();
 98 
 99         if(firstIni!=null)
100         {
101             ret=firstIni.getString(key);
102         }
103         return ret;
104     }
105     public static Integer readInt(String key) 
106     {
107         Integer ret=null;
108         
109         getFirstIniFile();
110 
111         if(firstIni!=null)
112         {
113             ret=firstIni.getInt(key);
114         }
115         return ret;
116     }
117     
118     private static IniFile getFirstIniFile()
119     {
120         //if(firstIni==null)
121         {
122             for(String fileCode:iniMap.keySet())
123             {
124                 firstIni=iniMap.get(fileCode);
125                 break;
126             }            
127         }
128         return firstIni;
129     }
130     
131     
132 
133     
134 }
View Code
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值