Pankaj Pathania Pankaj Pathania - 5 months ago 28
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 : The system cannot find the path specified
at Method)
at Source)
at com.create.CreatingFiles.create(
at com.create.CreatingFiles.main(

and code is :

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

try {
if (!file.exists()) {
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()) {
} else {
System.out.println("FILE EXISTS");
} catch (IOException e) {
// TODO Auto-generated catch block

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.


Something else is causing that error??

newFile = new File(buffer.append("/new File").append(i)

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");