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

          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流進行字節(jié)的掃描

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

          評論

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

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

          第二種方法,要將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ù)  更多評論   

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評論

          主站蜘蛛池模板: 石嘴山市| 信宜市| 公主岭市| 泾川县| 铜鼓县| 南雄市| 金塔县| 昌平区| 大新县| 石渠县| 雷州市| 威远县| 万安县| 浮梁县| 沐川县| 晋江市| 绍兴县| 抚松县| 兴仁县| 沂水县| 沁阳市| 综艺| 凭祥市| 句容市| 星座| 灵川县| 西青区| 邵阳市| 托里县| 嘉兴市| 兴业县| 武功县| 长治市| 固安县| 石楼县| 伊春市| 建湖县| 永泰县| 郸城县| 洛宁县| 曲沃县|