java - How to read data from two text files and linking the data? -
i want read data host.txt , instance.txt, able read data i'm not sure how can linked!
here host file:
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
here instance file:
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
here code using read files:
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()); } }
my other class:
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>(); } // statements host public arraylist<string> gethostid(){ return shostid; } public arraylist<string> getslot(){ return sslot; } public arraylist<string> getcentre(){ return scentre; } // 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; } }
here output of program:
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]
the hoststate.txt input lists different hosts , location. instancestate.txt input lists running instances , on host running.
i suggest create 2 classes 1 host , 1 instance. instance class have reference host object. can use built-in map facilitate search of hosts , instances. following changes code illustrate above idea. tried keep consistent possible original code although not agree design. anyhow, below should give idea of how achieve requirement:
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 , setters follow... }
instance.java
package cloud; public class instance { private string instanceid; private string customer; private host host; // reference related host object public instance(string instanceid, string customer, host host) { this.instanceid = instanceid; this.customer = customer; this.host = host; } // valid getters , 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>(); // key hostid instances = new hashmap<string, instance>(); // key 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 way 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 way want return clouddata; } public static void main(string[] args) { cloudstructuredata clouddata = readhost("c:\\tmp\\hoststate.txt"); clouddata = readinstance("c:\\tmp\\instancestate.txt", clouddata); /* * instances linked host. * can loop , whatever want. */ (map.entry<string, instance> currententry : clouddata.getinstances().entryset()) { instance currentinstance = currententry.getvalue(); host currenthost = currentinstance.gethost(); // have current instance , related host... } } }
is need?
Comments
Post a Comment