Android append file can't pass unit test -


i trying use android append on txt file. append method can't passed unit test.

example:

i tried append "123456" twice "123456123456", got error:

junit.framework.comparisonfailure: expected:<123456[]> was:<123456[123456]>   

my question what's meaning of <> [] in <123456[]> , <123456[123456]>?

my test method below, can passed first assertequal

 public void appendfilecorrect() throws exception {             string contenttowrite = "123456";             context appcontext = instrumentationregistry.gettargetcontext();             filemanager manager = new filemanager("jim11.txt");             manager.append(contenttowrite);             string file = manager.readfromfile(appcontext);             assertequals(file, contenttowrite);             manager.append(contenttowrite);             string appendfile = manager.readfromfile(appcontext);             assertequals(appendfile, contenttowrite+contenttowrite);         } 

the method append end of file:

public string append(string content) throws ioexception {          try {             fileoutputstream fout = new fileoutputstream(filepath);             outputstreamwriter myoutwriter = new outputstreamwriter(fout);             myoutwriter.append(content);             myoutwriter.close();             fout.close();         } catch (exception e) {             e.printstacktrace();         }         return filename;     } 

the method read

   public string readfromfile(context context) throws ioexception {          string ret = "";         try {             fileinputstream inputstream = context.openfileinput(filename);             if (inputstream != null) {                 inputstreamreader inputstreamreader = new inputstreamreader(inputstream);                 bufferedreader bufferedreader = new bufferedreader(inputstreamreader);                 stringbuilder stringbuilder = new stringbuilder();                 string receivestring = "";                 while ( (receivestring = bufferedreader.readline()) != null ) {                     stringbuilder.append(receivestring);                 }                 inputstream.close();                 bufferedreader.close();                 ret = stringbuilder.tostring();             }         } catch (filenotfoundexception e) {             e.printstacktrace();         }         return ret;     } 

also android device has no sd card, way can write public place can read file directly? right path "data/data/files/.."

change

 fileoutputstream fout = new fileoutputstream(filepath); 

to

fileoutputstream fout = new fileoutputstream(filepath, true); 

the second parameter means append or not.

another way append is:

outputstream = context.openfileoutput(filename, context.mode_append);

context.mode_append mode check file exist or not. if not create file , append on end.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -