带对象的数组;将文本行拆分为对象成员?

时间:2018-06-05 09:29:08

标签: java arrays object

我需要一些关于此代码的帮助。问题是我不知道如何将加载的行拆分为对象成员,然后将其toString()方法推送到另一个文件中。 Heres是主要代码:

public class TXTtoJSON {
    public static void main(String[] args) throws IOException {

        String fileName = "temp.csv";
        String line = null; 


    transaction trans=new transaction();

        try {

        FileReader fileReader=new FileReader(fileName);
            BufferedReader bufferedReader= new BufferedReader(fileReader);

        FileWriter fileWriter = new FileWriter("json.json");
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

            while((line=bufferedReader.readLine()) !=null) {


                bufferedWriter.write(line + "\n");

                System.out.println(line);

            }
            bufferedReader.close();
            bufferedWriter.close();
        }
        catch(FileNotFoundException e) {
            System.out.println("Unable to open File '" + fileName + "'");
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }
}

并且继承了班级交易

public class transaction {
    private String id;
    private String name;
    private String routine_name;
    private String correct_count;
    private String stopless;
    private String startless;
    private String average;
    private String min;
    private String max;
    private String sum;
    private String percent;

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{\"index\":{\"_index:\"test\",\"_id\":").append(id);
        sb.append("}}\n");
        sb.append("{\"BBL\":").append(name);
        sb.append(",\"SERVICE\":").append(routine_name);
        sb.append(",\"Correct Count\":").append(correct_count);
        sb.append(",\"Stopless\":").append(stopless);
        sb.append(",\"Startless\":").append(startless);
        sb.append(",\"Average\":").append(average);
        sb.append(",\"Min\":").append(min);
        sb.append(",\"Max\":").append(max);
        sb.append(",\"Sum\":").append(sum);
        sb.append(",\"Percent\":").append(percent);
        sb.append("}\n");
        return sb.toString();

    }
    public String get_id() {
        return id;
    }
    public void set_id(String id) {
        this.id=id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name=name;
    }
    public String GetRoutineName() {
        return routine_name;
    }
    public void SetRoutineName(String routine_name) {
        this.routine_name=routine_name;
    }
    public String get_count() {
        return correct_count;
    }
    public void set_count(String count) {
        this.correct_count=count;
    }
    public String getStopless() {
        return stopless;
    }
    public void setStopless(String stop) {
        this.stopless=stop;
    }
    public String getStartless() {
        return startless;
    }
    public void setStartless(String start) {
        this.startless=start;
    }
    public String getAverage() {
        return average;
    }
    public void setAverage(String average) {
        this.average=average;
    }
    public String getMin() {
        return startless;
    }
    public void setMin(String min) {
        this.min=min;
    }
    public String getMax() {
        return max;
    }
    public void setMax(String max) {
        this.max=max;
    }
    public String getSum() {
        return sum;
    }
    public void setSum(String sum) {
        this.sum=sum;
    }
    public String getPercent() {
        return percent;
    }
    public void setPercent(String percent) {
        this.percent=percent;
    }
}

我希望你看到问题,我知道如何写入文件,但我不能用toString方法得到它想要的格式?此ID还没有在我正在读取的文件中定义,以任何方式使for循环设置ID manualy?

我猜需要Arrays,但如果有人能解释语法:)

提前Thx:)

0 个答案:

没有答案