空文件判断方法分析
本文结合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); }publicclass 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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (null != fis) {try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (null != fr) {try {fr.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (null != br) {try {br.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {br = null;}}}return flag;}public static boolean isFileEmpty4(File file) {return file.length() ==0L;} }
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();}}}