使用 java.util.Scanner 逐字节读取文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2039229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using java.util.Scanner to read a file byte by byte
提问by openidsucks
I'm trying to read a one line file character by character using java.util.Scanner. However I'm getting this exception":
我正在尝试使用 java.util.Scanner 逐个字符地读取一行文件。但是我得到了这个例外”:
Exception in thread "main" java.util.InputMismatchException: For input string: "contents of my file"
at java.util.Scanner.nextByte(Scanner.java:1861)
at java.util.Scanner.nextByte(Scanner.java:1814)
at p008.main(p008.java:18) <-- line where I do scanner.nextByte()
Here's my code:
这是我的代码:
public static void main(String[] args) throws FileNotFoundException {
File source = new File("file.txt");
Scanner scanner = new Scanner(source);
while(scanner.hasNext()) {
System.out.println((char)scanner.nextByte());
}
scanner.close()
}
Does anyone have any ideas as to what I might be doing wrong?
有没有人对我可能做错了什么有任何想法?
Edit: I realized I wrote hasNext() instead of hasNextByte(). However if I do that it doesn't print out anything.
编辑:我意识到我写的是 hasNext() 而不是 hasNextByte()。但是,如果我这样做,它不会打印出任何内容。
回答by Carl Smotricz
Why on earth would you want to use a scanner to read a file byte by byte? That's like using a wheelbarrow to transport your pocket change. (If you really need a wheelbarrow for your pocket change, let me know so I can become your friend).
为什么要使用扫描仪逐字节读取文件?这就像使用独轮车运送零钱一样。(如果您真的需要手推车来换零钱,请告诉我,以便我成为您的朋友)。
But seriously: Class InputStreamreads bytes from a file, simply and reliably, and does nothing else.
但InputStream说真的:Class从文件中读取字节,简单而可靠,什么都不做。
Class scannerwas recently introduced into the Java API so textbook examples could pull data out of a file with less pain than is usually involved with using the cascade of new BufferedReader(new InputStream). Its specialty is inputting numbers and strings from free-form input files. The nextByte()method actually reads one or a few decimal digits from the input stream (if they're there) and converts the number thus scanned into a single byte value.
类scanner最近被引入到 Java API 中,因此教科书示例可以从文件中提取数据,而不像通常使用new BufferedReader(new InputStream). 它的专长是从自由格式的输入文件中输入数字和字符串。该nextByte()方法实际上从输入流中读取一个或几个十进制数字(如果它们在那里)并将由此扫描的数字转换为单个字节值。
And if you're reading bytes, why do you want to output them as chars? Bytes are notchars, and brute-force interconverting will fail in some places. If you want to see the values of those bytes, print them out as they are and you'll see small integers between 0 and 255.
如果您正在读取字节,为什么要将它们输出为chars?字节不是字符,暴力转换在某些地方会失败。如果您想查看这些字节的值,请将它们按原样打印出来,您将看到 0 到 255 之间的小整数。
If you want to read chars from a file, FileReaderis the class for you.
如果您想char从文件中读取s,FileReader是适合您的课程。
回答by Michael Borgwardt
Scanner is for parsingtext data - its nextByte()method expects the input to consist of digits (possibly preceded by a sign).
Scanner 用于解析文本数据 - 它的nextByte()方法期望输入由数字组成(可能前面有一个符号)。
You probably want to use a FileReaderif you're actually reading text data, or a FileInputStreamif it's binary data. Or a FileInputStreamwrapped in an InputStreamReaderif you're reading text with a specific character encoding (unfortunately, FileReaderdoes not allow you to specify the encoding but uses the platform default encoding implicitly, which is often not good).
FileReader如果您实际上是在读取文本数据,则您可能想要使用 a ,FileInputStream如果它是二进制数据,则可能需要使用 a 。或者FileInputStream,InputStreamReader如果您正在阅读具有特定字符编码的文本(不幸的是,FileReader不允许您指定编码,而是隐式使用平台默认编码,这通常不好),则将其包裹在 an 中。
回答by McDowell
When troubleshooting Scanner, check for underlying I/O errors:
进行故障排除时Scanner,请检查潜在的 I/O 错误:
if(scanner.ioException() != null) {
throw scanner.ioException();
}
Though I'm with the others - this probably isn't the right class for the job. If you want byte input, use an InputStream(in this case, FileInputStream). If you want char input, use a Reader(e.g. InputStreamReader).
虽然我和其他人在一起 - 这可能不是适合这份工作的课程。如果您想要字节输入,请使用InputStream(在本例中为FileInputStream)。如果您想要字符输入,请使用Reader(例如InputStreamReader)。
回答by Bernhard Barker
Scanneris all about reading delimited text (see the docs).
Scanner都是关于阅读分隔文本(请参阅文档)。
nextBytewill keep reading until it gets to whichever delimiter you specified (whitespace by default) and then try to convert that string into a byte.
nextByte将继续读取,直到到达您指定的任何分隔符(默认为空格),然后尝试将该字符串转换为字节。
So if you have 123 456in a file, one call to nextBytewill return 123, not 49(the decimal value for the 1character).
因此,如果您123 456在一个文件中,则一次调用nextByte将返回123,而不是49(1字符的十进制值)。
If you want to read byte-by-byte, you could use FileInputStream.
如果要逐字节读取,可以使用FileInputStream.

