1

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

kavya
  • 15
  • 1
  • 5

2 Answers2

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.
Steephen
  • 14,645
  • 7
  • 40
  • 47
  • Can You provide code for same? as I want to rename 4th Column data (Komal) everytime with random string? – kavya Mar 21 '19 at 17:32
1

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
        {
            reader = new BufferedReader(new FileReader(fileToBeModified));

            //Reading all the lines of input text file into oldContent

            String line = reader.readLine();

            while (line != null) 
            {
                oldContent = oldContent + line + System.lineSeparator();

                line = reader.readLine();
            }

            String[] items = oldContent.split("\\|");
            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();
            }
        }
    }
}
TheSociety
  • 1,936
  • 2
  • 8
  • 20