如何获取java中扫描仪对象中的文件名?
你好: 我得到了一个文件扫描仪。像这样的事情:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Scanner(File)
构造函数所做的第一件事是调用另一个构造函数,如下所示:因此,一旦构造了扫描仪,就无法恢复哪个文件,从而恢复哪个文件名,一个扫描仪正在阅读。
您只需在外部自行跟踪即可。例如,您可以将其封装到一个
FileScanner
中,它扩展了Scanner
但保存在字段中构造它时使用的文件。The first thing the
Scanner(File)
constructor does is to call another constructor, like this: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 extendsScanner
but saves the file used when constructing it in a field.例如,以下是其中一些:
询问:输入来自哪个文件是没有任何意义的。
修复
Filescanner
实例中封装file
和scanner
的问题。示例:在本例中,
FileScanner
知道文件存在。使用完扫描仪后,不要忘记调用fs.get_sc().close();
。For example, here are some of them:
It would not make any sense asking: from what file the input comes from.
Fix
Encapsulating
file
andscanner
in theFilescanner
instance. Example:In this case
FileScanner
is aware of the file existence. Don't forget to callfs.get_sc().close();
, when you are done using the scanner.