1 year ago

#377339

test-img

kawaiison

update records of two columns by name in txt file java

I have txt file with employees It contains name, date, post, salary:

Asher 2022/04/05 HR-Manager 24000

I've changed my code, this one works fine (maybe would be helpfull for someone):

Scanner sc = new Scanner(System.in);
        System.out.println("•\tWrite name of employee to change salary and post");
        String name = sc.nextLine();
        System.out.println("•\tNew post:");
        String post = sc.nextLine();
        System.out.println("•\tNew salary:");
        String salary = sc.nextLine();
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(oldFileName));
            bw = new BufferedWriter(new FileWriter(tmpFileName));
            String line;
            while ((line = br.readLine()) != null) {
            String[] arr =line.split(" ");
                if (line.contains(name))
                    line = line.replace(arr[2], post);
                bw.write(line+"\n");
            }

java

crud

txt

0 Answers

Your Answer

Accepted video resources