简体   繁体   English

Java 扫描程序限制

[英]Java Scanner Limitations

I am reading through all files on my disk and searching for a string (using java.util.Scanner ), so far I've limited this to just some basic file types like .html, .txt, .java.我正在阅读磁盘上的所有文件并搜索一个字符串(使用java.util.Scanner ),到目前为止,我仅将其限制为一些基本文件类型,如 .html、.txt、.java。 .css, and .cfg. .css 和 .cfg。 My question is if I don't limit it to those file types is there a chance I'll get some error trying to read an uncommon file type?我的问题是,如果我不将其限制为这些文件类型,是否有可能在尝试读取不常见的文件类型时出错? Is there a file type that Scanner wont be able to read?是否存在扫描仪无法读取的文件类型?

That depends on what you mean by "error".这取决于您所说的“错误”是什么意思。

Attempting to read a binary file through a Scanner is guaranteed to produce inconsistent results, since those binary files would use control characters which would prematurely terminate normal line processing.尝试通过 Scanner 读取二进制文件肯定会产生不一致的结果,因为这些二进制文件会使用控制字符,这会过早终止正常的行处理。

It's also very, very easy to get an error trying to interpret a line of binary data as any other data type besides a String , since there's no way to guarantee that what you're reading could even fit in another data type.尝试将一行二进制数据解释为String之外的任何其他数据类型时也非常非常容易出错,因为无法保证您正在阅读的内容甚至适合另一种数据类型。

So you are well advised to skip the non-text files.所以,你经过深思熟虑,决定跳过非文本文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM