Questions tagged [nio2]

NIO2 is an umbrella name for new features added to the original Java NIO package in the Java 1.7 release. Also referred to as "New I/O".

NIO.2 is an umbrella name for new features added to the original Java package in the Java 1.7 release. Also referred to as "New I/O".

  • Asynchronous Channels and Futures: A Channel that supports asynchronous I/O operations with implementations for sockets, server sockets and files.
  • Completion Handlers: The ability to register handlers that will be called on the completion of an asynchronous I/O event.
  • Asynchronous Channel Groups: A grouping of asynchronous channels for the purpose of resource sharing.
  • Asynchronous Multicast: Support for implementing asynchronous NIO I/O with callbacks for UDP multicast.
  • A New File System API for Java: Defines new interfaces and classes for the Java virtual machine to access files, file attributes, and file systems.

Articles and Tutorials

Note: This tag and wiki should be rolled into the nio tag.

173 questions
193
votes
8 answers

java.nio.file.Path for a classpath resource

Is there an API to get a classpath resource (e.g. what I'd get from Class.getResource(String)) as a java.nio.file.Path? Ideally, I'd like to use the fancy new Path APIs with classpath resources.
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
64
votes
2 answers

equivalent to Files.readAllLines() for InputStream or Reader?

I have a file that I've been reading into a List via the following method: List doc = java.nio.file.Files.readAllLines(new File("/path/to/src/resources/citylist.csv").toPath(), StandardCharsets.UTF_8); Is there any nice (single-line) Java…
Bitbang3r
  • 6,826
  • 5
  • 27
  • 40
28
votes
1 answer

How to check if the OS is POSIX compliant

I am writing a cross-platform application that creates temporary files and copies these to another location, where they need to be readable by everyone. (By default, only the owner has read access to temporary files.) I tried using the POSIX file…
Mangara
  • 1,116
  • 1
  • 10
  • 23
18
votes
3 answers

What is the difference between Files.list and Files.walkFileTree and Files.walk with maxdepth = 1?

If I want to do something with files only on the first level of the directory, is there a difference between using Files.list(...) or Files.walkFileTree(...) or Files.walk(...)? Files.walkFileTree(directory, Collections.emptySet(), 1, new…
andrybak
  • 2,129
  • 2
  • 20
  • 40
18
votes
1 answer

Why is Java 7 Files.walkFileTree throwing exception on encountering a tar file on remote drive

Im using Files.WalkFileTree() to navigate folder and counting audio files, but there is a problem when it encounters a tar file, it seems to be treating it as an actual folder I was expecting it to just skip over it. I cannot see any options that…
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
17
votes
1 answer

How to replace File.listFiles(FileFilter filter) with nio in Java 7?

I have some file I/0 traversal code written in Java 6, trying to move it the New I/O in Java 7 but I cannot find any replacement for this kind of stuff. File[] files = dir.listFiles(AudioFileFilter.getInstance()); Namely, no way to filter paths…
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
16
votes
3 answers

Quickest way to use common OpenOption combinations

Is there a concise, idiomatic way (maybe using Apache Commons) to specify common combinations of OpenOption like StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING
Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99
14
votes
4 answers

Find the directory for a FileStore

I'm trying to find a way to detect when a flash drive has been plugged into my computer. So far, the solution I found was to poll FileSystem#getFileStores for changes. This does indeed tell me when the flash drive has been inserted, but as far as I…
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
12
votes
2 answers

Why is NIO.2 FileVisitor type generic?

I'm doing some research on Java NIO.2 and its file operations, and currently I'm playing with filetree-walking functions and classes. NIO.2 FileVisitor API is wonderful, it's a shame that such thing has been added to Java SE only recently, not ten…
Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
11
votes
2 answers

Different results reading file with Files.newBufferedReader() and constructing readers directly

It seems that Files.newBufferedReader() is more strict about UTF-8 than the naive alternative. If I create a file with a single byte 128---so, not a valid UTF-8 character---it will happily be read if I construct an BufferedReader on an…
Robert Tupelo-Schneck
  • 10,047
  • 4
  • 47
  • 58
11
votes
1 answer

right way to set a Path to readonly in java.nio2

I'm confused... according to this Java page the File.setReadOnly() function is now a "legacy" function and should be replaced by Files.setAttribute()... but this requires you to know whether you are working with a DOS or a POSIX filesystem. I just…
Jason S
  • 184,598
  • 164
  • 608
  • 970
11
votes
2 answers

How do you close a AsynchronousSocketChannel cleanly?

My server uses a AsynchronousServerSocketChannel that listens for client connections using a CompletionHandler. When a client connection is accepted, the AsynchronousSocketChannel is read, again using a CompletionHandler to receive the data with no…
Nick Holt
  • 33,455
  • 4
  • 52
  • 58
11
votes
2 answers

Java NIO2 AsynchronousSocketChannel/AsynchronousServerSocketChannel and TLS/SSL

All the sources/samples on the INTERNET that available on NIO2 are without TLS/SSL support, java.nio.channels.AsynchronousSocketChannel java.nio.channels.AsynchronousServerSocketChannel As I understand the SSLEngine life-cycle of connection differ…
user1740371
  • 131
  • 1
  • 3
10
votes
2 answers

Is this an off-by-one bug in Java 7?

I don't know where to seek clarifications and confirmations on Java API documentation and Java code, so I'm doing it here. In the API documentation for FileChannel, I'm finding off-by-one errors w.r.t. to file position and file size in more places…
Harry
  • 3,684
  • 6
  • 39
  • 48
9
votes
2 answers

Java I/O: Ensure a file is not locked by another process before any r/w operation

I'm encountering a recurrent issue in an application that tracks content of files within a directory, based on the Java 7 WatchService API. When the underlying file system fires a modification event on a file, I want to compute its SHA-256 right…
sylvain
  • 246
  • 2
  • 5
1
2 3
11 12