Java - write string to file line by line vs one-liner / cannot convert String to String[] -


relatively new programming. want read url, modify text string, write line-separated csv textfile.

the read & modify parts run. also, outputting string terminal (using eclipse) looks fine (csv, line line), this;

data_a,data_b,data_c,... data_a1,data_b1,datac1... data_a2,data_b2,datac2... . . . 

but i'm unable write same string file - becomes one-liner (see below for-loops, attempts no. 1 & 2);

data_a,data_b,data_c,data_a1,data_b1,datac1,data_a2,data_b2,datac2... 

i guess i'm looking way to, in filewriter or bufferedwriter loops, convert string finaldataa array string (i.e. include string suffix "[0]") have not yet found such approach not give errors of type "cannot convert string string[]". suggestions?

    string data = "";     string datahelper = "";     try {         url myurl = new url(url);         httpurlconnection myconnection = (httpurlconnection) myurl.openconnection();         if (myconnection.getresponsecode() == urlstatus.http_ok.getstatuscode()) {             bufferedreader in = new bufferedreader(new inputstreamreader(myconnection.getinputstream()));              while ((data = in.readline()) != null) {                      datahelper = datahelper + "\n" + data;             }             in.close();              string trimmeddata = datahelper.trim().replaceall(" +", ",");             string parts[] = trimmeddata.split(pattern.quote(")"));// ,1.,");             string dataa = parts[1];             string finaldataa[] = dataa.split("</pre>");             // parts 2&3 removed in example              // console output testing purpose - prints out many many lines of csv-data             system.out.println(finaldataa[0]);             //this returns value 1             system.out.println(finaldataa.length);              // attempt no. 1 write file - writes oneliner             for(int = 0; < finaldataa.length; i++) {                 try (bufferedwriter bw = new bufferedwriter(new filewriter(patha, true))) {                     string s;                     s = finaldataa[i];                     bw.write(s);                     bw.newline();                     bw.flush();                 }             }              // attempt no. 2 write file - writes oneliner             filewriter fw = new filewriter(patha);             (int = 0; < finaldataa.length; i++) {               fw.write(finaldataa[i] + "\n");             }             fw.close();          }     } catch (exception e) {         system.out.println("exception" +e); } 

from code comments, finaldataa has 1 element, for-loop executed once. try splitting finaldataa[0] rows. this:

    string endoflinetoken = "..."; //your variant     string[] lines = finaldataa[0].split(endoflinetoken)     bufferdwriter bw = new bufferedwriter(new filewriter(patha, true));     try      {        (string line: lines)        {            bw.write(line);            bw.write(endoflinetoken);//to put line endings            bw.newline();            bw.flush();        }     }     catch (exception e) {} 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -