Questions tagged [java-nio]

Use the tag for questions related with the Java package java.nio

Java Package java.nio defines buffers, which are containers for data, and provides an overview of the other NIO packages.

Oracle java-nio Package Description

39 questions
119
votes
4 answers

How to create a new file together with missing parent directories?

When using file.createNewFile(); I get the following exception java.io.IOException: Parent directory of file does not exist: /.../pkg/databases/mydb I am wondering is there a createNewFile that creates the missing parent directories?
Pentium10
  • 204,586
  • 122
  • 423
  • 502
10
votes
6 answers

Alternative to File.exists() in Java

I never thought it would happen to me, but I ran into my first bug in Java: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5003595 I'm pretty much in the same exact situation as described in the bug (NFS on linux), and I'm seeing that…
Ben
  • 7,692
  • 15
  • 49
  • 64
5
votes
0 answers

How AsynchronousSocketChannel maintain reading?

I need to connect to 4 machines and read data from sockets. I've chosen to use an async model of nio2. Here is a pseudo-code : class Connector { private final AsynchronousChannelGroup group; private final String host; private final int…
flywell
  • 384
  • 3
  • 20
3
votes
2 answers

Only copy a file after it is being done written to

I have a non-Java program writing files to a directory. My Java program then copies these files to a different directory. Is there a way for Java to check if the last file is fully written to before being copied? This is because when I copy the…
MKRabbit
  • 49
  • 5
3
votes
1 answer

Why is StandardOpenOption.CREATE_NEW not creating a file to be read from in Java?

Here is my code: import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.nio.file.Path; import…
Shri
  • 109
  • 9
3
votes
1 answer

Reliable alternative to Java Watch Service

I am using Java nio's WatchService but I find it quite unreliable for the following use cases: When a very big file (>500 megs) is written to the directory being watched, if I rely on ENTRY_CREATE event, the file is often times not yet ready to…
saurabh.in
  • 389
  • 3
  • 13
3
votes
1 answer

FileSystem for zip file inside zip file

Can a java.nio.file.FileSystem be created for a zip file that's inside a zip file? If so, what does the URI look like? If not, I'm presuming I'll have to fall back to using ZipInputStream. I'm trying to recurse into the method below. Current…
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
2
votes
3 answers

Java InputStream: copy to and calculate hash at the same time

Here are my two code snippets: public class Uploader { private static final String SHA_256 = "SHA-256"; public String getFileSHA2Checksum(InputStream fis) throws IOException { try { MessageDigest md5Digest =…
Jordi
  • 20,868
  • 39
  • 149
  • 333
1
vote
1 answer

java: rename file and add suffix if it already exists

I need to rename a file. Path destPath = Paths.get(directoryPath, hash); Files.move(path, destPath); The problem is that when I'm trying to "rename" the file, it can already exist. Is there anyway to solve it by convention adding a suffix like…
Jordi
  • 20,868
  • 39
  • 149
  • 333
1
vote
1 answer

Selector.select() not being unblocked even after I register a channel ready to read from another thread

I am creating a server to handle many different connections at the same time. I create two Selectors, on for the serverSocketChannel to accept and then the other for the connections to read data. The one selector successfully gets past the blocking…
Matthew
  • 3,886
  • 7
  • 47
  • 84
1
vote
1 answer

Java - netty library execution time very big - Java NIO

I am developing a Java application that reads data from a Redis Database, I use Lettuce library to connect to Redis which in turn uses 'Netty' library to communicate with Redis I suspect that the execution time of my application is greater than…
ammcom
  • 992
  • 1
  • 7
  • 24
1
vote
0 answers

Enumerating a folder on Windows share throwing FileNotFoundException while sub folder getting deleted in parallel

I have a Java method that looks at a folder on Windows share and delete any sub folder whose modification timestamp is 2 days or more ago. Basically delete stale directories as this is a staging folder where other threads in the process queue up…
videoguy
  • 1,732
  • 2
  • 24
  • 49
1
vote
1 answer

Java Selector for socket client not waked up after changing of interested ops from different thread

I use Java Selector for both server and client. For Server side it works perfect. It stops the thread when i call select() and wakes up when i change interest ops and it is ready for this operation.. But unfortunatelt it does not work for the same…
Vetalll
  • 3,472
  • 6
  • 24
  • 34
1
vote
1 answer

Java Folder/File watch

I am trying to build a application that watch a folder and its sub folders to detect file creation or modification. Total files to watch will be growing day by day. I had tried with java nio WatchService and apache common FileAlterationObserver.…
J-007
  • 206
  • 2
  • 10
1
vote
0 answers

does java Files.delete remove files without write permission?

I am trying to write a unit test to test IOException handling in some code. I thought I would be able to create an IOException by removing permissions from a file and trying to delete it. But it looks like the file gets deleted anyway. 1st question…
Jeff Gaer
  • 351
  • 3
  • 21
1
2 3