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 閱讀(246) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 湘潭县| 昭平县| 迁西县| 高淳县| 虎林市| 闸北区| 保山市| 前郭尔| 洮南市| 宁夏| 荃湾区| 皮山县| 健康| 民权县| 荥经县| 甘德县| 工布江达县| 蕉岭县| 德钦县| 西安市| 塔城市| 高平市| 尼玛县| 云阳县| 黄平县| 漯河市| 信丰县| 宝山区| 卢龙县| 大姚县| 炎陵县| 河曲县| 城口县| 麻阳| 湘阴县| 马公市| 济南市| 洱源县| 和政县| 磴口县| 郸城县|