有一个程序通过MQ将文件放入OS的一个指定目录下面,另外一个监听程序定时扫描指定文件下的大于某一长度的文件,进行读取。但是对一个明确不为0的文件,下面方法确判断为空。不知道为什么?有没有遇到同样问题的?
/**
 * 判断文件是否为空
 * */
public static int getSize(File file) throws FileNotFoundException,IOException{
InputStream fin = null;
BufferedInputStream bis = null;
try{
fin = new FileInputStream(file); // 写入Blob
bis = new BufferedInputStream(fin, 500 * 1024);

int   size   =   0;  
  int   available   =   0;  
  while   ((available   =   bis.available())>0)  
  {  
      size   +=   available;  
      bis.skip(available);  
  }
  return size;
}catch(FileNotFoundException e){
throw e;
}catch (IOException e){
throw e;
}finally{
try{
if(fin!=null)
fin.close();
}catch(IOException e){
MmsLogger.warnOpenFile(file.getName());

try{
if(bis!=null)
bis.close();
}catch(IOException e){
MmsLogger.warnOpenFile(file.getName());

}
}这样的方法为什么对一个长度不为空的文件,却判断出此文件为空?

解决方案 »

  1.   

    。你的那些文件是不是动态生成的?
    如果文件正在写入状态,数据还没有写入完全,读取判断就会是空。而且你这样判断文件,在获得文件之后,先要判断file.exists()是否存在,isFile()是否为文件,canRead()能否读取等。拿到文件上来就用,是大忌。
      

  2.   

    File 可以直接拿到文件长度
    File.length();
      

  3.   

    to :xyz20003
    fin = new FileInputStream(file); // 写入Blob 
    如果文件不存在不是会抛出FileNotFoundException ,我catch后向上一层throw了,但是根据日志来看,并没有抛出任何异常。
      

  4.   

    之前用过File.length取
    一样会出现上面的问题,并且比现在出现这个问题的几率还要大。
    就是在读取网络文件的时候没有判断文件是否正在被占用
      

  5.   

    文件不存在,和写入不完全不是一个概念。以前我们做过文件遍历,因为是同步写入ftp的,我们去读的时候经常丢失,搞了不少外门邪路去解决,幸好最后使用http发请求来实现同步了(庆幸)。大文件写入慢,生成文件到写入完成需要一个过程,我觉得这期间writer会给文件加锁,导致恰文件无法正长访问,
      

  6.   

    这个问题之前没有遇到过,并且这种网络传输的问题还不能够重现。我做了这样的测试,使用ftp传输一个大文件,在文件传输过程中,exist? = true isFile?=true  canRead? =true
    而回抛出FileNotFoundException ,private void getFileByte2(){
    File f = new File(filePath);
    try {
    System.out.println("exist? "+f.exists());
    System.out.println("isFile? "+f.isFile());
    System.out.println("canRead? "+f.exists());
    FileInputStream in = new FileInputStream(f);
    System.out.println("exist? "+f.exists());
    System.out.println("isFile? "+f.isFile());
    System.out.println("canRead? "+f.exists());
    try {
    int   size   =   0;  
      int   available   =   0;  
      while   ((available   =   in.available())>0)  
      {  
          size   +=   available;  
          in.skip(available);  
      }
      System.out.println("size="+size);
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    } catch (FileNotFoundException e2) {
    e2.printStackTrace();
    }
    }
       
    打印信息如下:
    (文件正在传输中)
    $ r
    java filesize.FileByte
    exist? true
    isFile? true
    canRead? true
    java.io.FileNotFoundException: /home/mhdbs/SVL031914.04A (Cannot open or remove a file containing a running program.)
            at java.io.FileInputStream.open(Native Method)
            at java.io.FileInputStream.<init>(FileInputStream.java:135)
            at filesize.FileByte.getFileByte2(FileByte.java:51)
            at filesize.FileByte.access$0(FileByte.java:45)
            at filesize.FileByte$1.run(FileByte.java:84)(文件传输完毕)
    $ r
    java filesize.FileByte
    exist? true
    isFile? true
    canRead? true
    exist? true
    isFile? true
    canRead? true
    size=93614307
      

  7.   

    Window对于文件访问一般是加锁的,我原来测试Unix的时候,锁文件没有用。可能因为Unix是多用户的
    但是,如上面说的Cannot open or remove a file containing a running program. 应该是操作系统抛出的异常
      

  8.   

    90%是因为文件锁造成的,你有两个程序一个存一个找,windows在处理文件锁的时候我认为处理的不好,可能还是因为锁没有得到正确的释放.要不不使用BufferedInputStream 看看.
      

  9.   

    to : sunyujia之前测试去掉BufferedInputStream仍然有问题,我现在加上BufferedInputStream
    然后又加上file.exists()是否存在,isFile()是否为文件,canRead()等限制,
    然后在文件流打开500ms后再读取判断文件大小,给他一个缓冲的时间。现在实在没有什么好办法,死马当活马医了