软件编程
位置:首页>> 软件编程>> java编程>> java读取文件内容的三种方法代码片断分享(java文件操作)

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

  发布时间:2023-11-21 06:53:20 

标签: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();
       }

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com