java讀取文件內容的三種辦法代碼片段分享(java文件操作)。本站提示廣大學習愛好者:(java讀取文件內容的三種辦法代碼片段分享(java文件操作))文章只能為提供參考,不一定能成為您想要的結果。以下是java讀取文件內容的三種辦法代碼片段分享(java文件操作)正文
try {
// 辦法一
BufferedReader br = new BufferedReader(new FileReader(new File(
"D:\\1.xls")));
// StringBuilder bd = new StringBuilder();
StringBuffer bd = new StringBuffer();
while (true) {
String str = br.readLine();
if (str == null) {
break;
}
System.out.println(str);
bd.append(str);
}
br.close();
// System.out.println(bd.toString());
// 辦法二
InputStream is = new FileInputStream(new File("d:\\1.xls"));
byte b[] = new byte[Integer.parseInt(new File("d:\\1.xls").length()
+ "")];
is.read(b);
System.out.write(b);
System.out.println();
is.close();
// 辦法三
Reader r = new FileReader(new File("d:\\1.xls"));
char c[] = new char[(int) new File("d:\\1.xls").length()];
r.read(c);
String str = new String(c);
System.out.print(str);
r.close();
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}