打开文本文件的字符串到对象数组 [英] Turning String textfile into object array

查看:109
本文介绍了打开文本文件的字符串到对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个文本文件,并采取每行,并把它变成一个数组classobject。这是我的code

I want to use a textfile and take each line and put it into an classobject array. This is my code

try {
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream("Patient.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    String strLine;
    // Read file line by line
    while ((strLine = br.readLine()) != null) {
        // Print the content on the console
        System.out.println (strLine);
    }
    // Close the input stream
    in.close();
} catch (Exception e) { // Catch exception if any
    System.err.println("Error: " + e.getMessage());
}

我需要转变成一个数组对象这一点,这是我希望它看起来像

I need to transform this into an array object and this is how i want it to look like

Patient1 p[] = new Patient1[5];
p[0] = 001, "John", 17, 100, 65, 110, 110, 110, 109, 111, 114, 113, "Swaying, Nausea";
p[1] = 002, "Sun Min", 18, 101, 70, 113, 113, 110, 119, 111, 114, 113, "None";

等。

推荐答案

建筑了什么AVD曾建议,你可以完成你想要与参加了自己的价值构造的东西 - 虽然它不建议使用太多的参数构造函数(为便于阅读和调试的缘故)。根据您的数据是有序和读取的方式,你甚至可以使用的 String.split 来搞定一切成一个类型(即字符串)。

Building off of what AVD had suggested, you can accomplish what you want with a constructor that took in your values - although it's not suggested to use too many parameters in a constructor (for readability and debugging's sake). Depending on the way your data is ordered and read, you could even use String.split to get everything into one type (i.e. String).

public class Patient {

   public Patient(String name, String id, String symptoms, String measurements) {  to get the individual fields from using a delimiter.

        // Stuff to fill in the fields goes here
    }
}

您会通过调用新病人调用此(约翰,001,摇曳,恶心,...)。同样,这取决于如何读取数据的未来;如果你不能帮以合理的方式将数据了,那么你也可以选择创建的存取函数的。

You would invoke this by using the call new Patient("John, "001", "Swaying, Nausea", ...). Again, this depends on how you read the data coming in; if you cannot bunch the data up in a reasonable way, then you could also opt to create accessors and mutators.

这篇关于打开文本文件的字符串到对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