mardi 4 février 2020

Java: If file doesn´t exist I can´t create condition for file exist or not. Why?

Hi this is a simple code that compares whether a folder exists or not.

import java.io.*;

public class myfile {
    public static void main(String args[])
    {

        // Get the file
        File f = new File("file_f.txt");

        // Check if the specified file
        // Exists or not
        if (file_f.exists())
            System.out.println("Exists");
        else
            System.out.println("Does not Exists");
            System.out.println("I would like to run this possibility");
    }
}

But I need the code to be executed when the folder does not exist. But if I delete creating folder, It get an error ... (In my code, I need to check if the folder exists and if not, the code has started to execute.) So how do I do it if in some cases the folder does not already exist?

Now I deleted creating file, because I would like to run else part...

import java.io.*;

public class myfile {
    public static void main(String args[])
    {

        // Get the file

        //File f = new File("friends.txt");



        // Check if the specified file
        // Exists or not
        if (file_f.exists())
            System.out.println("Exists");
        else
            System.out.println("Does not Exists");
            System.out.println("I would like to run this possibility");
    }
}

but

Error:(14, 13) java: cannot find symbol symbol: variable f location: class myfile

So how do I do it if in some cases the folder does not already exist? Thank you

Aucun commentaire:

Enregistrer un commentaire