如何从两个文本文件中读取数据并链接数据?

时间:2015-06-25 12:08:26

标签: java arrays

我想从 Host.txt Instance.txt 中读取数据,我能够读取数据,但我不知道它是怎么回事联!

以下是主机文件:

Host ID     Number of Slots        Data Centre
=======     ===============       ============
2,                  4,                  0
5,                  4,                  0
7,                  3,                  0
9,                  3,                  1
3,                  3,                  1
10,                 2,                  2
6,                  4,                  2
8,                  2,                  2

以下是实例文件:

Instance ID            Customer        Host ID
===========           =========        =======
1,                         8,             2
2,                         8,             2
3,                         8,             2
4,                         8,             7
5,                        15,             7
6,                        16,             9
7,                        13,             9
8,                         9,             3
9,                        13,             3
10,                       16,             5
11,                       15,             8
12,                        8,             6
13,                       16,             8
14,                        9,             9
15,                        9,             7

以下是我用来读取文件的代码:

public class CloudStructure {

    public static void main(String[] args) {

        readHost("C:\\tmp\\HostState.txt");
        readInstance("C:\\tmp\\InstanceState.txt");
    }

    public static void readHost(String filePath)
    {
        String line = "";

        ArrayList<String> host = new ArrayList<String>();
        ArrayList<String> slot = new ArrayList<String>();
        ArrayList<String> centre = new ArrayList<String>();

        CloudStructureData getHostTxtData = new CloudStructureData();

        FileReader fr;
        BufferedReader br;

        String[] HostID = null;
        String[] Slots = null;
        String[] DataCentre = null;

          try {
               fr = new FileReader(filePath);
               br = new BufferedReader(fr);

               while((line = br.readLine()) != null) 
               {   
                   HostID = line.split(",");
                   Slots = line.split(",");
                   DataCentre = line.split(",");                

                   host.add(HostID[0]); 
                   slot.add(Slots[1]);          
                   centre.add(DataCentre[2]);
               }
              }
              catch(FileNotFoundException fN) {
               fN.printStackTrace();
              }
              catch(IOException e) {
               System.out.println(e);
              } 

         getHostTxtData.setHostID(host);
         getHostTxtData.setSlot(slot);
         getHostTxtData.setCentre(centre);

         System.out.println("Host File");
         System.out.println("=============");
         System.out.println("Hosts:      " + getHostTxtData.getHostID());
         System.out.println("Slots:      " + getHostTxtData.getSlot());
         System.out.println("DataCentre: " + getHostTxtData.getCentre());
    }   

    public static void readInstance(String filePath)
    {
        String line = "";

        ArrayList<String> instance = new ArrayList<String>();
        ArrayList<String> customer = new ArrayList<String>();
        ArrayList<String> host = new ArrayList<String>();

        CloudStructureData getHostTxtData = new CloudStructureData();

        FileReader fr;
        BufferedReader br;

        String[] InstanceId = null;
        String[] CustId = null;
        String[] HostId = null;

          try {
               fr = new FileReader(filePath);
               br = new BufferedReader(fr);

               while((line = br.readLine()) != null) 
               {   
                   InstanceId = line.split(",");
                   CustId = line.split(",");
                   HostId = line.split(",");                

                   instance.add(InstanceId[0]); 
                   customer.add(CustId[1]);         
                   host.add(HostId[2]);
               }
              }
              catch(FileNotFoundException fN) {
               fN.printStackTrace();
              }
              catch(IOException e) {
               System.out.println(e);
              } 

         getHostTxtData.setInstance(instance);
         getHostTxtData.setCust(customer);
         getHostTxtData.setHostInst(host);

         System.out.println();
         System.out.println("Instance File");
         System.out.println("=============");
         System.out.println("Instance: " + getHostTxtData.getIntstanceID());
         System.out.println("Customer: " + getHostTxtData.getCustomer());
         System.out.println("Host:     " + getHostTxtData.getHostInst());
    }       
}

我的另一堂课:

package cloud;

import java.util.ArrayList;

public class CloudStructureData {

    private ArrayList<String> sHostID;
    private ArrayList<String> sSlot;
    private ArrayList<String> sCentre;

    private ArrayList<String> sInstanceID;
    private ArrayList<String> sCust;
    private ArrayList<String> sHostInst;

    CloudStructureData(){
        sHostID = new ArrayList<String>();
        sSlot = new ArrayList<String>();
        sCentre = new ArrayList<String>();

        sInstanceID = new ArrayList<String>();
        sCust = new ArrayList<String>();
        sHostInst = new ArrayList<String>();        
    }

    // Get Statements Host
    public ArrayList<String> getHostID(){
        return sHostID;
    }

    public ArrayList<String> getSlot(){
        return sSlot;
    }

    public ArrayList<String> getCentre(){
        return sCentre;
    }   

    // Get Statements Instance
    public ArrayList<String> getIntstanceID(){
        return sInstanceID;
    }

    public ArrayList<String> getCustomer(){
        return sCust;
    }

    public ArrayList<String> getHostInst(){
        return sHostInst;
    }       

    //Set Statements Host
    public void setSlot(ArrayList<String> Slot){
        sSlot = Slot;
    }

    public void setCentre(ArrayList<String> Centre){
        sCentre = Centre;
    }

