使用java.util.Scanner逐字节读取文件 [英] using java.util.Scanner to read a file byte by byte

查看:178
本文介绍了使用java.util.Scanner逐字节读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用java.util.Scanner逐个字符地读取一行文件。但是我得到了这个异常:

 线程main中的异常java.util.InputMismatchException:对于输入字符串:我的文件的内容
at java.util.Scanner.nextByte(Scanner.java:1861)
at java.util.Scanner.nextByte(Scanner.java:1814)
at p008。 main(p008.java:18)< - 我在哪里行scan.nextByte()

这是我的代码:

  public static void main(String [] args)throws FileNotFoundException {
File source = new File (file.txt);
扫描仪扫描仪=新扫描仪(来源);
while(scanner.hasNext()){
System.out.println((char)scanner.nextByte ());
}
scanner.close()
}



<有没有人对我可能做错了什么有任何想法?



编辑:我意识到我写了hasNext()而不是hasNextByte()。但是如果我这样做的话它不打印任何东西。

解决方案

为什么你想要使用扫描器逐字节读取文件?这就像用手推车搬运口袋一样。 (如果你真的需要一个独轮车换口袋,请告诉我,这样我才能成为你的朋友。)



但严重的是:Class InputStream 简单可靠地从文件中读取字节,并且不执行任何其他操作。



Class scanner 最近被引入Java API,因此教科书示例可以从文件中提取数据,而不是通常使用级联的新的BufferedReader(新的InputStream)。它的专长是从自由格式的输入文件中输入数字和字符串。 nextByte()方法实际上从输入流中读取一个或几个十进制数字(如果它们在那里)并将扫描的数字转换为单个字节值。 / p>

如果您正在读取字节,为什么要将它们输出为 char s?字节字符,并且在某些地方强力互换将失败。如果你想看到那些字节的值,按原样打印它们,你会看到0到25​​5之间的小整数。



如果你想阅读<来自文件的code> char , FileReader 是适合您的课程。


I'm trying to read a one line file character by character using java.util.Scanner. However I'm getting this exception":

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.

解决方案

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 InputStream reads bytes from a file, simply and reliably, and does nothing else.

Class scanner was 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.

And if you're reading bytes, why do you want to output them as chars? Bytes are not chars, 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.

If you want to read chars from a file, FileReader is the class for you.

这篇关于使用java.util.Scanner逐字节读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