空文件判断方法分析

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


[img]http://dl2.iteye.com/upload/attachment/0087/3172/c378ffc9-b2b4-34e5-a17e-c4bafb91f7b5.jpg[/img]

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

[color=blue][b]File,FileInputStream,FileRedaer以及BufferedReader相应方法的源代码[/b][/color]

/**
* 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();
}
}
}


[b]
给出自己的实现类[/b]

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;
}
}


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

[img]http://dl2.iteye.com/upload/attachment/0087/3174/05ebef4c-1755-3c13-a2ca-c029a713e19c.jpg[/img]

[b]Note[/b]:[color=red][b] 新建文件后,像zip文件它们的大小都不是0KB。[/b][/color]

编写一个测试类:
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


[img]http://dl2.iteye.com/upload/attachment/0087/3182/e339d7ae-0eff-3b15-81a2-2973245d33a4.jpg[/img]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值