mercredi 18 mars 2020

How do I create an if statement of only writing to the file if the file does not contain a certain string?

I am trying to create a code where it will first read the txt file, and then if it contains the word "Level3," then will it only write "Level4" into the same txt file. Would like some help please, thank you :)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;

public class Test2 {

public static void main(String[] args) {

    try {
        File outFile = new File("LevelUnlock.txt");                         
        BufferedReader br = new BufferedReader(new FileReader(outFile));
        FileWriter fw = new FileWriter(outFile.getAbsoluteFile());          
        BufferedWriter bw = new BufferedWriter(fw);

        String a = br.readLine();


        if (a.contains("Level3"))
        {
            bw.append("Level4");                                                
            JOptionPane.showMessageDialog(null, "Wrote level 4");


        }

        br.close(); 
        bw.close();         
        } catch (IOException ex) {
        }

}

}

Aucun commentaire:

Enregistrer un commentaire