File和Scanner对象用于在Java中打开和读取数据。

huangapple go评论75阅读模式
英文:

File and Scanner objects for opening and reading data in Java

问题

我的教材陈述如下:

您还可以使用Scanner类来从文件中读取输入。不是将System.in传递给Scanner类构造函数,而是传递一个对File对象的引用。以下是一个示例:

File myFile = new File("Customers.txt");
Scanner inputFile = new Scanner(myFile);

第一条语句创建了File类的实例。File类位于Java API中,用于表示文件。请注意,我们将字符串"Customers.txt"传递给构造函数。这创建了一个代表文件"Customers.txt"的File对象。

在第二条语句中,我们将对这个File对象的引用作为参数传递给Scanner类的构造函数。这创建了一个使用文件"Customers.txt"作为其输入源的Scanner对象。

我的问题是:我无法理解为什么需要一个File对象?我们不能像下面这样写上述代码来读取文件吗?

Scanner inputFile = new Scanner("Customers.txt");

如果Java需要一个File对象和一个Scanner对象,它是如何确切地在后台工作的呢?

英文:

My textbook states the following:

You can also use the Scanner class to read input from a file. Instead of passing System.in to the Scanner class constructor, you pass a reference to a File object. Here is an example:

File myFile = new File("Customers.txt");
Scanner inputFile = new Scanner(myFile);

The first statement creates an instance of the File class. The File class is in the Java API, and is used to represent a file. Notice that we have passed the string "Customers.txt" to the constructor. This creates a File object that represents the file "Customers.txt."

In the second statement, we pass a reference to this File object as an argument to the Scanner class constructor. This creates a Scanner object that uses the file "Customers.txt" as its source of input.

My question: I am unable to understand why do we need a File object? Can't we write the above code just as below to read files?

Scanner inputFile = new Scanner("Customers.txt");

If Java needs a File object and a Scanner object, how is it exactly working behind?

答案1

得分: 2

你需要将 File 对象传递给 Scanner 构造函数,以调用 Scanner(File source) 构造函数,而不是 Scanner(String source) 构造函数。如果你将字符串 "Customers.txt" 用作参数,将会使用带有字符串作为参数的构造函数,并且根据文档中的说明,它会产生从指定字符串中扫描的值,而不是从文件中扫描,这与你的要求不符。

英文:

You need to pass the File object to the Scanner constructor to invoke Scanner(File source) constructor and not Scanner(String source) constructor. In case you use the string "Customers.txt" as the argument the constructor with the string as argument will be used and it will produce values scanned from the specified string as explained in the documentation and not from the file as you want.

答案2

得分: 1

我认为这里发生的情况是扫描器类要求您输入一个原始数据类型(也称为String、int等),而不是一个路径。

Scanner inputFile = new Scanner("Customers.txt");

如果您编写上面的代码并显示它,它将会是空白的,因为扫描器类无法理解您的参数。另一方面,File类接受一个路径作为参数,并创建一个文件对象,该对象可以用作Scanner类的参数。因此,在使用扫描器类之前,您必须创建一个文件对象。

英文:

What I believe is happening here is that the scanner class requires you to enter a primitive data type(aka String, int, etc.) and not a path.

Scanner inputFile = new Scanner("Customers.txt");

If you write the above code and display it, it will just be blank since the scanner class cannot understand your parameter. On the other hand, the class File takes in a path as a parameter and creates a file object that can be used as a parameter for the Scanner class. Hence why you have to create a file object before using the scanner class.

huangapple
  • 本文由 发表于 2020年8月10日 01:17:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63329239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定