Solved programming problem

Find a File Location in Java.

Source code

package filecreate;

import java.io.File;

import java.io.IOException;

public class FileCreate {

    public static void main(String[] args) {

        File file = new File("hello.txt");

        if (file.exists()) ///checke for existing file

        {

            System.out.println("File Exists");

        }

        else ///if no file found

        {

            System.out.println("File does not exists, lets Create one...");

            try {

                file.createNewFile(); ///create file

                System.out.println("File Created");

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        ///find the place where this file is saved

        System.out.println("File Location: "+System.getProperty("user.dir"));

    }

}

Output

File Exists

File Location:F/Programming/Java

Join the discussion

Add your comment