Java,读入文本文件,打印新文件,删除一些列并使用列中的操作?

时间:2015-07-24 19:13:21

标签: java file-io

我必须在一个包含多个列的文本文件中读取,第一列是州号(01-50),第二列是某个住宅位置的名称,第三列是每个城镇的总人口数住房位置在,第4列是可能的住房容量,第5列有前一列的可用住房数。第6和第7列有无用的ID号和日期。每个州都有多个住房位置。

目标是创建一个新输出,每个状态有一行。第1列必须是州编号,第2列必须是每个城镇人口的每个州的总人口数,第3列必须是可能的住房容量总数,第4列必须是总可用性,第5列必须是可用性的百分比。我必须摆脱我不需要的列。

这是我到目前为止的代码:

import java.io.*;
public class housing {
    public static void main(String[] args) {
        String fileName = "housingdata.txt";
        try {
            FileReader File = new FileReader(fileName);
            BufferedReader reader = new BufferedReader(File);
            String line;

            while ((line = bufferReader.readLine()) != null) {
                System.out.println(line);
                String[] splittedLine = line.split("\\s+");
            }
        }
    } catch(Exception) {
       System.out.println("Error.");
    }
}

示例数据:

01 Grainey County Housing Complex     2000     500      340 ID: 45440  March2010   
02 MainStays Housing Complex          3465     400       82 ID: 30094  March2010    

1 个答案:

答案 0 :(得分:0)

看起来您正在处理任务列表。首先读取并存储数据(以便能够处理它)

为此你可能想要构建一个数据结构,然后为它选择一个集合。 ArrayList应该通常可以工作(没有双关语)

class Data
{
    private int stateID;
    private String name;
    private int totalPopulation;
    private int totalAvailable;
    private int hereAvailable;

    // Appropriate setters/getters and constructor
    public Data(int stateID, String name, int population, int availablePop, int availHere, int ID, String date) { ... }
}

我会创建一个ArrayList继承类,以便在允许添加特定功能的同时使用ArrayList的功能。

class DataList extends ArrayList<Data>
{
    public addData(Data data) { this.add(data); }
}

然后你只是收集数据。我写的是这样的:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Housing {

    static String fileName = "housingdata.txt";
    static DataList data = new DataList();

    public static void main(String[] args)
    {
        String line;

        try(FileReader file = new FileReader(fileName))
        {
            BufferedReader bufferReader = new BufferedReader(file);
            Pattern r = Pattern.compile("(\d{2})\s+([^\d]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+ID:\s+(\d+)\s+([\w\d]+)");

            while ((line = bufferReader.readLine()) != null)
            {
                System.out.println(line);
                Matcher m = r.matcher(line);

                data.addData(
                        new Data(
                                Integer.parseInt(m.group(0)),
                                m.group(1),
                                Integer.parseInt(m.group(2)),
                                Integer.parseInt(m.group(3)),
                                Integer.parseInt(m.group(4)),
                                Integer.parseInt(m.group(5)),
                                m.group(6)
                        )
                );
            }

        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

在此之后,您只需要弄清楚如何计算生成输出所需的数据。我想我会建议使用hashset作为一些观点。事实上,我想如果你构建了自己的自定义哈希表,你可以轻松地使用它来返回计算。还有很多其他方法可以做到,这就是我到目前为止快速将它组合在一起的方式。