    public void setHostID(ArrayList<String> HostID) {
        sHostID = HostID;
    }

    //Set Statements Instance
    public void setInstance(ArrayList<String> InstanceID){
        sInstanceID = InstanceID;
    }

    public void setCust(ArrayList<String> Cust){
        sCust = Cust;
    }

    public void setHostInst(ArrayList<String> HostInst) {
        sHostInst = HostInst;
    }   

}

这是我的程序的输出:

Host File
=============
Hosts:      [2, 5, 7, 9, 3, 10, 6, 8]
Slots:      [4, 4, 3, 3, 3, 2, 4, 2]
DataCentre: [0 , 0 , 0 , 1, 1, 2, 2, 2]

Instance File
=============
Instance: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Customer: [8, 8, 8, 8, 15, 16, 13, 9, 13, 16, 15, 8, 16, 9, 9]
Host:     [2, 2, 2, 7, 7, 9, 9, 3, 3, 5, 8, 6, 8, 9, 7]

HostState.txt输入列出了不同的主机及其位置。 InstanceState.txt输入列出正在运行的实例以及它们正在运行的主机。

1 个答案:

答案 0 :(得分:0)

我建议你为Host创建一个类,为Instance创建一个类。 Instance类将引用Host对象。您还可以使用内置Map来帮助搜索主机和实例。以下是对代码的更改,以说明上述想法。虽然我不完全同意您的设计,但我尽量使用原始代码保持一致。 无论如何,下面的内容应该让您了解如何实现您的要求:

Host.java

package cloud;

public class Host {
    private String hostId;
    private String numberOfSlots;
    private String dataCentre;

    public Host(String hostId, String numberOfSlots, String dataCentre) {
        this.hostId = hostId;
        this.numberOfSlots = numberOfSlots;
        this.dataCentre = dataCentre;
    }

    // valid getters and setters follow...

}

Instance.java

package cloud;

public class Instance {
    private String instanceId;
    private String customer;
    private Host host; // this will be a reference to the related host object

    public Instance(String instanceId, String customer, Host host) {
        this.instanceId = instanceId;
        this.customer = customer;
        this.host = host;
    }

    // valid getters and setters follow...

}

CloudStructureData.java

package cloud;

import java.util.HashMap;
import java.util.Map;

public class CloudStructureData {

    private Map<String, Host> hosts;
    private Map<String, Instance> instances;

    public CloudStructureData(){

        hosts = new HashMap<String, Host>(); // the key will be HostId
        instances = new HashMap<String, Instance>(); // the key will be InstanceId
    }

    public Map<String, Host> getHosts() {
        return hosts;
    }

    public Map<String, Instance> getInstances() {
        return instances;
    }

}

CloudStructure.java

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;

import cloud.CloudStructureData;
import cloud.Host;
import cloud.Instance;

public class CloudStructure {

    public static CloudStructureData readHost(String filePath)
    {
        String line = "";

        CloudStructureData cloudData = new CloudStructureData();

        FileReader fr;
        BufferedReader br;

        String[] HostID = null;
        String[] Slots = null;
        String[] DataCentre = null;

          try {
               fr = new FileReader(filePath);
               br = new BufferedReader(fr);

               while((line = br.readLine()) != null) 
               {   
                   HostID = line.split(",");
                   Slots = line.split(",");
                   DataCentre = line.split(",");                

                   Host host = new Host(HostID[0], Slots[1], DataCentre[2]);

                   cloudData.getHosts().put(HostID[0], host);
               }
              }
              catch(FileNotFoundException fN) {
               fN.printStackTrace();
              }
              catch(IOException e) {
               System.out.println(e);
              } 

         // output data the way you want...

         return cloudData;
    }   

    public static CloudStructureData readInstance(String filePath, CloudStructureData cloudData)
    {
        String line = "";

        FileReader fr;
        BufferedReader br;

        String[] InstanceId = null;
        String[] CustId = null;
        String[] HostId = null;

          try {
               fr = new FileReader(filePath);
               br = new BufferedReader(fr);

               while((line = br.readLine()) != null) 
               {   
                   InstanceId = line.split(",");
                   CustId = line.split(",");
                   HostId = line.split(",");                

                   Host hostObject = cloudData.getHosts().get(HostId[2]); // Host object or null if not found
                   Instance instanceObject = new Instance(InstanceId[0], CustId[1], hostObject);
                   cloudData.getInstances().put(InstanceId[0], instanceObject);
               }
              }
              catch(FileNotFoundException fN) {
               fN.printStackTrace();
              }
              catch(IOException e) {
               System.out.println(e);
              } 

              // output instances the way you want

              return cloudData;
    } 


    public static void main(String[] args) {

        CloudStructureData cloudData = readHost("C:\\tmp\\HostState.txt");
        cloudData = readInstance("C:\\tmp\\InstanceState.txt", cloudData);
        /*
         * All Instances are now linked with Host.  
         * You can loop and do whatever you want.
         */
        for (Map.Entry<String, Instance> currentEntry 
                : cloudData.getInstances().entrySet()) {
            Instance currentInstance = currentEntry.getValue();
            Host currentHost = currentInstance.getHost();
            // Now you have the current instance and its related host...
        }

    }
}

这是你需要的吗?