Why file is being overwritten in the android external storage? -


i'm trying write student id's in text file in sdcard. have created file named students.txt in sdcard.

the problem when write value, , read file, last value in file. when open file can see last value written in it.

what doing wrong? i'm writing file in append mode using outputstreamwriter still problem remains same.

    //function insert private void myinsertfunc(string data) {     try {         file file = new file("/sdcard/" + filename);         fileoutputstream fileoutputstream = new fileoutputstream(file);         outputstreamwriter outputstreamwriter = new outputstreamwriter(fileoutputstream);         outputstreamwriter.append(data);          outputstreamwriter.close();         fileoutputstream.close();          toast.maketext(getapplicationcontext(), "student id: " + data + " inserted successfully!", toast.length_short).show();     }     catch (filenotfoundexception e) {         e.printstacktrace();     }     catch (ioexception e) {         e.printstacktrace();     } }  //function read private void myreadfunc() {     try {             //inputstream inputstream = openfileinput(filename);         //inputstream inputstream = getresources().openrawresource(r.raw.student);         file file = new file("/sdcard/" + filename);         fileinputstream fileinputstream = new fileinputstream(file);          if (fileinputstream != null) {             inputstreamreader inputstreamreader = new inputstreamreader(fileinputstream);             bufferedreader bufferedreader = new bufferedreader(inputstreamreader);             string receivestring = "";             while ((receivestring = bufferedreader.readline()) != null) {                 //if(receivestring.contains(data))                 //{                     toast.maketext(getapplicationcontext(), "student id: " + receivestring , toast.length_short).show();                     //break;                 //}             }              bufferedreader.close();             inputstreamreader.close();             fileinputstream.close();         }     }     catch (filenotfoundexception e) {         e.printstacktrace();     }     catch (ioexception e) {         e.printstacktrace();     } } 

i had same problem fileoutputstream , others. creating new file, when writing. filewriter solved me. should this:

private void myinsertfunc(string data) {     //opens andor creates file students.txt in folder /storage/emulated/0/android/data/<your package name>/files/     file file = new file(ctxt.getexternalfilesdir(null), "students.txt");     try {         //create filewriter , set append modus true         filewriter fw = new filewriter(file, true);         fw.append(data);         fw.close();      } catch (ioexception e) {         log.w("externalstorage", "error writing " + file, e);     } 

}

i hope helps you.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -