如何使用扫描器读取文件对象。

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

how do i use a scanner to read a file object

问题

从File类中获取的文件对象,然后使用包装在其中的Scanner对象来读取文件,假设文件中包含如下示例:

Student{name=Jill Gall,age=21,gpa=2.98}

要将其分隔成字段,然后跳过下一步,因为我只需在此处调用setter方法,然后设置这些值。接下来,我想将这些字段保存为一个Student对象,然后将这些对象保存在一个数组中以便返回。

我并不是真的在寻找整个问题的解决方案,我只是想知道如何调用包装在File对象周围的Scanner对象以对其进行读取。我可以使用子字符串来获取字段并将它们保存到新字段中,我相当确定我可以使用for循环将它们加载到一个可以返回的数组中。我之所以将整个问题都列出来,是为了向您提供关于任务的尽可能详细的信息。

目前,我最好的尝试是这样的:

Scanner n = new Scanner(System.in);
n.commandgoeshere?(filename);

我应该如何让Scanner输出数据,以便我可以获取子字符串并执行上述所需的操作呢?

英文:

like the question how would I use a file object taken from the File class
<https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/io/File.html>

and use a scanner object wrapped around this to read the file,
then say a example like this is contained in the file,
Student{name=Jill Gall,age=21,gpa=2.98}
separate it into fields, skip next step as I just have to call the setters here, and then set the values
I then to save the fields as a Student Object and then save those in a array that I can return?

I'm not really looking for a solution to the entire problem here I'm just wondering as to what the syntax is for calling a scanner object wrapped around a File object to read it, I can use substring to grab the fields to separate and save them onto the new fields, and I'm pretty sure I can just use a for loop to load these onto a array that i can return, the only reason I listed out the entire problem for you guys is so I can give you guys the most details about the task as I can give here

right now my best attempt is this

Scanner n = new Scanner(System.in);
n.commandgoeshere?(filename);

how would I get the scanner to output the data so that i can take a substring and do the stuff i need to do above?

答案1

得分: 0

你必须在扫描器的构造函数中提供一个输入流,目前你正在将系统输入流(来自控制台)提供给扫描器。我认为你正在寻找类似这样的解决方法:

File file = new File("");

try {
    Scanner scanner = new Scanner(file);
    while (scanner.hasNext()) {
        String nextLine = scanner.nextLine();
        // 或者使用正则表达式?
        String extractedPattern = scanner.next("some pattern");
    }

} catch (FileNotFoundException e) {
    e.printStackTrace();
}
英文:

You have to provide an input stream in the constructor of the scanner, atm you are providing the system input stream (from the console) to the scanner. I think something like this is what you are looking for:

    File file = new File(&quot;&quot;);

    try {
        Scanner scanner = new Scanner(file);
        while (scanner.hasNext()) {
            String nextLine = scanner.nextLine();
            // or use regex ?
            String extractedPattern = scanner.next(&quot;some pattern&quot;);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

huangapple
  • 本文由 发表于 2020年10月2日 03:33:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64162081.html
匿名

发表评论

匿名网友

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

确定