添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
侠义非凡的大象  ·  Delphi ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I currently have the problem that I encounter an exception I never saw before and that's why I don't know how to handle it.

I want to create a file according to given parameters, but it won't work.

public static Path createFile(String destDir, String fileName) throws IOException {
        FileAccess.createDirectory( destDir);
        Path xpath = new Path( destDir + Path.SEPARATOR + fileName);
        if (! xpath.toFile().exists()) {
            xpath.toFile().createNewFile();
            if(FileAccess.TRACE_FILE)Trace.println1("<<< createFile " + xpath.toString() );
      return xpath;
  public static void createDirectory(String destDir) {
      Path dirpath = new Path(destDir);
      if (! dirpath.toFile().exists()) {
          dirpath.toFile().mkdir();
          if(TRACE_FILE)Trace.println1("<<< mkdir " + dirpath.toString() );

Every time I run my application the following exception occurs:

java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
[...]

How do I get rid of it? (I am using Win7 64bit btw)

The problem is that a file can't be created unless the entire containing path already exists - its immediate parent directory and all parents above it.

If you have a path c:\Temp and no subdirectories below it, and you try to create a file called c:\Temp\SubDir\myfile.txt, that will fail because C:\Temp\SubDir doesn't exist.

Before

   xpath.toFile().createNewFile(); 
   xpath.toFile().mkdirs(); 

(I'm not sure if mkdirs() requires just the path in the object; if it does, then change that new line to

   new File(destDir).mkdirs();

Otherwise, you'll get your filename created as a subdirectory instead! You can verify which is correct by checking your Windows Explorer to see what directories it created.)

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.