java - Writing multiple queries from a test file -


    public static void main(string[] args) {     arraylist<string> studenttokens = new arraylist<string>();     arraylist<string> studentids = new arraylist<string>();     try {         // open file first         // command line parameter         fileinputstream fstream = new fileinputstream(new file("file1.txt"));         bufferedreader br = new bufferedreader(new inputstreamreader(fstream, "utf8"));          string strline;         // read file line line         while ((strline = br.readline()) != null) {             strline = strline.trim();              if ((strline.length()!=0) && (!strline.contains("#"))) {                 string[] students = strline.split("\\s+");                 studenttokens.add(students[token_column]);                 studentids.add(students[student_id_column]);             }          }              (int i=0; i<studentids.size();i++) {             file file = new file("query.txt");                                                      // path of textfile converted csv upload             bufferedreader reader = new bufferedreader(new filereader(file));             string line = "", oldtext = "";             while ((line = reader.readline()) != null) {                                                                                  oldtext += line + "\r\n";             }             reader.close();             string newtext = oldtext.replace("sanid", studentids.get(i)).replace("salabel",studenttokens.get(i));                                           // here name "sanket" replaced current time stamp              filewriter writer = new filewriter("final.txt",true);             writer.write(newtext);             writer.close();         }           fstream.close();         br.close();          system.out.println("done!!");     } catch (exception e) {         e.printstacktrace();         system.err.println("error: " + e.getmessage());     }  } 

the above code of mine reads data text file , query file has query in 2 places "sanid" , "salabel" replaced content of string array , writes file final . when run code the final not have queries. while debugging shows values replaced properly.

but while debugging shows values replaced properly

if values found replaced when debugged code, missing in file, suggest flush output stream. closing filewriter without calling flush(). close() method delegates call underlying streamencoder not flush stream either.

public void close() throws ioexception { se.close(); } 

try this

writer.flush(); writer.close(); 

that should it.


Comments