qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Java文件操作類FileManager

          讀寫文件是最常用的操作之一,每次將相應的代碼片段復制過來不僅麻煩,還會影響整體的美觀。為此我單獨寫了一個文件操作的類,需要時先把這個類的代碼粘過去,再調用就方便多了。
          import java.io.BufferedReader;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.InputStreamReader;
          import java.io.OutputStreamWriter;
          import java.util.ArrayList;
          import java.util.List;
          public class FileManager {
          public static String read(String fileName, String encoding) {
          StringBuffer fileContent = new StringBuffer();
          try {
          FileInputStream fis = new FileInputStream(fileName);
          InputStreamReader isr = new InputStreamReader(fis, encoding);
          BufferedReader br = new BufferedReader(isr);
          String line = null;
          while ((line = br.readLine()) != null) {
          fileContent.append(line);
          fileContent.append(System.getProperty("line.separator"));
          }
          br.close();
          isr.close();
          fis.close();
          } catch (Exception e) {
          e.printStackTrace();
          }
          return fileContent.toString();
          }
          public static void write(String fileContent, String fileName, String encoding) {
          try {
          FileOutputStream fos = new FileOutputStream(fileName);
          OutputStreamWriter osw = new OutputStreamWriter(fos, encoding);
          osw.write(fileContent);
          osw.flush();
          osw.close();
          fos.close();
          } catch (Exception e) {
          e.printStackTrace();
          }
          }
          }
            調用示例:
            寫入 FileManager.write("Hello, World!", "D:\\test.txt", "UTF-8");
            讀取 System.out.println(FileManager.read("D:\\test.txt", "UTF-8"));

          posted on 2014-03-12 10:57 順其自然EVO 閱讀(313) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網站導航:
           
          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 乌海市| 铜山县| 凌海市| 阳东县| 乃东县| 丰顺县| 瑞昌市| 东乌珠穆沁旗| 广水市| 阆中市| 东阿县| 禹城市| 阿拉善右旗| 美姑县| 海南省| 张家口市| 青岛市| 长沙市| 南昌市| 定襄县| 庄河市| 陇西县| 江油市| 泰和县| 湘潭市| 墨江| 贺州市| 营山县| 海安县| 新源县| 呼伦贝尔市| 象山县| 平泉县| 阳东县| 江孜县| 正宁县| 江华| 泰宁县| 博兴县| 襄城县| 潍坊市|