java - How to append multiple text in text file -


i want results 'name' , 'code' inserted log.txt file, if run program name results gets inserted .txt file, cannot see code results appending under name. if system.outprintln(name) & system.outprintln(code) results printed in console not being inserted in file.can tell me doing wrong?

            scanner sc = new scanner(file, "utf-8");             bufferedreader br = new bufferedreader(new filereader(file));             printwriter out = new printwriter(new filewriter("log.txt", true));              while ((line = br.readline()) != null) {             if (line.contains("text1")) {                 string[] splits = line.split("=");                 string name = splits[2];                 (int = 0; < name.length(); i++) {                     out.println(name);                  }             }              if (line.contains("text2")) {                 string[] splits = line.split("=");                 string code = splits[2];                 (int = 0; < code.length(); i++) {                     out.println(code);                  }             }          out.close()                       } 

file looks like:

name=111111111 code=333,5555 category-warranty  name=2222222 code=111,22 category-warranty 

have @ code. work you?

final string name = "name"; final string code = "code";  bufferedreader br = new bufferedreader(new filereader(file)); printwriter out = new printwriter(new filewriter("log.txt", true));  while ((line = br.readline()) != null) {     string[] splits = line.split("=");     string key = splits[0];     string value = splits[1];      if (key.equals(name) || key.equals(code)) {         out.println(value);     } }  out.close(); 

you have couple of problems in code:

  • you never assign variables name , code.
  • you close() printwriter inside while-loop, means have problem if read more 1 line.

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -