廉颇老矣,尚能饭否

java:从技术到管理

常用链接

统计

最新评论

java读取外部文件【转载】

 (1)File file = new File(root );
   if (file.exists()) {
    File[] filesArray = file.listFiles();
    if (filesArray != null) {
     for (File f : filesArray) {
      if (f.isFile()) {
       BufferedReader in = new BufferedReader(new FileReader(f.getPath()));
             String msg = in.readLine();
            System.out.println(msg);
             break;
      }
     }
    }
   }
上面是代码完成的功能是检查一个目录下是否有文件,如果有,把读取到的第一个文件的内容读出来。 
与之相反的处理:
  (2) File file = new File("C:\\CSSDG\\BatchError\\");
   if (file.exists()) {
    File[] filesArray = file.listFiles();
    if (filesArray != null) {
     for (File f : filesArray) {
      if (f.isFile()) {
       BufferedWriter out = new BufferedWriter(new FileWriter(f.getPath()));
             out.write("TEST成功!");
             out.close();
      }
     }
    }
   }
注意,(2)和(1)有两点不同。
一是(1)中Reader的地方都要改成Writer,另外,(2)是对外部文件进行更新,要有一个断开连接的过程,就是close方法。

柳德才
13691193654
18942949207
QQ:422157370
liudecai_zan@126.com
湖北-武汉-江夏-庙山

posted on 2009-01-19 23:36 liudecai_zan@126.com 阅读(506) 评论(0)  编辑  收藏 所属分类: 在路上


只有注册用户登录后才能发表评论。


网站导航: