展开全部
代码如下:/**
* Constructs a new {@code Scanner} that produces values scanned
* from the specified input stream. Bytes from the stream are converted
* into characters using the underlying platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
*
* @param source An input stream to be scanned
*/
public Scanner(InputStream source) {
this(new InputStreamReader(source), WHITESPACE_PATTERN);
}
在这个构造器中,只有一个参e68a843231313335323631343130323136353331333431346435数source,指的是需要被扫描的输入流。System.in是一个本地(native)的final常量,是可供用户输入的标准输入流。如果你这么写:try (Scanner sc = new Scanner(new FileInputStream(new File("D:\\5.txt"))))
{
while (sc.hasNextLine())
System.out.println(sc.nextLine());
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
就把Scanner的输入源重定位到了D盘下的5.txt文件,然后就可以按行读取文件了