Read file and update some data from text file in java? [duplicate] The Next CEO of Stack OverflowModifying existing file content in JavaHow do I call one constructor from another in Java?How do I read / convert an InputStream into a String in Java?How do I create a Java string from the contents of a file?How to get an enum value from a string value in Java?How to append text to an existing file in JavaHow to read all files in a folder from Java?How do I create a file and write to it in Java?How to avoid Java code in JSP files?Reading a plain text file in JavaHow to read a large text file line by line using Java?

How easy is it to start Magic from scratch?

What makes a siege story/plot interesting?

How do scammers retract money, while you can’t?

How do we know the LHC results are robust?

Why were Madagascar and New Zealand discovered so late?

Where to find order of arguments for default functions

Science fiction (dystopian) short story set after WWIII

India just shot down a satellite from the ground. At what altitude range is the resulting debris field?

Anatomically Correct Mesopelagic Aves

How to use tikz in fbox?

Rotate a column

Trouble understanding the speech of overseas colleagues

How can I get through very long and very dry, but also very useful technical documents when learning a new tool?

How to make a software documentation "officially" citable?

When airplanes disconnect from a tanker during air to air refueling, why do they bank so sharply to the right?

Return the Closest Prime Number

Why did we only see the N-1 starfighters in one film?

Customer Requests (Sometimes) Drive Me Bonkers!

What can we do to stop prior company from asking us questions?

Does it take more energy to get to Venus or to Mars?

Is it safe to use c_str() on a temporary string?

How to Reset Passwords on Multiple Websites Easily?

What is the purpose of the Evocation wizard's Potent Cantrip feature?

Does the Brexit deal have to be agreed by both Houses?



Read file and update some data from text file in java? [duplicate]



The Next CEO of Stack OverflowModifying existing file content in JavaHow do I call one constructor from another in Java?How do I read / convert an InputStream into a String in Java?How do I create a Java string from the contents of a file?How to get an enum value from a string value in Java?How to append text to an existing file in JavaHow to read all files in a folder from Java?How do I create a file and write to it in Java?How to avoid Java code in JSP files?Reading a plain text file in JavaHow to read a large text file line by line using Java?










1
















This question already has an answer here:



  • Modifying existing file content in Java

    4 answers



I m working cucumber with java automation framework , there one directory has one text file which I coded like (Every time it is renaming ) , I want to pick up that new renaming file and after opening that file I want to update some particular data and save it for further code.



Can anyone help me how can I open text file , How can I update and close it again,
want to update particular data (see example below)(There has Name column and Value in second raw , want to update Name data every time with any random string)
Text file data :



Name | DOB | Gender |
Komal | 5-6-1992 | Female










share|improve this question













