The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
          public String inputStream2String(InputStream is)  
                 
          throws UnsupportedEncodingException {  
             BufferedReader reader 
          = new BufferedReader(new InputStreamReader(is,  
                      
          "utf-8"));  
             StringBuffer sb 
          = new StringBuffer();  
              String line 
          = null;  
              
          try {  
                  
          while ((line = reader.readLine()) != null) {  
                      sb.append(line 
          + "\n");  
                  }  
              } 
          catch (IOException e) {  
                  e.printStackTrace();  
             } 
          finally {  
                  
          try {  
                      is.close();  
                 } 
          catch (IOException e) {  
                      e.printStackTrace();  
                   }  
              }  
             
          return sb.toString();  


          import java.io.*;

          public class Test {
              
          public BufferedReader bufread;
              
          public BufferedWriter bufwriter;
              File writefile;
              String filepath, filecontent, read;
              String readStr 
          = "";

              
          public String readfile(String path) // 從文本文件中讀取內容
              {
                  
          try {
                      filepath 
          = path; // 得到文本文件的路徑
                      File file = new File(filepath);
                      FileReader fileread 
          = new FileReader(file);
                      bufread 
          = new BufferedReader(fileread);
                      
          while ((read = bufread.readLine()) != null) {
                          readStr 
          = readStr + read;
                      }
                  } 
          catch (Exception d) {
                      System.out.println(d.getMessage());
                  }
                  
          return readStr; // 返回從文本文件中讀取內容
              }

              
          // 向文本文件中寫入內容
              public void writefile(String path, String content, boolean append) {
                  
          try {
                      
          boolean addStr = append; // 通過這個對象來判斷是否向文本文件中追加內容
                      filepath = path; // 得到文本文件的路徑
                      filecontent = content; // 需要寫入的內容
                      writefile = new File(filepath);
                      
          if (writefile.exists() == false// 如果文本文件不存在則創建它
                      {
                          writefile.createNewFile();
                          writefile 
          = new File(filepath); // 重新實例化
                      }
                      FileWriter filewriter 
          = new FileWriter(writefile, addStr);
                      bufwriter 
          = new BufferedWriter(filewriter);
                      filewriter.write(filecontent);
                      filewriter.flush();
                  } 
          catch (Exception d) {
                      System.out.println(d.getMessage());
                  }
              }

          }

          posted on 2011-07-05 18:27 Eric_jiang 閱讀(248) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 布尔津县| 文昌市| 韶关市| 天长市| 崇阳县| 新营市| 云浮市| 青川县| 松桃| 镇江市| 永顺县| 若羌县| 贵港市| 灵台县| 徐汇区| 泸定县| 衡阳市| 平塘县| 壤塘县| 庆阳市| 普安县| 乌苏市| 堆龙德庆县| 余江县| 瓦房店市| 兴海县| 漳州市| 陇川县| 宁武县| 新竹市| 乐清市| 黔西县| 榆树市| 个旧市| 五家渠市| 仪征市| 济阳县| 清新县| 江陵县| 古蔺县| 会东县|