Pankaj Pathania Pankaj Pathania - 6 months ago 34
Java Question

The system cannot find the path specified, ** IO Exception**

I m working on files and trying to meke multiple files and i'm getting exception as :

java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at com.create.CreatingFiles.create(CreatingFiles.java:25)
at com.create.CreatingFiles.main(CreatingFiles.java:36)


and code is :

File file = new File("F://fileIO");
StringBuffer buffer = null;
File newFile;

try {
if (!file.exists()) {
file.mkdir();
buffer = new StringBuffer(file.getAbsolutePath().toString());

} else {
System.out.println("DIRECTORY EXISTS");
buffer = new StringBuffer(file.getAbsolutePath().toString());
}

for (int i = 0; i < 10; i++) {
newFile = new File(buffer.append("/new File").append(i)
.append(".txt").toString()); //ERROR
if (!newFile.exists()) {
newFile.createNewFile();
System.out.println(newFile.getAbsolutePath());
} else {
System.out.println("FILE EXISTS");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


now correct me if I am wrong, i'm thinking that, i need to close file resources so that I can reassign it to new file but cannot close it.

OR

Something else is causing that error??

Answer
newFile = new File(buffer.append("/new File").append(i)
                        .append(".txt").toString());

this line append previous path like /newFile0.txt/newFile1.txt that's why it give you error.Do not append just concatenate newFile = new File(buffer+"/newFile"+i+".txt");

Comments