java读取文件内容的三种方法代码片断分享(java文件操作)
程序员文章站
2024-02-21 19:23:16
复制代码 代码如下:try { // 方法一&nbs...
复制代码 代码如下:
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();
}