marked as duplicate by Roddy of the Frozen Peas, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 22 at 9:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















    1
















    This question already has an answer here:



    • Modifying existing file content in Java

      4 answers



    I m working cucumber with java automation framework , there one directory has one text file which I coded like (Every time it is renaming ) , I want to pick up that new renaming file and after opening that file I want to update some particular data and save it for further code.



    Can anyone help me how can I open text file , How can I update and close it again,
    want to update particular data (see example below)(There has Name column and Value in second raw , want to update Name data every time with any random string)
    Text file data :



    Name | DOB | Gender |
    Komal | 5-6-1992 | Female










    share|improve this question













    marked as duplicate by Roddy of the Frozen Peas, Mark Rotteveel java
    Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Mar 22 at 9:32


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      1












      1








      1









      This question already has an answer here:



      • Modifying existing file content in Java

        4 answers



      I m working cucumber with java automation framework , there one directory has one text file which I coded like (Every time it is renaming ) , I want to pick up that new renaming file and after opening that file I want to update some particular data and save it for further code.



      Can anyone help me how can I open text file , How can I update and close it again,
      want to update particular data (see example below)(There has Name column and Value in second raw , want to update Name data every time with any random string)
      Text file data :



      Name | DOB | Gender |
      Komal | 5-6-1992 | Female










      share|improve this question















      This question already has an answer here:



      • Modifying existing file content in Java

        4 answers



      I m working cucumber with java automation framework , there one directory has one text file which I coded like (Every time it is renaming ) , I want to pick up that new renaming file and after opening that file I want to update some particular data and save it for further code.



      Can anyone help me how can I open text file , How can I update and close it again,
      want to update particular data (see example below)(There has Name column and Value in second raw , want to update Name data every time with any random string)
      Text file data :



      Name | DOB | Gender |
      Komal | 5-6-1992 | Female





      This question already has an answer here:



      • Modifying existing file content in Java

        4 answers







      java






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 21 at 16:36









      kavyakavya

      63




      63




      marked as duplicate by Roddy of the Frozen Peas, Mark Rotteveel java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Mar 22 at 9:32


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Roddy of the Frozen Peas, Mark Rotteveel java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Mar 22 at 9:32


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You can't update existing file using Java.



          1. You have to read, modify and write to a new file.

          2. Delete the old file

          3. Rename the new file with old file name.





          share|improve this answer























          • Thanks steephen

            – kavya
            Mar 21 at 17:31











          • Can You provide code for same? as I want to rename 4th Column data (Komal) everytime with random string?

            – kavya
            Mar 21 at 17:32


















          0














          Below program would replace 4th Column data (Komal) with desired string and We need to pass directory name which would have 1 txt file.



          public class MyApplication 

          public static void main(String[] args) throws Exception
          getTxtFileWithInADirectory("D:\Learning\newyork");


          static void getTxtFileWithInADirectory(String directoryPath)
          String fileExtension;
          File folder = new File(directoryPath);
          File[] listOfFiles = folder.listFiles();
          for (int i = 0; i < listOfFiles.length; i++)
          if (listOfFiles[i].isFile())
          fileExtension = listOfFiles[i].getName().substring(listOfFiles[i].getName().lastIndexOf("."), listOfFiles[i].getName().length());
          if(fileExtension.contains(".txt"))
          updateTXTFile(listOfFiles[i].getAbsolutePath(),"Poonam");

          else
          System.out.println(".txt file not found");




          static void updateTXTFile(String fileToBeModified, String newText)
          String oldContent = "";
          BufferedReader reader = null;
          FileWriter writer = null;
          try
          ");
          String newContent = oldContent.replace(items[3], " "+newText+" ");
          writer = new FileWriter(fileToBeModified);
          writer.write(newContent);
          catch (Exception e)

          e.printStackTrace();

          finally

          try

          //Closing the resources
          reader.close();
          writer.close();

          catch (Exception e)

          e.printStackTrace();









          share|improve this answer





























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You can't update existing file using Java.



            1. You have to read, modify and write to a new file.

            2. Delete the old file

            3. Rename the new file with old file name.





            share|improve this answer























            • Thanks steephen

              – kavya
              Mar 21 at 17:31











            • Can You provide code for same? as I want to rename 4th Column data (Komal) everytime with random string?

              – kavya
              Mar 21 at 17:32















            1














            You can't update existing file using Java.



            1. You have to read, modify and write to a new file.

            2. Delete the old file

            3. Rename the new file with old file name.





            share|improve this answer























            • Thanks steephen

              – kavya
              Mar 21 at 17:31











            • Can You provide code for same? as I want to rename 4th Column data (Komal) everytime with random string?

              – kavya
              Mar 21 at 17:32













            1












            1








            1







            You can't update existing file using Java.



            1. You have to read, modify and write to a new file.

            2. Delete the old file

            3. Rename the new file with old file name.





            share|improve this answer













            You can't update existing file using Java.



            1. You have to read, modify and write to a new file.

            2. Delete the old file

            3. Rename the new file with old file name.






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 21 at 17:14









            SteephenSteephen

            8,04642335




            8,04642335












            • Thanks steephen

              – kavya
              Mar 21 at 17:31











            • Can You provide code for same? as I want to rename 4th Column data (Komal) everytime with random string?

              – kavya
              Mar 21 at 17:32

















            • Thanks steephen

              – kavya
              Mar 21 at 17:31











            • Can You provide code for same? as I want to rename 4th Column data (Komal) everytime with random string?

              – kavya
              Mar 21 at 17:32
















            Thanks steephen

            – kavya
            Mar 21 at 17:31





            Thanks steephen

            – kavya
            Mar 21 at 17:31













            Can You provide code for same? as I want to rename 4th Column data (Komal) everytime with random string?

            – kavya
            Mar 21 at 17:32





            Can You provide code for same? as I want to rename 4th Column data (Komal) everytime with random string?

            – kavya
            Mar 21 at 17:32













            0














            Below program would replace 4th Column data (Komal) with desired string and We need to pass directory name which would have 1 txt file.



            public class MyApplication 

            public static void main(String[] args) throws Exception
            getTxtFileWithInADirectory("D:\Learning\newyork");


            static void getTxtFileWithInADirectory(String directoryPath)
            String fileExtension;
            File folder = new File(directoryPath);
            File[] listOfFiles = folder.listFiles();
            for (int i = 0; i < listOfFiles.length; i++)
            if (listOfFiles[i].isFile())
            fileExtension = listOfFiles[i].getName().substring(listOfFiles[i].getName().lastIndexOf("."), listOfFiles[i].getName().length());
            if(fileExtension.contains(".txt"))
            updateTXTFile(listOfFiles[i].getAbsolutePath(),"Poonam");

            else
            System.out.println(".txt file not found");




            static void updateTXTFile(String fileToBeModified, String newText)
            String oldContent = "";
            BufferedReader reader = null;
            FileWriter writer = null;
            try
            ");
            String newContent = oldContent.replace(items[3], " "+newText+" ");
            writer = new FileWriter(fileToBeModified);
            writer.write(newContent);
            catch (Exception e)

            e.printStackTrace();

            finally

            try

            //Closing the resources
            reader.close();
            writer.close();

            catch (Exception e)

            e.printStackTrace();









            share|improve this answer



























              0














              Below program would replace 4th Column data (Komal) with desired string and We need to pass directory name which would have 1 txt file.



              public class MyApplication 

              public static void main(String[] args) throws Exception
              getTxtFileWithInADirectory("D:\Learning\newyork");


              static void getTxtFileWithInADirectory(String directoryPath)
              String fileExtension;
              File folder = new File(directoryPath);
              File[] listOfFiles = folder.listFiles();
              for (int i = 0; i < listOfFiles.length; i++)
              if (listOfFiles[i].isFile())
              fileExtension = listOfFiles[i].getName().substring(listOfFiles[i].getName().lastIndexOf("."), listOfFiles[i].getName().length());
              if(fileExtension.contains(".txt"))
              updateTXTFile(listOfFiles[i].getAbsolutePath(),"Poonam");

              else
              System.out.println(".txt file not found");




              static void updateTXTFile(String fileToBeModified, String newText)
              String oldContent = "";
              BufferedReader reader = null;
              FileWriter writer = null;
              try
              ");
              String newContent = oldContent.replace(items[3], " "+newText+" ");
              writer = new FileWriter(fileToBeModified);
              writer.write(newContent);
              catch (Exception e)

              e.printStackTrace();

              finally

              try

              //Closing the resources
              reader.close();
              writer.close();

              catch (Exception e)

              e.printStackTrace();









              share|improve this answer

























                0












                0








                0







                Below program would replace 4th Column data (Komal) with desired string and We need to pass directory name which would have 1 txt file.



                public class MyApplication 

                public static void main(String[] args) throws Exception
                getTxtFileWithInADirectory("D:\Learning\newyork");


                static void getTxtFileWithInADirectory(String directoryPath)
                String fileExtension;
                File folder = new File(directoryPath);
                File[] listOfFiles = folder.listFiles();
                for (int i = 0; i < listOfFiles.length; i++)
                if (listOfFiles[i].isFile())
                fileExtension = listOfFiles[i].getName().substring(listOfFiles[i].getName().lastIndexOf("."), listOfFiles[i].getName().length());
                if(fileExtension.contains(".txt"))
                updateTXTFile(listOfFiles[i].getAbsolutePath(),"Poonam");

                else
                System.out.println(".txt file not found");




                static void updateTXTFile(String fileToBeModified, String newText)
                String oldContent = "";
                BufferedReader reader = null;
                FileWriter writer = null;
                try
                ");
                String newContent = oldContent.replace(items[3], " "+newText+" ");
                writer = new FileWriter(fileToBeModified);
                writer.write(newContent);
                catch (Exception e)

                e.printStackTrace();

                finally

                try

                //Closing the resources
                reader.close();
                writer.close();

                catch (Exception e)

                e.printStackTrace();









                share|improve this answer













                Below program would replace 4th Column data (Komal) with desired string and We need to pass directory name which would have 1 txt file.



                public class MyApplication 

                public static void main(String[] args) throws Exception
                getTxtFileWithInADirectory("D:\Learning\newyork");


                static void getTxtFileWithInADirectory(String directoryPath)
                String fileExtension;
                File folder = new File(directoryPath);
                File[] listOfFiles = folder.listFiles();
                for (int i = 0; i < listOfFiles.length; i++)
                if (listOfFiles[i].isFile())
                fileExtension = listOfFiles[i].getName().substring(listOfFiles[i].getName().lastIndexOf("."), listOfFiles[i].getName().length());
                if(fileExtension.contains(".txt"))
                updateTXTFile(listOfFiles[i].getAbsolutePath(),"Poonam");

                else
                System.out.println(".txt file not found");




                static void updateTXTFile(String fileToBeModified, String newText)
                String oldContent = "";
                BufferedReader reader = null;
                FileWriter writer = null;
                try
                ");
                String newContent = oldContent.replace(items[3], " "+newText+" ");
                writer = new FileWriter(fileToBeModified);
                writer.write(newContent);
                catch (Exception e)

                e.printStackTrace();

                finally

                try

                //Closing the resources
                reader.close();
                writer.close();

                catch (Exception e)

                e.printStackTrace();










                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 21 at 17:55









                TheSocietyTheSociety

                437110




                437110













                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript