java.io.FileNotFoundException The system cannot fi

2019-09-22 08:01发布

问题:

I have a timer set to invoke a class that contains pdf generating codes.I have set the file path where it needs to be saved..but it is showing exception as java.io.FileNotFoundException : D:\ (The system cannot find the path specified) .I dont know where is the mistake..

Here is my code..

try {

        OutputStream file = new FileOutputStream(new File("D://"));
        Document document = new Document();
         //PDF generating code..     
        document.add(list);            //In the new page we are going to add list
        document.close();

        file.close();

        System.out.println("Pdf created successfully..");

    } catch (Exception e) {
        e.printStackTrace();
    }

回答1:

You have not provided the file name so you get that exception,use below code

  OutputStream file = new FileOutputStream(new File("D://timer.pdf"));

See the doc here



回答2:

You should provide a valid file name. You just can't provide the directory name alone, which is why you get the FileNotFoundException.

OutputStream file = new FileOutputStream(new File("D://someFile.txt"));