可能FileInputStream.available愚蠢的我? [英] May the FileInputStream.available foolish me?

查看:335
本文介绍了可能FileInputStream.available愚蠢的我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个FileInputStream.available()javadoc说:

lockquote

返回
剩余字节数的估计值,从这个输入流
读取(或
跳过),而不会被下一个
对这个输入
流的方法调用阻塞。下一个调用可能是
同一个线程或另一个线程。
单个读取或跳过这么多字节
不会被阻塞,但可能读取或跳过
个字节。



例如,当
仅仅是很慢的时候,非阻塞的读取(或者
skip)可能会被阻塞,例如当
读取缓慢的
网络上的大文件时。 >

我不确定是否在这个检查中:

  if(new FileInputStream(xmlFile).available()== 0)

我依靠空文件总是会返回零?

$ b

如果xmlFile是java.io.File对象,
可以使用length()方法获得
的大小。


解决方案

You can 依赖于 new FileInputStream(fileName).available() e命名文件为空。
$ b

You 不能依赖 new FileInputStream(fileName).available()== 0 作为一个确定的测试,该文件是空的。如果 fileName 是本地文件系统上的常规文件,它可能会工作。但如果 fileName 是设备文件,或者如果它是远程文件系统上的文件,则可能返回 available()零来报告一个 read()将不得不阻塞一段时间。 (或者在远程文件系统的情况下,它可能不会)。

测试常规文件长度的更可靠的方法是使用 new File(fileName).length()== 0 。但是,对于设备文件或管道,不论最终读取的字节数是多少, length()调用都可能返回0。请记住,如果文件不存在,那么 new File(fileName).length()也会返回零。

<编辑如果你想要一个可靠的测试来看看文件是否为空,你必须进行一些调用:

  public static isEmptyFile(String fileName){
File file = new File(fileName);
if(!file.exists()){
return false;
} else if(file.length()!= 0L){
return false;
} else if(file.isFile()){
return true;
} else if(file.isDirectory()){
return false;
} else {
//如果没有真正读取设备文件/命名管道是
//空,可能是不可能的。这不是
// Java的失败:这是某些
//设备等工作的逻辑结果。
抛出新的CannotAnswerException(...);






$ b

但是你最好仔细测试一下您运行应用程序的所有平台上的各种文件类型。某些文件谓词的行为被记录为特定于平台;请参阅 javadoc


This FileInputStream.available() javadoc says:

Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

In some cases, a non-blocking read (or skip) may appear to be blocked when it is merely slow, for example when reading large files over slow networks.

I'm not sure if in this check:

if (new FileInputStream(xmlFile).available() == 0)

can I rely that empty files will always return zero?

--

Thanks @SB, who does not exactly answered the question, but was the first to give the best alternative:

If xmlFile is a java.io.File object, you can use the length() method to get its size.

解决方案

You can rely on new FileInputStream(fileName).available() returning zero if the named file is empty.

You cannot rely on new FileInputStream(fileName).available() == 0 as a definitive test that the file is empty. If fileName is a regular file on a local file system it will probably work. But if fileName is a device file or if it is a file on a remote file system, available() may return zero to report that a read() will have to block for a period. (Or in the case of a remote file system, it may not.)

A more reliable way to test the length of a regular file is to use new File(fileName).length() == 0. However for a device file or pipe, a length() call may return zero, irrespective of the number of bytes that can ultimately be read. And bear in mind that new File(fileName).length() also returns zero if the file does not exist.

EDIT If you want a reliable test to see if a file is empty, you have to make a number of calls:

public static isEmptyFile(String fileName) {
    File file = new File(fileName);
    if (!file.exists()) {
        return false;
    } else if (file.length() != 0L) {
        return false;
    } else if (file.isFile()) {
        return true;
    } else if (file.isDirectory()) {
        return false;
    } else {
        // It may be impossible to tell that a device file / named pipe is
        // "empty" without actually reading it.  This is not a failing of
        // Java: it is a logical consequence of the way that certain
        // devices, etc work.
        throw new CannotAnswerException(...);
    }
}        

But you would be well advised to test this carefully with a variety of "file" types on all platforms that you run your application on. The behavior of some of the file predicates is documented as being platform specific; see the javadoc.

这篇关于可能FileInputStream.available愚蠢的我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