有一个程序通过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());
}
}
}这样的方法为什么对一个长度不为空的文件,却判断出此文件为空?
/**
* 判断文件是否为空
* */
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());
}
}
}这样的方法为什么对一个长度不为空的文件,却判断出此文件为空?
如果文件正在写入状态,数据还没有写入完全,读取判断就会是空。而且你这样判断文件,在获得文件之后,先要判断file.exists()是否存在,isFile()是否为文件,canRead()能否读取等。拿到文件上来就用,是大忌。
File.length();
fin = new FileInputStream(file); // 写入Blob
如果文件不存在不是会抛出FileNotFoundException ,我catch后向上一层throw了,但是根据日志来看,并没有抛出任何异常。
一样会出现上面的问题,并且比现在出现这个问题的几率还要大。
就是在读取网络文件的时候没有判断文件是否正在被占用
而回抛出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
但是,如上面说的Cannot open or remove a file containing a running program. 应该是操作系统抛出的异常
然后又加上file.exists()是否存在,isFile()是否为文件,canRead()等限制,
然后在文件流打开500ms后再读取判断文件大小,给他一个缓冲的时间。现在实在没有什么好办法,死马当活马医了