標(biāo)準(zhǔn)的讀取文件的方法,讀取整個(gè)文件

          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()對(duì)整個(gè)文件或者Read流進(jìn)行字符掃描


          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()對(duì)整個(gè)文件或者Read流進(jìn)行字節(jié)的掃描

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

          評(píng)論

          # re: 標(biāo)準(zhǔn)的讀取文件的方法,讀取整個(gè)文件 2008-06-27 10:33 zxy

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

          第二種方法,要將ASCII碼轉(zhuǎn)換為字符才行。
          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());  回復(fù)  更多評(píng)論   

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

          導(dǎo)航

          統(tǒng)計(jì)

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評(píng)論

          主站蜘蛛池模板: 顺平县| 长海县| 祁阳县| 东兰县| 杨浦区| 朝阳区| 龙海市| 汉源县| 黔南| 洛南县| 六安市| 婺源县| 孟津县| 新津县| 随州市| 酒泉市| 沈阳市| 安国市| 永登县| 新和县| 会东县| 永城市| 旅游| 乡城县| 杭锦后旗| 抚松县| 柯坪县| 蓬溪县| 西青区| 巫山县| 股票| 山东省| 汉源县| 武清区| 峡江县| 岑溪市| 龙陵县| 巴马| 阿拉善盟| 博爱县| 德清县|