Java读文件写文件操作(续)

Java读文件写文件操作(续)

 1  package  velcro.util;  
 2    
 3  import  java.io.File;  
 4  import  java.io.FileWriter;  
 5  import  java.io.IOException;  
 6    
 7  /**  
 8   * 对文本文件进行读写操作 
 9    */   
10  public   class  WriteAndReadText {  
11       /**  
12       * 文本文件所在的目录 
13        */   
14       private  String textPath;  
15       /**  
16       * 读取文本内容 
17       *  @param  textname 文本名称 
18       *  @return  
19        */   
20       public  String readText(String textname){  
21          File file = new  File(textPath + File.separator + textname);  
22           try  {  
23              BufferedReader br  =   new  BufferedReader( new  java.io.FileReader(file));  
24              StringBuffer sb  =   new  StringBuffer();  
25              String line  =  br.readLine();  
26               while  (line  !=   null ) {  
27                  sb.append(line);  
28                  line  =  br.readLine();  
29              }  
30              br.close();  
31               return  sb.toString();  
32          }  catch  (IOException e) {  
33              LogInfo.error( this .getClass().getName(),e.getLocalizedMessage(),e);  
34              e.printStackTrace();  
35               return   null ;  
36          }  
37      }  
38      }  
39       /**  
40       * 将内容写到文本中 
41       *  @param  textname 文本名称 
42       *  @param  date 写入的内容 
43       *  @return  
44        */   
45       public   boolean  writeText(String textname,String date){  
46           boolean  flag = false ;  
47          File filePath = new  File(textPath);  
48           if ( ! filePath.exists()){  
49              filePath.mkdirs();  
50          }  
51           try  {  
52              FileWriter fw  = new  FileWriter(textPath + File.separator + textname);  
53              fw.write(date);  
54              flag = true ;  
55               if (fw != null )  
56                  fw.close();  
57          }  catch  (IOException e) {  
58              LogInfo.error( this .getClass().getName(),e.getMessage(),e);  
59              e.printStackTrace();  
60          }  
61    
62           return  flag;          
63      }  
64       /**  
65       * 在文档后附加内容 
66       *  @param  textName 
67       *  @param  date 
68       *  @return  
69        */   
70       public   boolean  appendText(String textName,String date){  
71           boolean  flag = false ;  
72          File filePath = new  File(textPath);  
73           if ( ! filePath.exists()){  
74              filePath.mkdirs();  
75          }  
76           try  {  
77              FileWriter fw  = new  FileWriter(textPath + File.separator + textName, true );  
78              fw.append(date);  
79              flag = true ;  
80               if (fw != null )  
81                  fw.close();  
82          }  catch  (IOException e) {  
83              LogInfo.error( this .getClass().getName(),e.fillInStackTrace().toString());  
84              e.printStackTrace();  
85          }  
86           return  flag;      
87      }  
88       public  String getTextPath() {  
89           return  textPath;  
90      }  
91       public   void  setTextPath(String textPath) {  
92           this .textPath  =  textPath;  
93      }  
94  }  
95 

PrintWriter out = new PrintWriter(new FileWriter(logFileName, true), true); 
Java读写文件最常用的类是FileInputStream/FileOutputStream和FileReader/FileWriter。 
其中FileInputStream和FileOutputStream是基于字节流的,常用于读写二进制文件。 
读写字符文件建议使用基于字符的FileReader和FileWriter,省去了字节与字符之间的转换。 
但这两个类的构造函数默认使用系统的编码方式,如果文件内容与系统编码方式不一致,可能会出现乱码。 
在这种情况下,建议使用FileReader和FileWriter的父类:InputStreamReader/OutputStreamWriter, 
它们也是基于字符的,但在构造函数中可以指定编码类型:InputStreamReader(InputStream in, Charset cs) 和OutputStreamWriter(OutputStream out, Charset cs)。 

// 读写文件的编码: 
InputStreamReader r = new InputStreamReader(new FileInputStream(fileName), “utf-8″); 
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileName),”utf-8″);




你可能感兴趣的:(Java读文件写文件操作(续))