0
File Name = new File("G:/Java/lesson25_1/Name.txt");

if(Name==null){

    JOptionPane.showMessageDialog(this, "Details are missing");
}

try{

    FileReader fr1 = new FileReader("Name.txt");

    BufferedReader br1=new BufferedReader(fr1);

    String str=br1.readLine();

    br1.close();

when i do this in a method it dosn't work when i click the button

Amol B.
  • 164
  • 4
Dany123
  • 39
  • 1
  • 8
  • Possible duplicate of [How do I create a file and write to it in Java?](https://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it-in-java) – M3talM0nk3y Aug 12 '17 at 15:05

1 Answers1

0

There is a method for file called exists to check if the given file exist or not.

String path = "G:/Java/lesson25_1/Name.txt";
File file = new File(path);

if(!file.isFile() || !file.canRead()){
    JOptionPane.showMessageDialog(this, "Details are missing");
} else {
    try {

        FileReader fr1 = new FileReader("Name.txt");
        BufferedReader br1=new BufferedReader(fr1);

        String str=br1.readLine();

        br1.close();
    } catch (Exception e){
        e.printStackTrace();
    }
}
XPLOT1ON
  • 2,994
  • 2
  • 20
  • 36
  • Further reading into the docs, you should use `isFile()` instead, `exists()` will return true if the path is a directory. I have edited my solution. @Dany123 – XPLOT1ON Aug 12 '17 at 15:03