如何获取java中扫描仪对象中的文件名?

发布于 2024-10-23 13:55:08 字数 227 浏览 3 评论 0原文

你好: 我得到了一个文件扫描仪。像这样的事情:

Scanner theScan = new Scanner(new File("name.file"));

我想知道是否有办法从 Scanner 对象获取文件的名称。 我尝试过:

theScan.getName();

但这仅适用于文件对象。 感谢您的意见。

Hello:
I was given a Scanner that is a File. Something like this:

Scanner theScan = new Scanner(new File("name.file"));

I was wondering if there is a way to get the name of the file from the Scanner object.
I tried:

theScan.getName();

but that just works for File objects.
Thanks for your input.

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

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

发布评论

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

评论(2

书信已泛黄 2024-10-30 13:55:08

Scanner(File) 构造函数所做的第一件事是调用另一个构造函数,如下所示:

this((ReadableByteChannel)(new FileInputStream(source).getChannel()));

因此,一旦构造了扫描仪,就无法恢复哪个文件,从而恢复哪个文件名,一个扫描仪正在阅读。

您只需在外部自行跟踪即可。例如,您可以将其封装到一个 FileScanner 中,它扩展了 Scanner 但保存在字段中构造它时使用的文件。

The first thing the Scanner(File) constructor does is to call another constructor, like this:

this((ReadableByteChannel)(new FileInputStream(source).getChannel()));

so once the scanner has been constructed, there is no way to recover which file, and thus which filename, a scanner is reading from.

You'll simply have to keep track of this yourself on the outside. You could for instance encapsulate this into a FileScanner which extends Scanner but saves the file used when constructing it in a field.

风吹过旳痕迹 2024-10-30 13:55:08

API 是你的朋友

扫描仪可以从任何物体上读取文本
实现可读接口。如果
底层的调用
可读的
Readable.read(java.nio.CharBuffer)
方法抛出 IOException 然后
扫描仪假设结束
已达到输入。最
最近抛出的 IOException
可以检索底层可读的
通过 ioException() 方法。

例如,以下是其中一些:

    Scanner sc1 = new Scanner("some string");
    Scanner sc2 = new Scanner(System.in);

询问:输入来自哪个文件是没有任何意义的。

修复

Filescanner 实例中封装 filescanner 的问题。示例:

public static class FileScanner {
    private final Scanner _sc;
    private final File _file;
    public Scanner get_sc() {return _sc;}
    public File get_file() {return _file;}

    public FileScanner(File f) throws 
           FileNotFoundException, NullPointerException  {
        super();
        this._sc = new Scanner(f);
        this._file = f;
    }
}

在本例中,FileScanner 知道文件存在。使用完扫描仪后,不要忘记调用 fs.get_sc().close();

API is your friend

A scanner can read text from any object which
implements the Readable interface. If
an invocation of the underlying
readable's
Readable.read(java.nio.CharBuffer)
method throws an IOException then the
scanner assumes that the end of the
input has been reached. The most
recent IOException thrown by the
underlying readable can be retrieved
via the ioException() method.

For example, here are some of them:

    Scanner sc1 = new Scanner("some string");
    Scanner sc2 = new Scanner(System.in);

It would not make any sense asking: from what file the input comes from.

Fix

Encapsulating file and scanner in the Filescanner instance. Example:

public static class FileScanner {
    private final Scanner _sc;
    private final File _file;
    public Scanner get_sc() {return _sc;}
    public File get_file() {return _file;}

    public FileScanner(File f) throws 
           FileNotFoundException, NullPointerException  {
        super();
        this._sc = new Scanner(f);
        this._file = f;
    }
}

In this case FileScanner is aware of the file existence. Don't forget to call fs.get_sc().close();, when you are done using the scanner.

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