java - Picking specif data from .txt and writing as columns in csv -
this part of .txt:
thu mar 28 12:18:28 edt 2013
memfree: 601836 kb
linux 2.6.18-308.el5 (cwapp1.conceptwave.com) 03/28/2013
avg-cpu: %user %nice %system %iowait %steal %idle 0.15 0.00 0.05 0.02 0.00 99.79
========================
thu mar 28 12:18:38 edt 2013
memfree: 598312 kb
linux 2.6.18-308.el5 (cwapp1.conceptwave.com) 03/28/2013
avg-cpu: %user %nice %system %iowait %steal %idle 0.15 0.00 0.05 0.02 0.00 99.79
========================
i need read date, memory free , %idle, , write 2 csv files 2 columns. 1 date , memory, , other date , %idle. have written code reads data , append 3 lists. how write them 2 csv files such fulfill requirement?
thanks in advance.
my code:
list<string> datetime = new arraylist<string>(); list<string> memfree = new arraylist<string>(); list<string> pidle = new arraylist<string>(); public static void main(string[] args) { // todo auto-generated method stub } public void readfile(string file) { try { bufferedreader br = new bufferedreader(new filereader(file)); string str = null; try { while((str=br.readline()) != null) { string[] st = str.split("\\s+"); if (st[0] == "memfree:") memfree.add(st[1]); if(isdouble(st)) pidle.add(st[4]); if(isdate(str)) { string substr = str.substring(0, 15); datetime.add(substr); } } } { br.close(); } } catch(filenotfoundexception e) { system.out.println("file not found!"); } catch(ioexception e) { system.out.println(e); } } public boolean isdate(string str) { dateformat df = new simpledateformat("e m d"); try { string substr = str.substring(0, 8); @suppresswarnings("unused") date currentdate = df.parse(substr); return true; } catch(parseexception e) { return false; } } public boolean isdouble(string[] st) { try { (int = 0; < st.length;) { @suppresswarnings("unused") double num = double.parsedouble(st[i]); return true; } } catch(illegalargumentexception ex) { return false; } return false; } public void writetocsv(list<string> datetime, list<string> memfree, list<string> pidle) { //unfinished - need help! string datesepval = ""; if(datetime != null && memfree != null) { iterator<string> iter = datetime.iterator(); while(iter.hasnext()) { datesepval += iter.next() + ","; } if(datesepval.endswith(",")) { datesepval = datesepval.substring(0, datesepval.lastindexof(",")); } } if(datetime != null && pidle !=null) { //todo } }
writing file reading, other way around:
try (bufferedwriter writer = new bufferedwriter(new filewriter("delete.me"))) { writer.write("something"); writer.newline(); }
if want have new line each extracted date & memory value, this:
public void writetocsv(list<string> datetime, list<string> memfree) throws ioexception { try (bufferedwriter writer = new bufferedwriter(new filewriter("delete.me"))) { if(datetime == null || memfree == null) return; iterator<string> memfrees = memfree.iterator(); (string dt: datetimes) { if (!memfrees.hasnext()) return; writer.write(dt + "," + memfrees.next()); writer.newline(); } } }
but m sach suggested, may want @ framework doing things you.
Comments
Post a Comment