java - trying to write to read and write to a text file in eclipse -


i trying write , read text file name students having kinds of hassles new android programming trying out first time. have looked @ code here , there try , figure out doing wrong cant find 1 specific thing help, question has been asked couple of times, sorry asking again. please see different .xml , .java files below. actual question able write data textfile , main screen click on textfield take edit screen, edit specific field , save text file (this has not been done yet still struggling figure out why writing , reading textfile not working, hope poor attempt @ coding shed light on matter. please don't crucify me bad coding super new android

/////////////////////////////add screen.java///////////////////////////////  public class addnew extends activity { private static final string newline = system.getproperty("line.separator"); textview txttext;  edittext modules; edittext types;  @override protected void oncreate(bundle savedinstancestate){     super.oncreate(savedinstancestate);     setcontentview(r.layout.add);      txttext = (textview)findviewbyid(r.id.textview1);     modules = (edittext)findviewbyid(r.id.etmod);     types = (edittext)findviewbyid(r.id.ettype);  button backman = (button)findviewbyid(r.id.btnbackmain); backman.setonclicklistener(new onclicklistener(){   public void onclick(view v){   //this code go       startactivity(new intent(addnew.this, mainactivity.class));   } });  //end button  //get day, month & year date picker datepicker mydpicker = (datepicker)findviewbyid(r.id.dpdate); integer year = mydpicker.getyear(); integer month = mydpicker.getmonth(); integer day = mydpicker.getdayofmonth(); stringbuilder sb = new stringbuilder(); sb.append(year.tostring()).append("-").append(month.tostring()).append ("-").append(day.tostring()); final string dobstr=sb.tostring();  txttext.settext("test");  button save = (button)findviewbyid(r.id.btnsaveadded); save.setonclicklistener(new onclicklistener(){ public void onclick(view v){  //this code go        try {     writetofile(modules.gettext().tostring(),     types.gettext().tostring(),dobstr);  } catch (ioexception e) {     // todo auto-generated catch block     e.printstacktrace(); }      }  private void writetofile(string mod, string astype, string dobdate) throws  ioexception {     // todo auto-generated method stub     //string texttofile;     stringbuilder sbtext = new stringbuilder();     sbtext.append(mod + "," + dobstr + "," + astype);     //texttofile=sbtext.tostring();           string filename = "student";         printwriter printwriter = null;         file file = new file(filename);         try {             if (!file.exists()) file.createnewfile();             printwriter = new printwriter(new fileoutputstream(filename,          true));             printwriter.write(newline ); //+texttofile);         } catch (ioexception ioex) {             ioex.printstacktrace();         } {             if (printwriter != null) {                 printwriter.flush();                 printwriter.close();         }     }                } 

}); //end button

} 

}

`public class mainactivity extends activity { textview fdisplay; textview ftest; int numitems=0; //use later keep track of number of items. string intext; //use variable information read in textfile.

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      button but1=(button)findviewbyid(r.id.btnadd);     but1.setonclicklistener(new onclicklistener(){        public void onclick(view v){       //this code go           startactivity(new intent(mainactivity.this, addnew.class));       }     });  //end but1      button but2 = (button)findviewbyid(r.id.btneditcur);     but2.setonclicklistener(new onclicklistener(){        public void onclick(view v){       //this code go           startactivity(new intent(mainactivity.this, editcur.class));       }     });  //end of button 2      fdisplay = (textview)findviewbyid(r.id.tvassign1);        try {     readfromfile();     } catch (filenotfoundexception e) {         // todo auto-generated catch block         e.printstacktrace();     } catch (ioexception e) {         // todo auto-generated catch block         e.printstacktrace();     }   }  private void readfromfile() throws ioexception {      // todo auto-generated method stub     // string ret="";     bufferedreader br;     filereader fr = null;     try {         fr = new filereader("student");         br = new bufferedreader(fr);         string line = br.readline();         while (null != line) {             fdisplay.append(line);             fdisplay.append("\n");             line = br.readline();         }     } catch (ioexception e) {         // todo auto-generated catch block         e.printstacktrace();     } {         if (null != fr) {             try {                 fr.close();             } catch (ioexception e) {                 // ignore             }         }     }        } 

} `

for writting on file have used this

string filename; string content;  filename = "path_and_file"; content = "content on file"  bufferedwriter out = new bufferedwriter(new filewriter(filename));                             out.write(mystring.tostring());                               out.flush();                               out.close();  

and reading have function:

public static string readfileasstring() {     string result = ""; string filename;  filename = "path_and_file";     file file = new file(filename);     if ( file.exists() ) {         fileinputstream fis = null;         try {                   fis = new fileinputstream(file);             char current;             while (fis.available() > 0) {                 current = (char) fis.read();                 result = result + string.valueof(current);             }         } catch (exception e) {             // system.out.println("debug exception string :"+ e.tostring());         } {             if (fis != null)             { try {                 fis.close();             } catch (ioexception ignored) {             }}             else {// system.out.println("debug exception string null");             }         }         return result;     }     else     {         return "default content";     } } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -