欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java读取文件内容的三种方法代码片断分享(java文件操作)

程序员文章站 2024-02-14 17:53: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();
       }