标准的读取文件的方法,读取整个文件

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 刘铮 阅读(633) 评论(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());  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

留言簿(1)

文章分类(141)

文章档案(147)

搜索

最新评论