Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
[Re-posted Renaming and deleting a text file in java, the available answer has been ignored — SA]

I have two text files namely - item.txt (file 1) and temp.txt (file 2). My goal is to search for a name in the file 1 and if found then replace it with a different name and write the updated line to file 2. Also, I have a method that checks for the lines for the string I searched in file 1. The lines that do not contain that string will be added to file 2.

So, here is where I'm stuck. Everything works fine except the part where I want to delete file 1 and rename file 2 by file 1 (i.e item.txt). Can someone please help me with any correction? I am still a beginner in Java, so my code might not be the best looking code as one might expect but this is what I tried so far. Thanks The problem is when i compile the code the updated data is written to file2 and file1 which was supposed to get deleted doesn't delete. So, what could be the problem?

Java
package project4;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class kitkat {

    PrintWriter out,in;
    Scanner in;
    Scanner temp;
    File file1 = new File("item.txt");
    File file2 = new File("temp.txt");

    public void write() throws FileNotFoundException {
        out = new PrintWriter(file1);

        out.println("User1"+ "\t"+"639755"+"\t"+"400");
        out.println("User2"+ "\t"+"639725"+"\t"+"800");
        out.close();        
    }

    public void nfile() throws IOException {
        n = new PrintWriter(new FileWriter(file2,true));
    }

    Scanner input = new Scanner(System.in);
    String replacement = "User3";
    String search;
    String total;

    public void search() {
        System.out.println("Enter your search name");
        search = input.nextLine();
        total = search; 
    }

    public void lolipop() throws IOException {
        in = new Scanner(file1);

        search();
        while(in.hasNext()) {
            String a,b,c;
            a = in.next();
            b = in.next();
            c = in.next();

            if(a.contains(search)) {
                System.out.println("Your match is found"+search);
                a = replacement;
                System.out.println(a+b+c);
                n.file();
                n.println(a+"\t"+b+"\t"+c);

                n.close();
            }
        }
    }

    public void jellybeans() throws IOException {
        temp = new Scanner(file1);
        while(temp.hasNext()) {
            String p,q,r;
            p = temp.next();
            q = temp.next();
            r = temp.next();
            if(!(p.contains(total))) {
                System.out.println(p+q+r);
                n.file();
                n.println(p+"\t"+q+"\t"+r);
                n.close();
                renamefile();
            }
        }
    }

   public void renamefile() {
       file1.delete();
       file2.renameTo(file1);
   }    
}


package project4;

import java.io.FileNotFoundException;
import java.io.IOException;

public class tuna {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        kitkat kt = new kitkat();
        kt.lolipop();
        kt.jellybeans();
    }
}
Posted
Updated 25-May-15 16:17pm
v3

1 solution

I already answered your previous question: Renaming and deleting a text file in java.

I precisely explained where your bug is; it is very apparent.

And what have you done? I did not bother to check if you modified your code or not. For me, it's enough to see that you reproduced exact same bug I pointed out in my answer. That said, you completely ignored my answer and did not try to fix you bug. Let's assume that you did not understand my answer. But you did not comment on it, did not ask any questions. Instead, you just ignored the whole thing and decided to re-post the same question with — notably — the same exact bug.

And this is the pure abuse as it is. Also, this is counterproductive. Who will want to help you if you are re-posting and ignoring the answers? All right, it's not too late to check yourself, but then you will need to do some effort.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900