`
MouseLearnJava
  • 浏览: 460515 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

空文件判断方法分析

    博客分类:
  • Java
阅读更多

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




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

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

/**
     * Returns the length of the file denoted by this abstract pathname.
     * The return value is unspecified if this pathname denotes a directory.
     *
     * @return  The length, in bytes, of the file denoted by this abstract
     *          pathname, or <code>0L</code> if the file does not exist
     *
     * @throws  SecurityException
     *          If a security manager exists and its <code>{@link
     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
     *          method denies read access to the file
     */
    public long length() {
	SecurityManager security = System.getSecurityManager();
	if (security != null) {
	    security.checkRead(path);
	}
	return fs.getLength(this);
    }


public
class FileInputStream extends InputStream
{
    /**
     * Returns the number of bytes that can be read from this file input
     * stream without blocking.
     *
     * @return     the number of bytes that can be read from this file input
     *             stream without blocking.
     * @exception  IOException  if an I/O error occurs.
     */
public native int available() throws IOException;
}


public class InputStreamReader extends Reader {
/**
     * Tell whether this stream is ready to be read.  An InputStreamReader is
     * ready if its input buffer is not empty, or if bytes are available to be
     * read from the underlying byte stream.
     *
     * @exception  IOException  If an I/O error occurs
     */
    public boolean ready() throws IOException {
	return sd.ready();
    }
}


public class FileReader extends InputStreamReader {

}


public class BufferedReader extends Reader {

/**
     * Tell whether this stream is ready to be read.  A buffered character
     * stream is ready if the buffer is not empty, or if the underlying
     * character stream is ready.
     *
     * @exception  IOException  If an I/O error occurs
     */
    public boolean ready() throws IOException {
	synchronized (lock) {
	    ensureOpen();

	    /* 
	     * If newline needs to be skipped and the next char to be read
	     * is a newline character, then just skip it right away.
	     */
	    if (skipLF) {
		/* Note that in.ready() will return true if and only if the next 
		 * read on the stream will not block.
		 */
		if (nextChar >= nChars && in.ready()) {
		    fill();
		}
		if (nextChar < nChars) {
		    if (cb[nextChar] == '\n') 
			nextChar++;
		    skipLF = false;
		} 
	    }
	    return (nextChar < nChars) || in.ready();
	}
}
}



给出自己的实现类


package my.tool.file.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class EmptyFileChecker {

	public static boolean isFileEmpty1(File file) {
		FileInputStream fis = null;
		boolean flag = true;
		try {
			fis = new FileInputStream(file);
			if (fis.available() != 0) {
				flag = false;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != fis) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					fis = null;
				}
			}
		}

		return flag;
	}

	public static boolean isFileEmpty2(File file) {
		boolean flag = true;
		FileReader fr = null;
		try {
			fr = new FileReader(file);

			if (fr.ready()) {
				flag = false;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != fr) {
				try {
					fr.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					fr = null;
				}
			}
		}
		return flag;
	}

	public static boolean isFileEmpty3(File file) {
		boolean flag = true;

		BufferedReader br = null;

		try {
			br = new BufferedReader(new FileReader(file));

			if (br.ready()) {
				flag = false;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != br) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					br = null;
				}
			}
		}

		return flag;
	}
	
	public static boolean isFileEmpty4(File file) {
		return file.length() ==0L;
	}
 }


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



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

编写一个测试类:
package my.tool.file.util;

import java.io.File;

public class Main {
	public static void main(String[] args) {
		for(File file: new File("D: \\EmptyFileFolder").listFiles())
		{
			System.out.print(file.getName());
			System.out.print(" ");
			System.out.print(" is empty ? ");
			System.out.println();
			
			System.out.println("Call method available in FileInputStream -->" + EmptyFileChecker.isFileEmpty1(file));
			
			System.out.println("Call method ready in FileReader -->" + EmptyFileChecker.isFileEmpty2(file));
			
			System.out.println("Call method ready in BufferedReader -->" + EmptyFileChecker.isFileEmpty3(file));
			
			System.out.println("Call method length in File -->" + EmptyFileChecker.isFileEmpty4(file));
			
			System.out.println();
		}
	}
	
}


输出的结果如下:

新建 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,这样的文件,如果想要知道文件里面有没有内容,只能采用其它方式来做
  • 大小: 19.7 KB
  • 大小: 21.9 KB
  • 大小: 96.8 KB
0
3
分享到:
评论

相关推荐

    Vue前端判断数据对象是否为空的实例

    Vue提供了强大的前端开发架构,很多时候我们需要判断数据对象是否为空,使用typeof判断是个不错选择,具体代码见图。 补充知识:vue打包后 history模式 跟子目录 静态文件路径 分析 history 根目录 路由mode变为...

    LL(1)语法分析器

    首先输入定义好的文法书写文件(所用的文法可以用LL(1)分析),先求出所输入的文法的每个非终结符是否能推出空,再分别计算非终结符号的FIRST集合,每个非终结符号的FOLLOW集合,以及每个规则的SELECT集合,并判断任意...

    java 导入及判断的Excel 使用方法

    java+Excel+使用方法,java上传excel 详解,java上传excel 实例分析

    词法分析Lexer.zip

    输入:单词序列(以文件形式提供),输出识别的单词的二元组序列到文件和屏幕 输出:二元组构成: (syn,token或sum)其中: syn 为单词的种别码 token 为存放的单词自身符号串 sum 为整型常数

    编译原理 词法分析器

    设计一个简单的c—语言词法分析器,该分析器能将C语言程序分割成若干个token子列,分别判断出该token类型和属性值。 二.实验要求: 1.程序要做成命令行程序,带两个参数,分别表示输入和输出文件名。 2.实例输入...

    有空分析一下这个 Gerber 为什么不识别板框-2235.zip

    有空分析一下这个 Gerber 为什么不识别板框-2235.zip

    编译原理词法分析

    2熟悉PASCAL系统中的基本语句及文件类型的使用方法。 3掌握PL/O语言源程序的结构及构成规则。 (二)实验内容与步骤  1用PL/0语言编写程序:建立和访问正文文件 2用PL/O语言编写能打印如下图形的...

    如何编写批处理文件批处理文件批处理文件

    内存,并被当作文件分析。因此,以下例子: FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i 会枚举当前环境中的环境变量名称。 另外,FOR 变量参照的替换已被增强。您现在可以使用下列 选项语法: ~I - ...

    c语言词法分析

    1.初始化:从文件将源程序全部输入到字符缓冲区中。 2.取单词前:去掉多余空白。 3.取单词后:去掉多余空白(可选,看着办)。 4.取单词:读出单词的每一个字符,组成单词,分析类型。(关键是如何判断取单词结束?...

    编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。并依次输出各个单词的内部编码及单词符号自身值。

    并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。 编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。并...

    编译原理 C语言实现词法分析

    并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。 编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。并...

    编译原理语法分析器课程设计

    语法分析程序用LL(1)语法分析方法。首先输入定义好的文法书写文件(所用的文法可以用LL(1)分析),然后建立词法分析器,包括词法分析主程序、扫描器部分、关键字表等。经词法分析后分别计算所输入的文法的每个非终结...

    音频文件标签编辑器 Metatogger 7.0.2..zip

    使用 Acoustid 声学指纹识别技术来识别可能重复的音频文件,即使它们并非严格相同(编码参数,文件格式等); 快速删除不良标签。 该实用程序的界面干净直观。通过使用文件浏览器,文件夹视图或“拖放”方法,...

    编译原理词法分析实验报告

    调试并完成一个词法分析程序,加深对词法分析原理的理解。 二、 实验要求 1、 待分析的简单语言的词法 (1) 关键字: begin if then while do end 所有关键字都是小写。 (2) 运算符和界符: := + – * / ...

    基于C语言实现的LL(1)分析.zip

    资源包含文件:课程报告word+源码 确定文法的文件存储格式,存储文法的非终结符集合、开始符号、终结符集合和产生式规则集合。详细介绍参考:https://blog.csdn.net/newlw/article/details/126048498

    编译原理 词法分析 源代码

    并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。 编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。并...

    代码静态分析工具

    PC-Lint 是GIMPEL ...PC-Lint不仅能够对程序进行全局分析,识别没有被适当检验的数组下标,报告未被初始化的变量,警告使用空指针以及冗余的代码,还能够有效地帮你提出许多程序在空间利用、运行效率上的改进点。

    代码语法错误分析工具pclint8.0

    它进行程序的全局分析,能识别没有被适当检验的数组下标,报告未被初始化的变量,警告使用空指针,冗余的代码,等等。软件除错是软件项目开发成本和延误的主要因素。PClint能够帮你在程序动态测试之前发现编码错误。...

    词法分析器(对输入的源码进行分类识别).cpp

    编制一个源程序的输入过程,从键盘、文件或文本框输入若干行语句,依次存入输入缓冲区(字符型数据);然后编制一个预处理子程序,去掉输入串中的回车符、换行符和跳格符等编辑性文字;把多个空白符合并为一个;去掉...

Global site tag (gtag.js) - Google Analytics