how to update the contain in .txt file java

40,095

Solution 1

You have to use the approach of reading it from the original file line by line. Search and replace the salary with the new number. Write the content to a new temp file. Delete the original file. Finally replace the temp file to the original file name. Here is the code to do that:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class FileContentUpdater {

    public static void main(String args[]) throws IOException {
        String ID, Name;
        double salary;
        int replenish;

        Scanner console = new Scanner(System.in);
        System.out.print("Enter ID : ");
        String pID = console.nextLine();
        System.out.print("Enter replenish salary: ");
        replenish = console.nextInt();

        File originalFile = new File("file.txt");
        BufferedReader br = new BufferedReader(new FileReader(originalFile));

        // Construct the new file that will later be renamed to the original
        // filename.
        File tempFile = new File("tempfile.txt");
        PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

        String line = null;
        // Read from the original file and write to the new
        // unless content matches data to be removed.
        while ((line = br.readLine()) != null) {

            if (line.contains(pID)) {
                String strCurrentSalary = line.substring(line.lastIndexOf(" "), line.length());
                if (strCurrentSalary != null || !strCurrentSalary.trim().isEmpty()) {
                    int replenishedSalary = Integer.parseInt(strCurrentSalary.trim()) + replenish;
                    System.out.println("replenishedSalary : " + replenishedSalary);
                    line = line.substring(0,line.lastIndexOf(" ")) + replenishedSalary;
                }

            }
            pw.println(line);
            pw.flush();
        }
        pw.close();
        br.close();

        // Delete the original file
        if (!originalFile.delete()) {
            System.out.println("Could not delete file");
            return;
        }

        // Rename the new file to the filename the original file had.
        if (!tempFile.renameTo(originalFile))
            System.out.println("Could not rename file");

    }
}

Solution 2

One possible way can be like this

  1. Read each line from file
  2. Split the line

  3. prepare another line with updated data and write to temp file

  4. Finally delete your old file and rename temp file to old file.

  5. Now you are done. Try the above algorithm.
Share:
40,095
surisava
Author by

surisava

Updated on May 06, 2020

Comments

  • surisava
    surisava about 4 years

    for example i have the file named file.txt which contain personal information (id,name, salary):

    A123 Anna 3000
    A234 Alex 4000
    A986 Jame 5000
    

    How can I write a java code that allow user to enter an ID of a person and replenish salary? The final output will look like:

    Enter ID: A123

    Enter replenish salary: 2000

    file.txt after run the program:

    A123 Anna 5000
    A234 Alex 4000
    A986 Jame 5000
    

    This is what i have done so far but it didnt work:

    public static void addDalary() throws IOException {
            String ID, Nanme;
            double salary;
    
    
                Scanner console = new Scanner(System.in);
                System.out.print("Enter ID : ");
                String pID = console.nextLine();
                System.out.print("Enter replenish salary: ");
                replenish = console.nextInt();
    
                Scanner input = new Scanner(new File("file.txt"));
                while (input.hasNext()) {
                    ID = input.next();
                    Name = input.next();
                    salary = input.nextDouble();
    
                    if (ID == pID) {    
                        salary = salary + replenish;
                    }
                }
                input.close();
            }
    

    Im new to these things. Any help would be much appreciate.