標(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());
}
posted on 2008-01-02 18:02 劉錚 閱讀(657) 評論(1) 編輯 收藏 所屬分類: JAVA General