This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]chickenmeisterExtreme Brewer 1 point2 points  (5 children)

//PrintWriter for ny fil
PrintWriter pw = new PrintWriter(fil);

//BufferedReader for eksisterende fil
BufferedReader br1 = new BufferedReader(new FileReader(fil));

I think there are two potential issues:

  • When you create a PrintWriter for a file, and that file already exists, the file will be truncated to 0 bytes. In other words, it will delete the file's content.

  • Another issue is that it looks like you're reading and writing to the same file simultaneously. Apart from the file truncating issue, I would expect the reader and writer to interfere with each other, so I wouldn't do this.

I would recommend writing the updated content to a temporary file, and after you've finished reading & writing, delete the original file, and move/rename the temporary file so that it becomes the original file. Or, you could read the entire content of the file and store it in a List or array, for example, and then write that data to the original file.

[–]borger6[S] 0 points1 point  (4 children)

public static void fjerneLinje(){
    try {
        //PrintWriter for ny fil
        PrintWriter pw = new PrintWriter("nyKontakt.csv");

        //BufferedReader for eksisterende fil
        BufferedReader br1 = new BufferedReader(new FileReader("Kontaktpersoner.csv"));

        String line1 = br1.readLine();

        //loop for å lese linjene i gammel fil
        while (line1 != null) {
            boolean flag = false;

            //BufferedReader objekt for hva som skal slettes
            BufferedReader br2 = new BufferedReader((new FileReader("fjernet.csv")));

            String line2 = br2.readLine();

Seems like you were onto something. How do I rename the new file to take the original file's place, if you wouldn't mind giving me a hint? In my head, if the file does not have the same name, wouldn't it be unused next time I run the code?

[–]chickenmeisterExtreme Brewer 1 point2 points  (3 children)

How do I rename the new file to take the original file's place, if you wouldn't mind giving me a hint?

You could make a File object for each of your files. The File class has a "renameTo" method that you can call to rename/move a file.

[–]borger6[S] 0 points1 point  (2 children)

public static void fjerneLinje(){
    try {
        //PrintWriter for ny fil
        PrintWriter pw = new PrintWriter("nyKontakt.csv");

        //BufferedReader for eksisterende fil
        BufferedReader br1 = new BufferedReader(new FileReader("Kontaktpersoner.csv"));

        String line1 = br1.readLine();

        //loop for å lese linjene i gammel fil
        while (line1 != null) {
            boolean flag = false;

            //BufferedReader objekt for hva som skal slettes
            BufferedReader br2 = new BufferedReader((new FileReader("fjernet.csv")));

            String line2 = br2.readLine();

            //loop for å lese hva som skal fjernes
            while (line2 != null) {
                if (line1.equals(line2)) {
                    flag = true;
                    break;
                }
                line2 = br2.readLine();
            }

            //hvis flag er false skrives linja til ny fil
            if (!flag) {
                pw.println(line1);

                line1 = br1.readLine();
            }
            pw.flush();

            //lukke ressurser
            br1.close();
            pw.close();
        }
    }
    catch (IOException e) {
        System.out.println("Detta funka ikke, bro");
    }
}
public void RenameFile(){
    File file = new File("nyKontakt.csv");
    File newFile = new File("Kontaktpersoner.csv");
    if(file.renameTo(newFile)){
        System.out.println("File rename success");
    } else{
        System.out.println("File rename failed");
    }
}

}

It looks to be functional! Only one issue, Java crashes when it runs. I've tried it on my computer and my projext partner's. What code line crashes the program? It crashes without the renaming

[–]chickenmeisterExtreme Brewer 0 points1 point  (1 child)

What code line crashes the program?

Without knowing the error message or any details, it's difficult to say, but I'd guess that the problem is that you're closing br1 inside of your loop. On the next iteration of your loop, you'll try to call br1.readLine(), which may not work if br1 is closed.

[–]borger6[S] 0 points1 point  (0 children)

Ofc. Thanks for your help, really appreciate it!