java 从文件读取数据到对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10490348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:17:29  来源:igfitidea点击:

Reading data from a file to an object?

javafilereader

提问by Andrew

Ok another question about my program that I'm writing called "Flight." In my tester I am creating an object from the Flight class called myFlight. The object has several fields (flight name, miles traveled, etc) but what I'm doing is reading the data from a file called input.txt and trying to put it into the object that I created. There are five lines of information on the file that I'm reading. For some reason I can't quite get it right, if anyone could help me fix this problem I would greatly appreciate it.

好的,关于我正在编写的程序的另一个问题是“飞行”。在我的测试器中,我正在从名为 myFlight 的 Flight 类创建一个对象。该对象有多个字段(航班名称、行驶里程等),但我正在做的是从名为 input.txt 的文件中读取数据并尝试将其放入我创建的对象中。我正在阅读的文件中有五行信息。出于某种原因,我不能完全正确,如果有人能帮我解决这个问题,我将不胜感激。

Here is the Constructor that has all the fields from my Flight class:

这是具有我的 Flight 类中所有字段的构造函数:

 public Flight(String name, int num, int miles, String origin, String destination)
  {
    Airlinename = name;
    flightnumber = num;
    numofmiles = miles;
    Origincity = origin;
    Destinationcity = destination;
  }

And the part of my program where I created the object and try to read the data from the file. I had created a blank constructor in my class too because I wasn't sure if I was supposed to put anything in the object when I created it.

以及我创建对象并尝试从文件中读取数据的程序部分。我也在我的类中创建了一个空白的构造函数,因为我不确定在创建对象时是否应该在对象中放入任何东西。

Flight myFlight = new Flight();

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

    while (inputFile.hasNext())
    {
      myFlight = inputFile.nextLine();
    }

    inputFile.close();
  }
}

回答by Paul Vargas

Just in case you use special characters, you need to modify your program so you can read them correctly.

以防万一您使用特殊字符,您需要修改您的程序,以便您可以正确读取它们。

Scanner inputFile = new Scanner(myFile, "UTF-8");

On the other hand, if the text file contains the following, possibly subsequent calls to nextInt()generate a runtime exception.

另一方面,如果文本文件包含以下内容,则可能后续调用会nextInt()生成运行时异常。

Gran Espa?a
1
1001
New York
Los Angeles

If that were the case, the reading should be different.

如果是这样的话,读数应该是不同的。

myFlight = new Flight(inputFile.nextLine(), 
           Integer.parseInt(inputFile.nextLine()), 
           Integer.parseInt(inputFile.nextLine()), 
           inputFile.nextLine(), 
           inputFile.nextLine());

As with any program, when adding more conditions to improve the model, it needs more and more code.

与任何程序一样,当添加更多条件来改进模型时,它需要越来越多的代码。

Good luck.

祝你好运。

回答by Lyn Headley

try

尝试

myFlight = new Flight(inputFile.next(), inputFile.nextInt(), 
           inputFile.nextInt(), inputFile.next(), inputFile.next());

回答by ?smet Alkan

You can't directly assign a string line to an object, you need to parse the line into parts and assign values to variables one by one.

不能直接给对象赋值字符串行,需要将行解析为多个部分,并一一赋值给变量。

回答by raddykrish

How is your input text organized? The Scanner's nextLine()method is returning a String object and definitely you cannot assign that to a type Flight. There are different methods to get the different values in the Scanner like nextInt(), nextFloat().

您的输入文本是如何组织的?Scanner 的nextLine()方法返回一个 String 对象,您绝对不能将其分配给 Flight 类型。有不同的方法可以在 Scanner 中获取不同的值,例如nextInt(), nextFloat().

Basically you can try like this

基本上你可以这样试试

Flight myFlight = new Flight();
myFlight.setFlightName(inputFile.next());
myflight.setMiles(inputFile.nextInt();

etc.

等等。

This is just a sample, you need to check the format you have in the input.txt file.

这只是一个示例,您需要检查 input.txt 文件中的格式。