0

Below program throws "java.nio.file.NoSuchFileException" when subdirecties doesnot exist in path. Could some one please help how can I acheive this ? I want to insert records in asyncronous way.

public static void main(String[] args) {

    String str = "testing application2.";
    Path path = Paths.get("/Users/santosh/test/operatord/testsd.txt");
    Set<StandardOpenOption> set = new HashSet<>();
    set.add(WRITE);
    set.add(CREATE);
    try {
        AsynchronousFileChannel asyncfileChannel = AsynchronousFileChannel.open(path, set,
                Executors.newFixedThreadPool(10));
        TestCompletionHandler handler = new TestCompletionHandler();
        ByteBuffer dataBuffer = ByteBuffer.wrap(str.getBytes());
        Attachment attachment = new Attachment(asyncfileChannel);
        asyncfileChannel.write(dataBuffer, 0, attachment, handler);
        attachment.getResponse().join();
        System.out.println(new String(Files.readAllBytes(path)));
    } catch (Throwable e) {
        e.printStackTrace();
    }

}
santosh jk
  • 111
  • 1
  • 11
  • None of the file-opening methods will create a directory for you. You have to do that yourself. But I don't see why you are using asynchronous I/O here at all. You start the operation and then you wait for it without having done anything else in between. There is no advantage here over using a simple `FileOutputStream` or `FileWriter`, just a lot of overhead. – user207421 Apr 20 '22 at 05:09

1 Answers1

1

AsynchronousFileChannel not creating subdirectories

That is correct. Opening a file won't create the directories on the path to the file. You will also get this behavior with all of the standard APIs for opening a file.

You need to create the missing directories as a separate operation.

The preferred way to do this is to use Files.makeDirectories method; see javadoc. This will attempt to create all of the missing directories.

You could also use the legacy File.mkdirs(path) method; see javadoc. It does the same thing, but it has the disadvantage that it doesn't report any meaningful diagnostics if the directory (or directories) cannot be created.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • yes I got that. but is there a way to create in asyncronous way ? – santosh jk Apr 08 '22 at 02:55
  • AFAIK, no there isn't. – Stephen C Apr 08 '22 at 03:07
  • @santoshjk Why? Opening the file isn't asynchronous either. Only the I/O. And the way you're doing it, not even that really. – user207421 Apr 20 '22 at 05:07
  • Well ... I guess ... you could create a bunch of threads (or tasks) and make each one responsible for creating a a directory or directories. But you would then need some internal data structure to track which directories have been created so far, and you would need a way to wait for creation of missing directories. I can't envisage a realistic use-case in which all of that extra complexity would offer any performance benefit. It certainly wouldn't help in the use-case of your example. – Stephen C Apr 20 '22 at 05:16