java - save string list to a file since you compare it -
i saving file double list (mydata) data user enters , string list (dates_strings) current date.
the user enters data , pressing 'save' button , save data , currents date.
so , user may enter "1" , press save (1, 08/05/13)
enter "2" , press save (2, 08/05/13).
because user may enter data during day (same date) don't want save many instances of date.i want save user data in date.
i tried sth like:
(int i=1;i<mydata.size();i++){ bw.write(mydata.get(i)+","); while (!(dates_strings.get(i).equals(dates_strings.get(i-1)))) bw.write(dates_strings.get(i)+"\n"); }
but saves last entered data.
i saving as:
file sdcard = environment.getexternalstoragedirectory(); file directory = new file (sdcard, "myfiles"); directory.mkdirs(); file file = new file(directory, filename); fileoutputstream fos; //saving them try { fos = new fileoutputstream(file,true); //true in order append bufferedwriter bw = new bufferedwriter(new outputstreamwriter(fos)); (int i=1;i<mydata.size();i++){ //if (!(dates_strings.get(i).equals(dates_strings.get(i-1)))) bw.write(mydata.get(i)+","+dates_strings.get(i)+"\n"); } value.settext(""); bw.flush(); bw.close(); } catch (ioexception e2) { e2.printstacktrace(); }//catch }
i loading as:
file sdcard = environment.getexternalstoragedirectory(); file directory = new file (sdcard, "myfiles"); file file = new file(directory, filename); string s; fileinputstream fis; try { fis = new fileinputstream(file); bufferedreader br = new bufferedreader(new inputstreamreader(fis)); { s = br.readline(); if (s != null ){ string[] splitline = s.split(","); mydata.add(double.parsedouble(splitline[0])); //dates_strings.add(thedate.parse(splitline[1])); dates_strings.add(splitline[1]); } } while (s != null ); br.close(); } catch (ioexception e) { e.printstacktrace(); } }
mmmm... maybe can you, basic idea mentioned our colleagues: receive input, save in file, receive new input, read existing file before, add new content old content , save updated content of file.
//asumming values these: list<string> dateslist = new arraylist<string>(); list<double> datalist = new arraylist<double>(); //you must fill data of course... //i use buffer put in order data stringbuffer stringappender = new stringbuffer(); (int = 0; < datalist.size(); i++) { stringappender.append(datalist.get(i)); stringappender.append(","); stringappender.append(dateslist.get(i)); if (i != datalist.size()-1) { stringappender.append("\n"); } } //i use buffered writer , save data ordered in 1 single string. bufferedwriter bw = new bufferedwriter(new filewriter(new file("/home/mtataje/saved.txt"))); bw.write(stringappender.tostring()); bw.close();
then... have new inputs right?
//i read file first bufferedreader br = new bufferedreader(new filereader(new file("/home/mtataje/saved.txt"))); string line; stringbuffer auxbuffer = new stringbuffer(); while ((line = br.readline()) != null) { auxbuffer.append(line); auxbuffer.append("\n"); } //then append stringbuffer again, stringbuffer has data saved inside :) (int = 0; < newdatalistincoming.size(); i++) { auxbuffer.append(newdatalistincoming.get(i)); auxbuffer.append(","); auxbuffer.append(newdatesincoming.get(i)); if (i != newdatalistincoming.size()-1) { auxbuffer.append("\n"); } } //and write file bufferedwriter bw2 = new bufferedwriter(new filewriter(new file("/home/mtataje/saved.txt"))); bw2.write(auxbuffer.tostring()); bw2.close();
of course, use methods , not use redundancy in code me, hope gave hand this. best regards.
Comments
Post a Comment