空文件判断方法分析

本文结合File,FileInputStream,FileRedaer以及BufferedReader的方法,将给出四个判断文件是否为空的方法: 




本文包括如下三个部分: 
1. 贴出File,FileInputStream,FileRedaer以及BufferedReader相应方法的源代码,了解一下。 
2. 给出自己的实现。 
3. 测试并给出分析结论。 

File,FileInputStream,FileRedaer以及BufferedReader相应方法的源代码  

Java代码   收藏代码
  1. /** 
  2.      * Returns the length of the file denoted by this abstract pathname. 
  3.      * The return value is unspecified if this pathname denotes a directory. 
  4.      * 
  5.      * @return  The length, in bytes, of the file denoted by this abstract 
  6.      *          pathname, or <code>0L</code> if the file does not exist 
  7.      * 
  8.      * @throws  SecurityException 
  9.      *          If a security manager exists and its <code>{@link 
  10.      *          java.lang.SecurityManager#checkRead(java.lang.String)}</code> 
  11.      *          method denies read access to the file 
  12.      */  
  13.     public long length() {  
  14.     SecurityManager security = System.getSecurityManager();  
  15.     if (security != null) {  
  16.         security.checkRead(path);  
  17.     }  
  18.     return fs.getLength(this);  
  19.     }  


Java代码   收藏代码
  1. public  
  2. class FileInputStream extends InputStream  
  3. {  
  4.     /** 
  5.      * Returns the number of bytes that can be read from this file input 
  6.      * stream without blocking. 
  7.      * 
  8.      * @return     the number of bytes that can be read from this file input 
  9.      *             stream without blocking. 
  10.      * @exception  IOException  if an I/O error occurs. 
  11.      */  
  12. public native int available() throws IOException;  
  13. }  


Java代码   收藏代码
  1. public class InputStreamReader extends Reader {  
  2. /** 
  3.      * Tell whether this stream is ready to be read.  An InputStreamReader is 
  4.      * ready if its input buffer is not empty, or if bytes are available to be 
  5.      * read from the underlying byte stream. 
  6.      * 
  7.      * @exception  IOException  If an I/O error occurs 
  8.      */  
  9.     public boolean ready() throws IOException {  
  10.     return sd.ready();  
  11.     }  
  12. }  


Java代码   收藏代码
  1. public class FileReader extends InputStreamReader {  
  2.   
  3. }  


Java代码   收藏代码
  1. public class BufferedReader extends Reader {  
  2.   
  3. /** 
  4.      * Tell whether this stream is ready to be read.  A buffered character 
  5.      * stream is ready if the buffer is not empty, or if the underlying 
  6.      * character stream is ready. 
  7.      * 
  8.      * @exception  IOException  If an I/O error occurs 
  9.      */  
  10.     public boolean ready() throws IOException {  
  11.     synchronized (lock) {  
  12.         ensureOpen();  
  13.   
  14.         /*  
  15.          * If newline needs to be skipped and the next char to be read 
  16.          * is a newline character, then just skip it right away. 
  17.          */  
  18.         if (skipLF) {  
  19.         /* Note that in.ready() will return true if and only if the next  
  20.          * read on the stream will not block. 
  21.          */  
  22.         if (nextChar >= nChars && in.ready()) {  
  23.             fill();  
  24.         }  
  25.         if (nextChar < nChars) {  
  26.             if (cb[nextChar] == '\n')   
  27.             nextChar++;  
  28.             skipLF = false;  
  29.         }   
  30.         }  
  31.         return (nextChar < nChars) || in.ready();  
  32.     }  
  33. }  
  34. }  



给出自己的实现类
 

