字符流读取文件

时间:2021-09-25 21:00:58
 1 package test;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.FileReader;
 5 import java.io.IOException;
 6 
 7 
 8 public class IOTest {
 9     public String getTt(String file){
10         StringBuffer str = new StringBuffer();
11         BufferedReader reder = null;
12         try {
13             reder = new BufferedReader(new FileReader("jiao"+file+".txt"));
14             String s = null;
15             int len =1;
16             while((s = reder.readLine())!=null ){
17                 str.append(s.trim());
18             }
19             
20         } catch (Exception e) {
21             e.printStackTrace();
22         }    finally{
23             try {
24                 reder.close();
25             } catch (IOException e) {
26                 e.printStackTrace();
27             }
28         }
29         if("".equals(str.toString())){
30             return null;
31         }
32         return str.toString();
33     }
34         
35     public static void main(String[] args) {
36         IOTest i = new IOTest();
37         String s = i.getTt("2");
38         System.out.println(s);
39     }
40 }