This standard Java package defines classes and interfaces to access files, file attributes and file systems.
Questions tagged [java.nio.file]
159 questions
12
votes
3 answers
Java File.exists() versus File.isFile()
I'm unable to think of a realistic use case for the method java.io.File.exists() or its equivalent in Java 7 java.nio.file.Files.exists(Path). It seems that isFile() or isDirectory() would be preferable in all cases (or canRead(), canWrite(),…

jaco0646
- 15,303
- 7
- 59
- 83
11
votes
3 answers
How to guarantee atomic move or exception of a file in Java?
In one of my projects I have concurrent write access to one single file within one JRE and want to handle that by first writing to a temporary file and afterwards moving that temp file to the target using an atomic move. I don't care about the order…

Thorsten Schöning
- 3,501
- 2
- 25
- 46
11
votes
2 answers
Setting/changing the ctime or "Change time" attribute on a file
I wish to change the timestamp metadata on files in Java using the java.nio.Files class.
I would like to change all 3 Linux/ext4 timestamps (last modified, access, and changed).
I am able to change the first two timestamp fields as…

Eitan
- 1,308
- 3
- 15
- 32
9
votes
3 answers
How to inject java.nio.file.Path dependency using @ConfigurationProperties
I'm using Spring Boot and have the following Component class:
@Component
@ConfigurationProperties(prefix="file")
public class FileManager {
private Path localDirectory;
public void setLocalDirectory(File localDirectory) {
…

James
- 2,876
- 18
- 72
- 116
9
votes
1 answer
Files.newInputStream() inconsequential behaviour when the target Path is a directory?
NOTE: Please run the exact code below; no adaptations of it, in particular, do not use File, as this bug is tied to the new java.nio.file API
OK, this is not really a "question which is in need of an answer" but rather a call for…

fge
- 119,121
- 33
- 254
- 329
8
votes
1 answer
JMeter - Static method get( java.lang.String ) not found in class'java.nio.file.Paths'
I am trying to create a JMeter load test. I need the test to take a sample log file and change its name. The only way I could find to do this was to copy the file in a BeanShell Preprocessor but I am getting the following error:
ERROR -…

Grimx34
- 83
- 1
- 6
8
votes
1 answer
Bug in Oracle's JDK zip filesystem, how do you write an SSCCE to reproduce it?
This bug is present in the latest 1.7 and 1.8 versions of the JDK (7u72, 8u25). Required: jackson-databind 2.5.0. Tested on Linux x86_64 (Ubuntu 14.10 to be precise).
Code:
public static void main(final String... args)
throws IOException
{
…

fge
- 119,121
- 33
- 254
- 329
7
votes
1 answer
Issue with Jenkins pipeline and java.nio.file.* methods
I am trying to use methods from java.nio.file.* to perform some basic file operations in a Jenkins pipeline. Regardless of the node block in which the code exists, the code executes on the master node. In the pipeline, I have verified that the…

gaccardo
- 377
- 2
- 13
7
votes
1 answer
WatchService - incorrectly resolved absolute path
I've been playing around with the java.nio.file.WatchService and noticed that the Paths returned from WatchEvent.context() does not return correct .toAbsolutePath(). Here is an example application:
public class FsWatcher {
public static void…

Svetlin Zarev
- 14,713
- 4
- 53
- 82
7
votes
1 answer
Is Files.getLastModifiedTime() leaking memory?
I have encountered a bug where one of our server applications was using more and more memory every second pretty much and I have managed to filter out a short example that still shows that behavior:
public class TestGetLastModifiedTime {
private…

skiwi
- 66,971
- 31
- 131
- 216
6
votes
4 answers
creating a virtual file system with JIMFS
I'd like to use Google's JIMFS for creating a virtual file system for testing purposes. I have trouble getting started, though.
I looked at this tutorial: http://www.hascode.com/2015/03/creating-in-memory-file-systems-with-googles-jimfs/
However,…

user3629892
- 2,960
- 9
- 33
- 64
5
votes
2 answers
Different behaviour for Path.toFile() and new File(pathString)
I have mounted a network drive (samba server) in Windows.
I have a file in that drive that I wanted to read in a Java program.
Before I was trying to read the file using:
Paths.get(basePath, fileName).toFile()
But it was failing with error that…

ata
- 8,853
- 8
- 42
- 68
5
votes
0 answers
Java 8 FileSystem access file in zip package zipped in another zip
I want to ask if is possible to access and modify a text file inside a zip file which is zipped in another zip package.
I know that can I access the file using Java.nio FileSysten like this. In this example to read all the lines in "sample.txt"…

kovko
- 123
- 1
- 8
5
votes
5 answers
Handle temporary file in try with resources
For my application, I have to write a method that takes an InputStream as argument, writes the content to a temporary file, performs some operations and finally deletes the temporary.
This is what I have so far:
public void myMethod(InputStream in,…

link
- 1,676
- 1
- 13
- 21
5
votes
1 answer
Why is 'Path.startsWith' behaviour different from a 'String.startsWith' operation - even for 'Path.getFilename'
I am using Java version 1.8.0_31.
I am trying to recursively access a directory tree using the FileVisitor interface.
The program should print the name of all files in C:/books whose file name starts with "Ver".
The directory C:/books has two files…

Nicks
- 16,030
- 8
- 58
- 65