Java代码   收藏代码
  1. package my.tool.file.util;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.FileReader;  
  8. import java.io.IOException;  
  9.   
  10. public class EmptyFileChecker {  
  11.   
  12.     public static boolean isFileEmpty1(File file) {  
  13.         FileInputStream fis = null;  
  14.         boolean flag = true;  
  15.         try {  
  16.             fis = new FileInputStream(file);  
  17.             if (fis.available() != 0) {  
  18.                 flag = false;  
  19.             }  
  20.         } catch (FileNotFoundException e) {  
  21.             // TODO Auto-generated catch block  
  22.             e.printStackTrace();  
  23.         } catch (IOException e) {  
  24.             // TODO Auto-generated catch block  
  25.             e.printStackTrace();  
  26.         } finally {  
  27.             if (null != fis) {  
  28.                 try {  
  29.                     fis.close();  
  30.                 } catch (IOException e) {  
  31.                     // TODO Auto-generated catch block  
  32.                     e.printStackTrace();  
  33.                 } finally {  
  34.                     fis = null;  
  35.                 }  
  36.             }  
  37.         }  
  38.   
  39.         return flag;  
  40.     }  
  41.   
  42.     public static boolean isFileEmpty2(File file) {  
  43.         boolean flag = true;  
  44.         FileReader fr = null;  
  45.         try {  
  46.             fr = new FileReader(file);  
  47.   
  48.             if (fr.ready()) {  
  49.                 flag = false;  
  50.             }  
  51.         } catch (FileNotFoundException e) {  
  52.             // TODO Auto-generated catch block  
  53.             e.printStackTrace();  
  54.         } catch (IOException e) {  
  55.             // TODO Auto-generated catch block  
  56.             e.printStackTrace();  
  57.         } finally {  
  58.             if (null != fr) {  
  59.                 try {  
  60.                     fr.close();  
  61.                 } catch (IOException e) {  
  62.                     // TODO Auto-generated catch block  
  63.                     e.printStackTrace();  
  64.                 } finally {  
  65.                     fr = null;  
  66.                 }  
  67.             }  
  68.         }  
  69.         return flag;  
  70.     }  
  71.   
  72.     public static boolean isFileEmpty3(File file) {  
  73.         boolean flag = true;  
  74.   
  75.         BufferedReader br = null;  
  76.   
  77.         try {  
  78.             br = new BufferedReader(new FileReader(file));  
  79.   
  80.             if (br.ready()) {  
  81.                 flag = false;  
  82.             }  
  83.         } catch (FileNotFoundException e) {  
  84.             // TODO Auto-generated catch block  
  85.             e.printStackTrace();  
  86.         } catch (IOException e) {  
  87.             // TODO Auto-generated catch block  
  88.             e.printStackTrace();  
  89.         } finally {  
  90.             if (null != br) {  
  91.                 try {  
  92.                     br.close();  
  93.                 } catch (IOException e) {  
  94.                     // TODO Auto-generated catch block  
  95.                     e.printStackTrace();  
  96.                 } finally {  
  97.                     br = null;  
  98.                 }  
  99.             }  
  100.         }  
  101.   
  102.         return flag;  
  103.     }  
  104.       
  105.     public static boolean isFileEmpty4(File file) {  
  106.         return file.length() ==0L;  
  107.     }  
  108.  }  


为了测试文件是否为空,新建多种文件类型的文件。 

 

Note :  新建文件后,像zip文件它们的大小都不是0KB。  

编写一个测试类: 
Java代码   收藏代码
  1. package my.tool.file.util;  
  2.   
  3. import java.io.File;  
  4.   
  5. public class Main {  
  6.     public static void main(String[] args) {  
  7.         for(File file: new File("D: \\EmptyFileFolder").listFiles())  
  8.         {  
  9.             System.out.print(file.getName());  
  10.             System.out.print(" ");  
  11.             System.out.print(" is empty ? ");  
  12.             System.out.println();  
  13.               
  14.             System.out.println("Call method available in FileInputStream -->" + EmptyFileChecker.isFileEmpty1(file));  
  15.               
  16.             System.out.println("Call method ready in FileReader -->" + EmptyFileChecker.isFileEmpty2(file));  
  17.               
  18.             System.out.println("Call method ready in BufferedReader -->" + EmptyFileChecker.isFileEmpty3(file));  
  19.               
  20.             System.out.println("Call method length in File -->" + EmptyFileChecker.isFileEmpty4(file));  
  21.               
  22.             System.out.println();  
  23.         }  
  24.     }  
  25.       
  26. }  


输出的结果如下: 

新建 BMP 图像.bmp  is empty ? 
Call method available in FileInputStream -->true 
Call method ready in FileReader -->true 
Call method ready in BufferedReader -->true 
Call method length in File -->true 

新建 Microsoft Office Access 2007 Database.accdb  is empty ? 
Call method available in FileInputStream -->false 
Call method ready in FileReader -->false 
Call method ready in BufferedReader -->false 
Call method length in File -->false 

新建 Microsoft Office Word Document.docx  is empty ? 
Call method available in FileInputStream -->true 
Call method ready in FileReader -->true 
Call method ready in BufferedReader -->true 
Call method length in File -->true 

新建 OpenDocument Drawing.odg  is empty ? 
Call method available in FileInputStream -->false 
Call method ready in FileReader -->false 
Call method ready in BufferedReader -->false 
Call method length in File -->false 

新建 OpenDocument Spreadsheet.ods  is empty ? 
Call method available in FileInputStream -->false 
Call method ready in FileReader -->false 
Call method ready in BufferedReader -->false 
Call method length in File -->false 

新建 压缩(zipped)文件夹.zip  is empty ? 
Call method available in FileInputStream -->false 
Call method ready in FileReader -->false 
Call method ready in BufferedReader -->false 
Call method length in File -->false 

新建 文本文档.txt  is empty ? 
Call method available in FileInputStream -->true 
Call method ready in FileReader -->true 
Call method ready in BufferedReader -->true 
Call method length in File -->true 

新建 波形声音.wav  is empty ? 
Call method available in FileInputStream -->false 
Call method ready in FileReader -->false 
Call method ready in BufferedReader -->false 
Call method length in File -->false 




结论:   通过File的length方法,FileInputStream的available方法或者是FileReader以及BufferedReader的ready方法,只有当新建的文件是0KB的时候,才能正确判断文件是否为空如果新建后的文件有一定的大小,比如zip文件有1KB,这样的文件,如果想要知道文件里面有没有内容,只能采用其它方式来做
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值