1

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??

bananas
  • 1,176
  • 2
  • 19
  • 39
  • 2
    Show the full stacktrace that includes line numbers. – Kayaman May 10 '16 at 06:26
  • 2
    the `/` character doesn't need to be escaped. It's the `\\` that needs to be escaped by repeating it. You might try "F:\\fileIO" – Hank D May 10 '16 at 06:27
  • @HankD actually I'm trying to create a new text file under **fielIO** so i used **/fileName** ,that **/** will provide path to name of file. is that a wrong approach ?? – bananas May 10 '16 at 06:30
  • First of all: **always** close streams if you don't use them. I'd recommend [try-with-resources](http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html). And the path-name doesn't exactly look valid to me. Haven't worked on windows in some time though. –  May 10 '16 at 06:31
  • which platform you using windows or linux? as in windows `F://` should be replaced with `F:\\ ` – piyushj May 10 '16 at 06:32
  • Did you check whether the directory `F:\fileIO` actually exists? `createNewFile()` does not create intermediate directories. – Andreas Fester May 10 '16 at 06:33
  • actually it exists and the **new file0** is created too – bananas May 10 '16 at 06:34
  • 1
    Directory separators are usually either "\\" (because the backslash needs to be escaped in java strings), or "/", depending on the OS. Instead of using either one, it is safest to use `File.separatorchar`, e.g. `"F:" + File.separatorChar + "fileIO"` – Hank D May 10 '16 at 06:36
  • but as the loop counts to 10 so it is supposed to create newfile[0-9] and **code is throwing exception after creation of one file** I think i need to close **newfile** every time so it can be reassigned new path to create file ,but i'm not able to close it. – bananas May 10 '16 at 06:37
  • i got it , was using buffer, replaced it with simple string concatenation and it worked , thanks, – bananas May 10 '16 at 06:44

2 Answers2

3
File file = new File("C://TNS_ADMIN");
        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(new StringBuffer(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();
        }
    }

When you call the append method of StringBuffer, it writes on itself. That is the problem.

Burak Uyar
  • 119
  • 1
  • 5
1
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

Solution 1:

newFile = new File(buffer+"/newFile"+i+".txt");

Solution 2

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

Java: String concat vs StringBuilder - optimised, so what should I do?

Community
  • 1
  • 1
Khan Abdulrehman
  • 816
  • 2
  • 10
  • 22