java - Save ArrayList<String> to text file -


longlat files:

101.2425101.2334103.345  

the coding have algorithm this:

arraylist<string> getdifferencelist(string filepath) {     file f = new file("longlat.txt");      string line, x1 = null, x2= null, x3 = null;  bufferedreader buffreader = null;  try {     buffreader = new bufferedreader(new filereader(f));     while (( line = buffreader.readline()) != null) {         string[] parts = line.split("");         x1 = (parts[0]);            x2 = (parts[1]);           x3 = (parts[2]);      } } catch (filenotfoundexception e) {     e.printstacktrace(); } catch (ioexception e) {     e.printstacktrace(); }  arraylist<string> ret = new arraylist<string>(); filewriter writer;     if (x1 == null || x1.isempty()         || x2 == null || x2.isempty()         || x3 == null || x3.isempty()) {      ret.add(x1);     ret.add(x2);     ret.add(x3);      return ret; }  int index = 0;  while (index < x1.length()         && index < x2.length()         && index < x3.length()) {     if (x1.charat(index) != x2.charat(index)             || x1.charat(index) != x3.charat(index)) {         break;     }     index++; }  ret.add(x1.substring(index, x1.length())); ret.add(x2.substring(index, x2.length())); ret.add(x3.substring(index, x3.length()));   return ret;   } 

the values need stored text file inside arraylist of ret.

i've tried :

filewriter writer = new filewriter("lalala.txt");      for(string str: ret) {         writer.write(str);                } writer.close(); 

i put above coding @ top of :

ret.add(x1.substring(index, x1.length())); 

problem : nothing shown when click button "show lalala text".

and tried :

 try {       outputstreamwriter out = new outputstreamwriter(openfileoutput ("lalala.txt",mode_append));                      string text =(ret);               out.write(text);               out.write('\n');                            out.close();                     }          catch (java.io.ioexception e) {         toast.maketext(this,"sorry text could't added",toast.length_long).show ();}                                    } 

i put above coding @ top of :

ret.add(x1.substring(index, x1.length())); 

problem : error @ 'ret'. said "type mismatch: cannot convert arraylist string"

i don't know how take arraylist , stored them text file. please give me ideas, thanks.

arraylist<string> cannot cast string.

you can write content of arraylist file in way:

printwriter out = null; try {     out = new printwriter(new filewriter("lalala.txt"));     (string text : ret) {         out.writeln(text);     }              } catch (ioexception e) {     system.err.println("caught ioexception: " +  e.getmessage());  } {     if (out != null) {         out.close();     } } 

Comments