Home Getting java.io.IOException: No such file or directory intermittently
 I have a spring scheduled job which runs every minute. Whenever a predefined criteria matches, it does following. Creates a directory Writes a bunch of files into it in a loop Zips it Copies the zip into another location Deletes the directory Psuedo-code for above. // id is numeric primary key. so it is guarenteed to be unique and contains no special characters final List attachmentDtos = service.getAttachments(id); if (!attachmentDtos.isEmpty()) { final File directoryFolder = new File(properties.getZIPFolder() + fileSeperator+ id); // create directory if (!directoryFolder.exists()) { logger.debug("Creating directory : {}", directoryFolder); final boolean isDirectoryCreated = directoryFolder.mkdir(); logger.debug("Directory: {}, created : {}", directoryFolder, isDirectoryCreated); } // write files into directory for (final AttachmentDTO dto : attachmentDtos) { final byte[] dataFromGeneva = util.getStream(dto.getFileKey); final String fileName = dto.getFilename(); final File file = new File(directoryFolder, fileName); logger.debug("getAbsolutePath ={}",file.getAbsolutePath()); if (!file.exists()) { final boolean isFileCreated = file.createNewFile(); logger.info("Is new file created : {}", isFileCreated); } try (FileOutputStream fostream = new FileOutputStream(file)) { IOUtils.write(data, fostream); fostream.flush(); } } // Zip the directory final String zipFilePath = FileTransferUtil.writeZipFile(directoryFolder, properties.getZIPFileFolder()); FileTransferUtil.copyZip(zipFilePath); FileUtils.deleteDirectory(directoryFolder); }  This piece of code has been working fine for a long time, but recently started giving problems. In the for loop it works fine for few files and then suddenly it throws IOException. Sample logs, 2018-04-16 12:17:39,385 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:250) : Creating directory : /app/sharedPath/zipfilefolder/125201 2018-04-16 12:17:39,386 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:252) : Directory: /app/sharedPath/zipfilefolder/125201, created : true 2018-04-16 12:17:40,306 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:269) : getAbsolutePath=/app/sharedPath/zipfilefolder/125201/sample file 1.pdf 2018-04-16 12:17:40,306 - INFO - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:272) : Is new file created : true 2018-04-16 12:17:40,442 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:269) : getAbsolutePath=/app/sharedPath/zipfilefolder/125201/sample file 2.pdf 2018-04-16 12:17:40,442 - INFO - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:272) : Is new file created : true 2018-04-16 12:17:40,793 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:269) : getAbsolutePath=/app/sharedPath/zipfilefolder/125201/sample file 3.pdf 2018-04-16 12:17:40,794 - ERROR - [pool-1-thread-1] com.test.sp.jobs.helper.SPJobHelper.handleError(SPJobHelper.java:80) : errMsg=java.io.IOException: No such file or directory at java.io.UnixFileSystem.createFileExclusively(Native Method) at java.io.File.createNewFile(File.java:1012) at com.test.Test.downloadAttachments(Test.java:271) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy\$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)  If it was a path or access issue then it wouldnt work for the first few iterations. I thought since the file name has space in it "sample file 1.pdf", that might mess up the path. But thats untrue. It seems to be working just fine for other iterations and retries. There is no concurrency here; only 1 thread is writing, reading, deleting on that directory. After the scheduled job fails, we reset the criteria in DB so that it is picked up again. This time all the files go through. So it doesnt look like a problem with the file parameters. The job is running on a linux machine. I have gone through similar questions but did not find anything useful. Even tried to find the native implementation for UnixFileSystem.createFileExclusively(). I have no clue on why system is throwing this IOException intermittently. Hope someone can point out what am missing here. Thanks!