Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 扫描程序与文件输入流_Java_Eclipse_Io - Fatal编程技术网

Java 扫描程序与文件输入流

Java 扫描程序与文件输入流,java,eclipse,io,Java,Eclipse,Io,及 Scanner.nextByte()和FileInputStream.read()之间有什么区别 我试图理解它,因为当我使用FileInputStream从包含简单文本的文件中读取字节(一个接一个)时,它工作得很好。但是当我使用Scanner时,Scanner.nextByte()不会返回任何内容 这是为什么?Scanner.nextByte()将读取下一个标记,如果它可以作为字节计算,则返回它,而FileInoutStream.read()将返回文件的每个字节。考虑这个例子: FileIn

Scanner.nextByte()
FileInputStream.read()
之间有什么区别

我试图理解它,因为当我使用
FileInputStream
从包含简单文本的文件中读取字节(一个接一个)时,它工作得很好。但是当我使用
Scanner
时,
Scanner.nextByte()
不会返回任何内容

这是为什么?

Scanner.nextByte()将读取下一个标记,如果它可以作为字节计算,则返回它,而FileInoutStream.read()将返回文件的每个字节。考虑这个例子:

FileInputStream d = new FileInputStream("target.txt");
将其作为
target.txt
的内容:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class SO {
  public static void scanner() throws FileNotFoundException {
    System.out.println("Reading with the Scanner Class:");
    Scanner scanner= new Scanner(new File("target.txt"));
    while(scanner.hasNext()) {
      try {
        System.out.println("A Byte:"+scanner.nextByte());
      } catch(InputMismatchException e) {
        System.out.println("Not a byte:"+scanner.next());
      }
    }
    scanner.close();
  }

  public static void stream() throws IOException {
    System.out.println("Reading with the FileInputStream Class:");
    FileInputStream d = new FileInputStream("target.txt");
    int b = -1;
    while((b = d.read()) != -1) {
      System.out.print((byte)b+" ");
    }
    d.close();
    System.out.println();
  }

  public static void main(String...args) throws IOException {
    scanner();
    stream();
  }
}
这将产生以下输出:

Next up is a byte:
6
Wasn't that fun?

这些课程实际上在做非常不同的事情

FileInputStream
实际上是从输入文件中读取原始字节,而
Scanner
则将文件解析为空格分隔的标记,并在您请求时尝试将每个标记转换为请求的类型

例如,如果您的输入文件如下所示:

Reading with the Scanner Class:
Not a byte:Next
Not a byte:up
Not a byte:is
Not a byte:a
Not a byte:byte:
A Byte:6
Not a byte:Wasn't
Not a byte:that
Not a byte:fun?
Reading with the FileInputStream Class:
78 101 120 116 32 117 112 32 105 115 32 97 32 98 121 116 101 58 10 54 10 87 97 115 110 39 116 32 116 104 97 116 32 102 117 110 63 
FileInputStream.read()
将对
1
进行字节计算,并返回其值:
49
Scanner.nextByte()
将读取
1
,并尝试将其作为基数为10的整数正则表达式计算,并给出:
1

另一方面,如果输入文件包含

1
然后
FileInputStream.read()
a
作为一个字节计算,并返回其值:
97
Scanner.nextByte()
将读取
a
并尝试将其作为基数为10的整数正则表达式进行求值,然后抛出a
java.util.InputMismatchException
Scanner.nextByte()与
FileInputStream.read()不同。

nextByte()
方法将输入的下一个标记作为字节进行扫描。如果下一个标记无法转换为如下所述的有效字节值,此方法将抛出InputMismatchException。如果翻译成功,扫描仪将通过匹配的输入

如果下一个标记与上面定义的
整数
正则表达式匹配,则该标记将转换为字节值,就像删除所有特定于区域设置的前缀、组分隔符和特定于区域设置的后缀一样,然后通过Character.digit将非ASCII数字映射为ASCII数字,并在其前面加上负号(-)如果存在特定于语言环境的否定前缀和后缀,并将结果字符串以指定的基数传递给
Byte.parseByte

i、 e.
nextByte()
方法尝试匹配数字的文本表示形式,将其存储为字节值

另一方面,
FileInputStream.read()
将从输入流中读取一个字节的数据


参考资料:

Sooo换句话说,Scanner.nextByte()只读取文件中的整数,对吗?是否在扫描仪上使用了分隔符,如
Scanner.useDelimiter(REGEX\u INPUT\u BOUNDARY\u start)。next()
1
a