Questions tagged [java.nio.file]

This standard Java package defines classes and interfaces to access files, file attributes and file systems.

159 questions
52
votes
6 answers

Watching a Directory for Changes in Java

I want to watch a directory for file changes. And I used WatchService in java.nio. I can successfully listen for file created event. But I can't listen for file modify event. I checked official java tutorial, but still struggling. Here is the source…
Isuru
  • 3,818
  • 13
  • 49
  • 64
50
votes
5 answers

What is the exact use of java nio package when already methods are available with io package

I was learning java nio package and I realized there are lots of methods already provided by File which nio.Files is providing again by using Path class. Like that few more I got. I am actually not getting what is the actual use of nio package. I am…
Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128
46
votes
1 answer

Remove first element of a Stream in Java 8

I have generated a Stream in Java 8 with Files.walk() method from java.nio library. The problem is that the method includes by default the root path but I do not want this element. I have solved in this case with this code using filter()…
35
votes
3 answers

Can there be a (Java 7) FileSystem for which a Path .isAbsolute() but has a null root?

The javadoc for .isAbsolute() says: Tells whether or not this path is absolute. An absolute path is complete in that it doesn't need to be combined with other path information in order to locate a file. Returns: true if, and only if, this path…
fge
  • 119,121
  • 33
  • 254
  • 329
21
votes
4 answers

java - The process cannot access the file because it is being used by another process

I have a piece of code that monitors a directory for addition of files. Whenever a new file is added to the directory, the contents of the file are picked and published on kafka and then the file is deleted. This works when I make a single request…
20
votes
2 answers

How do I use directory globbing in JDK7

I have been trying to use the new globbing feature in JDK7, starting from the documentation and examples I can get globs such as "glob:*.dat" to work with the Files.walkFileTree(startingDir, finder); example but I have been unable to get the "**"…
peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
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
2 answers

java.nio.file: Where is the Path interface actually implemented?

Recently I was doing some coding using the java.nio.file package introduced in Java 7 and saw an example using Path like this: Path path = Paths.get("C:\\Users"); Given that Path is an interface I was confused on how you could have a reference to…
Levenal
  • 3,796
  • 3
  • 24
  • 29
15
votes
1 answer

Hot to get rid of an java.io.Exception at java.io.WinNTFileSystem.createFileExclusively?

I currently have the problem that I encounter an exception I never saw before and that's why I don't know how to handle it. I want to create a file according to given parameters, but it won't work. public static Path createFile(String destDir,…
keinabel
  • 1,002
  • 3
  • 15
  • 33
15
votes
2 answers

Order of traversal in Files.walkFileTree

What is the order in which Files.walkFileTree visits files/directories at the same level? It doesn't appear to visit them in order of size, last modified time or name. I couldn't find anything in the API documentation either. Perhaps the…
Sridhar
  • 2,416
  • 1
  • 26
  • 35
14
votes
1 answer

Files.move and Files.copy is throwing java.nio.file.FileAlreadyExistsException

I want to delete one file and rename another file with the old file but I am not able to move this file as java is throwing java.nio.file.FileAlreadyExistsException Following is the code snippet I am using static void swapData(String origFilePath,…
aga
  • 359
  • 2
  • 4
  • 15
14
votes
1 answer

Why do I get ProviderMismatchException when I try to .relativize() a Path against another Path?

[note: self answered question] I have opened a FileSystem to a zip file using java.nio. I have gotten a Path from that filesystem: final Path zipPath = zipfs.getPath("path/into/zip"); Now I have a directory on the local filesystem which I have…
fge
  • 119,121
  • 33
  • 254
  • 329
13
votes
2 answers

How to handle FileSystemAlreadyExistsException?

How should you handle a FileSystemAlreadyExistsException when calling FileSystems.newFileSystem? One way would be to use the already created FileSystem (getFileSystem​), but then you could run into problems when the original creator closes it while…
Marcono1234
  • 5,856
  • 1
  • 25
  • 43
13
votes
1 answer

How to instantiate Path object using clojure

since a Path class does not have public constructor ,so path object are created using get factory method in Paths object. e.g Path p2 = Paths.get("/home/admin","Migrations","/blog/tables/6-rating.xml"); //or Path p2 = Paths.get(new…
sakhunzai
  • 13,900
  • 23
  • 98
  • 159
12
votes
3 answers

Using Java 8 NIO, how can I read a file while skipping the first line or header record?

I am trying to read a large file line by line, in java, using NIO library. But this file also contains headers... try (Stream stream = Files.lines(Paths.get(schemaFileDir+File.separator+schemaFileNm))){ …
vhora
  • 320
  • 2
  • 16
1
2 3
10 11