How to append text into text file dynamically

Posted by niraj deshmukh on Stack Overflow See other posts from Stack Overflow or by niraj deshmukh
Published on 2013-10-28T09:33:44Z Indexed on 2013/10/28 9:53 UTC
Read the original article Hit count: 251

Filed under:
|
[12]
key1=val1
key2=val2
key3=val3
key4=val4
key5=val5
[13]
key1=val1
key2=val2
key3=val3
key4=val4
key5=xyz
[14]
key1=val1
key2=val2
key3=val3
key4=val4
key5=val5

I want to update key5=val5 where [13].

 try {
         br = new BufferedReader(new FileReader(oldFileName));
         bw = new BufferedWriter(new FileWriter(tmpFileName));
         String line;
         while ((line = br.readLine()) != null) {
             System.out.println(line);

            if (line.contains("[13]"))
            {
                while (line.contains("key5")) {
                      if (line.contains("key5"))
                      {
                           line = line.replace("key5", "key5= Val5");
                           bw.write(line+"\n");
                      }

                }

            }

         }
      } catch (Exception e) {
         return;
      } finally {
         try {
            if(br != null)
               br.close();
         } catch (IOException e) {
            //
         }
         try {
            if(bw != null)
               bw.close();
         } catch (IOException e) {
            //
         }
      }

© Stack Overflow or respective owner

Related posts about java

Related posts about file-io