標準的讀取文件的方法,讀取整個文件

          public void test() throws IOException{
                  File f=new File("d:"+File.separator+"1.txt");
                  BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
                  StringBuffer whole=new StringBuffer();
                  String line=new String();
                  for (line=br.readLine(); line!=null; line=br.readLine()) {
                      whole.append(line);
                     
                  }
                  System.out.println(whole.toString());
              }
          使用readLine()對整個文件或者Read流進行字符掃描


          public void test1() throws IOException{
                  File f=new File("d:"+File.separator+"1.txt");

                  BufferedInputStream br=new BufferedInputStream(new FileInputStream(f));
                  StringBuffer whole=new StringBuffer();
                  int line;
                  for (line=br.read(); line!=-1; line=br.read()) {
                      whole.append(line);
                     
                  }
                  System.out.println(whole.toString());
              }

          使用read()對整個文件或者Read流進行字節的掃描

          posted on 2008-01-02 18:02 劉錚 閱讀(662) 評論(1)  編輯  收藏 所屬分類: JAVA General

          評論

          # re: 標準的讀取文件的方法,讀取整個文件 2008-06-27 10:33 zxy

          我覺得第一種方法
          whole.append(line);改為whole.append(" " + line);會比較好一點

          第二種方法,要將ASCII碼轉換為字符才行。
          File f=new File("abc.in");

          BufferedInputStream br=new BufferedInputStream(new FileInputStream(f));
          StringBuffer whole=new StringBuffer();
          int line;
          char c;

          for (line =br.read(); line!=-1; line=br.read()) {
          c = (char)line;
          whole.append(c);

          }
          System.out.println(whole.toString());  回復  更多評論   

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評論

          主站蜘蛛池模板: 平武县| 舟山市| 保康县| 年辖:市辖区| 景德镇市| 南康市| 布尔津县| 通州市| 囊谦县| 厦门市| 保靖县| 彰化市| 桓仁| 扶沟县| 汝阳县| 太康县| 宝丰县| 广德县| 邢台市| 英吉沙县| 扎囊县| 习水县| 江津市| 全南县| 新巴尔虎右旗| 青阳县| 安国市| 潼关县| 宁远县| 永年县| 衡阳市| 新乐市| 修文县| 专栏| 濮阳县| 象州县| 旬阳县| 油尖旺区| 集安市| 高青县| 聊城市|