- 浏览: 460515 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
ty1972873004:
sunwang810812 写道我运行了这个例子,怎么结果是这 ...
Java并发编程: 使用Semaphore限制资源并发访问的线程数 -
lgh1992314:
simpleDean 写道请问,Logger.setLevel ...
Java内置Logger详解 -
sunwang810812:
我运行了这个例子,怎么结果是这样的:2号车泊车6号车泊车5号车 ...
Java并发编程: 使用Semaphore限制资源并发访问的线程数 -
jp260715007:
nanjiwubing123 写道参考你的用法,用如下方式实现 ...
面试题--三个线程循环打印ABC10次的几种解决方法 -
cb_0312:
SurnameDictionary文章我没看完,现在懂了
中文排序
本文结合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,这样的文件,如果想要知道文件里面有没有内容,只能采用其它方式来做。
发表评论
-
工厂类中移除if/else语句
2016-07-10 19:52 857面向对象语言的一个强大的特性是多态,它可以用来在代码中移除 ... -
Java编程练手100题
2014-12-11 17:13 6673本文给出100道Java编程练手的程序。 列表如下: 面 ... -
数组复制的三种方法
2014-11-30 12:57 2181本文将给出三种实现数组复制的方法 (以复制整数数组为例)。 ... -
数组复制的三种方法
2014-11-30 12:54 0本文将给出三种实现数组复制的方法 (以复制整数数组为例)。 ... -
四种复制文件的方法
2014-11-29 13:21 1690尽管Java提供了一个类ava.io.File用于文件的操 ... -
判断一个字符串中的字符是否都只出现一次
2014-11-25 12:58 2661本篇博文将给大家带来几个判断一个字符串中的字符是否都只出现一 ... -
使用正则表达式判断一个数是否为素数
2014-11-23 13:35 2108正则表达式能够用于判断一个数是否为素数,这个以前完全没有想过 ... -
几个可以用英文单词表达的正则表达式
2014-11-21 13:12 3705本文,我们将来看一下几个可以用英文单词表达的正则表达式。这些 ... -
(广度优先搜索)打印所有可能的括号组合
2014-11-20 11:58 1922问题:给定一个正整n,作为括号的对数,输出所有括号可能 ... -
随机产生由特殊字符,大小写字母以及数字组成的字符串,且每种字符都至少出现一次
2014-11-19 14:48 3944题目:随机产生字符串,字符串中的字符只能由特殊字符 (! ... -
找出1到n缺失的一个数
2014-11-18 12:57 3121题目:Problem description: You h ... -
EnumSet的几个例子
2014-11-14 16:24 8709EnumSet 是一个与枚举类型一起使用的专用 Set 实现 ... -
给定两个有序数组和一个指定的sum值,从两个数组中各找一个数使得这两个数的和与指定的sum值相差最小
2014-11-12 11:24 3288题目:给定两个有序数组和一个指定的sum值,从两个数组 ... -
Java面试编程题练手
2014-11-04 22:49 6659面试编程 写一个程序,去除有序数组中的重复数字 编 ... -
Collections用法整理
2014-10-22 20:55 9803Collections (java.util.Collect ... -
The Code Sample 代码实例 个人博客开通
2014-09-04 18:48 1377个人博客小站开通 http://thecodesample. ... -
Collections.emptyXXX方法
2014-06-08 13:37 2106从JDK 1.5开始, Collections集合工具类中预先 ... -
这代码怎么就打印出"hello world"了呢?
2014-06-08 00:37 7358for (long l = 4946144450195624L ... -
最短时间过桥
2014-04-21 22:03 4075本文用代码实现最短时间过桥,并且打印如下两个例子的最小过桥时间 ... -
将数组分割成差值最小的子集
2014-04-20 22:34 2844本文使用位掩码实现一个功能 ==》将数组分割成差值最小的子集 ...
相关推荐
Vue提供了强大的前端开发架构,很多时候我们需要判断数据对象是否为空,使用typeof判断是个不错选择,具体代码见图。 补充知识:vue打包后 history模式 跟子目录 静态文件路径 分析 history 根目录 路由mode变为...
首先输入定义好的文法书写文件(所用的文法可以用LL(1)分析),先求出所输入的文法的每个非终结符是否能推出空,再分别计算非终结符号的FIRST集合,每个非终结符号的FOLLOW集合,以及每个规则的SELECT集合,并判断任意...
java+Excel+使用方法,java上传excel 详解,java上传excel 实例分析
输入:单词序列(以文件形式提供),输出识别的单词的二元组序列到文件和屏幕 输出:二元组构成: (syn,token或sum)其中: syn 为单词的种别码 token 为存放的单词自身符号串 sum 为整型常数
设计一个简单的c—语言词法分析器,该分析器能将C语言程序分割成若干个token子列,分别判断出该token类型和属性值。 二.实验要求: 1.程序要做成命令行程序,带两个参数,分别表示输入和输出文件名。 2.实例输入...
有空分析一下这个 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 - ...
1.初始化:从文件将源程序全部输入到字符缓冲区中。 2.取单词前:去掉多余空白。 3.取单词后:去掉多余空白(可选,看着办)。 4.取单词:读出单词的每一个字符,组成单词,分析类型。(关键是如何判断取单词结束?...
并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。 编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。并...
并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。 编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。并...
语法分析程序用LL(1)语法分析方法。首先输入定义好的文法书写文件(所用的文法可以用LL(1)分析),然后建立词法分析器,包括词法分析主程序、扫描器部分、关键字表等。经词法分析后分别计算所输入的文法的每个非终结...
使用 Acoustid 声学指纹识别技术来识别可能重复的音频文件,即使它们并非严格相同(编码参数,文件格式等); 快速删除不良标签。 该实用程序的界面干净直观。通过使用文件浏览器,文件夹视图或“拖放”方法,...
调试并完成一个词法分析程序,加深对词法分析原理的理解。 二、 实验要求 1、 待分析的简单语言的词法 (1) 关键字: begin if then while do end 所有关键字都是小写。 (2) 运算符和界符: := + – * / ...
资源包含文件:课程报告word+源码 确定文法的文件存储格式,存储文法的非终结符集合、开始符号、终结符集合和产生式规则集合。详细介绍参考:https://blog.csdn.net/newlw/article/details/126048498
并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。 编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。并...
PC-Lint 是GIMPEL ...PC-Lint不仅能够对程序进行全局分析,识别没有被适当检验的数组下标,报告未被初始化的变量,警告使用空指针以及冗余的代码,还能够有效地帮你提出许多程序在空间利用、运行效率上的改进点。
它进行程序的全局分析,能识别没有被适当检验的数组下标,报告未被初始化的变量,警告使用空指针,冗余的代码,等等。软件除错是软件项目开发成本和延误的主要因素。PClint能够帮你在程序动态测试之前发现编码错误。...
编制一个源程序的输入过程,从键盘、文件或文本框输入若干行语句,依次存入输入缓冲区(字符型数据);然后编制一个预处理子程序,去掉输入串中的回车符、换行符和跳格符等编辑性文字;把多个空白符合并为一个;去掉...