Java: isBinary 方法的类是什么?

发布于 2024-08-27 18:38:50 字数 420 浏览 7 评论 0原文

我习惯了 java.io.* 和 java.util.* 但不习惯树:

com.starbase.util
Class FileUtils

java.lang.Object

  |

  +--com.starbase.util.FileUtils

源。

那么我应该导入哪个类才能使用 isBinary 方法?我是否执行“import java.lang.Object;”或“导入java.lang.Object.com.starbase.util.FileUtils;”?

I am accustomed to java.io.* and java.util.* but not to the tree:

com.starbase.util
Class FileUtils

java.lang.Object

  |

  +--com.starbase.util.FileUtils

Source.

So which class should I import to use the isBinary-method? Do I do "import java.lang.Object;" or "import java.lang.Object.com.starbase.util.FileUtils;"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尾戒 2024-09-03 18:38:50

您可以import com.starbase.util.FileUtils;import static com.starbase.util.FileUtils.*。层次结构仅显示类 FileUtils 扩展了 Object(与所有类一样)。

您还必须拥有 .jar 文件/API 才能访问此类。

编辑:添加了可能的独立实现:

如果您想自己实现这一点(我注意到您自己的“琐碎”答案),您可以执行以下操作:

    public static boolean isBinary(String fileName) throws IOException {
        return isBinary(new File(fileName));
    }

    public static boolean isBinary(File file) throws IOException {
        InputStream is = new FileInputStream(file);
        try {
            byte[] buf = new byte[4096];
            int bytesRead;
            while ((bytesRead = is.read(buf)) >= 0)
            {
                for (int i = 0; i < bytesRead; i++) {
                    if (buf[i] == (byte) 0)
                        return true;
                }
            }

            return false;
        } finally {
            is.close();
        }
    }

请注意,我尚未对此进行测试。

我应该补充一点,这是一个简单的实现。有很多种文本文件都可以被视为二进制文件。如果您允许文本采用 Unicode 和/或 UTF-8(或其他文本编码),那么这很快就会变得非常困难。然后您需要开发某种启发式方法来区分不同类型的文件,但这并不是 100% 准确。所以,这实际上取决于你想用它做什么。

You would do import com.starbase.util.FileUtils; or import static com.starbase.util.FileUtils.*. The hierarchy is just showing that the class FileUtils extends Object (as do all classes).

You do also have to have the .jar file/API to access this class.

EDIT: Added possible standalone implementation:

If you want to implement this yourself (I noticed your own 'trivial' answer), you could do something like this:

    public static boolean isBinary(String fileName) throws IOException {
        return isBinary(new File(fileName));
    }

    public static boolean isBinary(File file) throws IOException {
        InputStream is = new FileInputStream(file);
        try {
            byte[] buf = new byte[4096];
            int bytesRead;
            while ((bytesRead = is.read(buf)) >= 0)
            {
                for (int i = 0; i < bytesRead; i++) {
                    if (buf[i] == (byte) 0)
                        return true;
                }
            }

            return false;
        } finally {
            is.close();
        }
    }

Please note, I have not tested this.

I should add that this is the trivial implementation. There are many kinds of text files that would be considered binary with this. If you were to allow text to be Unicode and/or UTF-8 (or other text encoding) then this quickly becomes very difficult. Then you need to develop some kinds of heuristics to differentiate between the kinds of files and this would not be 100% accurate. So, it really depends on what you are trying to do with this.

荒路情人 2024-09-03 18:38:50

您永远不必导入 java.lang.Object,它是隐式导入的,并且是所有其他类派生自的类。当您导入另一个类时,您可以根据它所在的包来导入它。因此,对于您想要使用的类,它将是:

import com.starbase.util.FileUtils;

You never have to import java.lang.Object, it's imported implicitely and is the class that every other class is derived from. When you import another class, you import it based on the package it's in. So for the class you want to use it would be:

import com.starbase.util.FileUtils;
寻找我们的幸福 2024-09-03 18:38:50

com.starbase.util.FileUtils 不在标准打包的 Java SDK 中,而是在 StarTeam SDK,您需要下载它才能使用 FileUtils#isBinary 方法。

安装后,您只需要添加:

import com.starbase.util.FileUtils;

但是,如果您不想使用第三方 SDK,请告诉我们 isBinary 对您有何帮助,我们可以找到标准的 Java 等效项。

另请澄清,import.java.lang.Object.com.starbase.util.FileUtils 不是有效的导入,您正在将两个不同的包连接在一起。

它必须是 import java.lang.Objectimport com.starbase.util.FileUtils

com.starbase.util.FileUtils is not in the standard packaged Java SDK, but instead the StarTeam SDK which you would need to download in order to use the FileUtils#isBinary method.

Once installed, you would just need to add:

import com.starbase.util.FileUtils;

However, if you do not want to use a third-party SDK, let us know how isBinary would be helpful to you and we could find a standard Java equivalent.

Also to clarify, import.java.lang.Object.com.starbase.util.FileUtils is not a valid import, you are concatenating two different packages together.

It has to be either import java.lang.Object or import com.starbase.util.FileUtils.

夏末的微笑 2024-09-03 18:38:50

完全微不足道,也许最简单,但非常不可靠!

if( filename.toLowerCase().trim().endsWith(".bin"))
     return "Binary";

Totally trivial and perhaps easiest but very unreliable!

if( filename.toLowerCase().trim().endsWith(".bin"))
     return "Binary";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文