java.nio.file

How to move directories using jdk7

牧云@^-^@ 提交于 2019-12-12 11:26:21
问题 Using jdk7 , I am trying to use the java.nio.file.Files class to move an empty directory, let's say Bar , into another empty directory, let's say Foo Path source = Paths.get("Bar"); Path target = Paths.get("Foo"); try { Files.move( source, target, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); } After executing that code snippet, I expected that the Bar directory would be in the Foo directory ( ...\Foo\Bar ). Instead it is not. And here's the kicker, it's

Converting a string to FileTime in java

ε祈祈猫儿з 提交于 2019-12-12 02:19:15
问题 I have this problem. PROBLEM: I am making a program in which i am taking input from user via a JFormatedTextField i.e. in string format and then i want this value to be creation time of a file chosen by user. So I need to uses setTimes() function which only accepts fileTime format. So the PROBLEM is:::: how do I convert the string into an eligible fileTime so that it can be used by the function setTimes() which is predefined in .nio.attribute. http://www.docjar.com/docs/api/java/nio/file

How can I set OTHERS_WRITE when creating a file?

允我心安 提交于 2019-12-11 03:17:17
问题 I am attempting to use createFile with a FileAttribute derived from "rw-rw-rw-" . My file is being created as "rw-rw-r--" on Fedora. How can I set OTHERS_WRITE when creating a file? Example: (->> "rw-rw-rw-" PosixFilePermissions/fromString PosixFilePermissions/asFileAttribute vector into-array (Files/createFile (Paths/get "temp" (into-array String [])))) ;;; temp is created as rw-rw-r-- 回答1: In unix like systems every process has a property called umask which is masked onto the permissions of

WatchService Api file watching throws exception when try access file in created event occur

梦想的初衷 提交于 2019-12-11 01:08:30
问题 i have code below to tracking file changes on a dedicate folder Path path = Paths.get("f:\\logs"); WatchService watchService = FileSystems.getDefault().newWatchService(); WatchKey key = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); while (true) { final WatchKey watchKey = watchService.take(); for (WatchEvent<?> watchEvent : key.pollEvents()) { WatchEvent.Kind<?> kind = watchEvent.kind(); if (kind == StandardWatchEventKinds.ENTRY_CREATE) { WatchEvent<Path> eventPath =

Alternative to java.nio.file

一曲冷凌霜 提交于 2019-12-10 16:23:45
问题 I'm using Android Studio 2.3.1 and jdk 1.8 for this project. I import a project and it's using 'java.nio.file.Files' and 'java.nio.file.Paths'. Package "java.nio.file" does not exist in Java 8. Also Android Studio does not allow to select jdk 1.7. What can i do with this code ? public void toFile(final String path) throws IOException { Files.write(Paths.get(path), toBuffer()); } and this code public static Source fromFile(final String path) throws IOException { return fromBuffer(Files

Reading files in a SpringBoot 2.0.1.RELEASE app

陌路散爱 提交于 2019-12-07 17:58:31
问题 I have a SpringBoot 2.0.1.RELEASE mvc application. In the resources folder I have a folder named /elcordelaciutat. In the controller I have this method to read all the files inside the folder ClassLoader classLoader = this.getClass().getClassLoader(); Path configFilePath = Paths.get(classLoader.getResource("elcordelaciutat").toURI()); List<String> cintaFileNames = Files.walk(configFilePath) .filter(s -> s.toString().endsWith(".txt")) .map(p -> p.subpath(8, 9).toString().toUpperCase() + " / "

Order of traversal in Files.walkFileTree

五迷三道 提交于 2019-12-06 18:28:08
问题 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 preVisitDirectory method can be used to specify the order of visit but what is default behaviour ? 回答1: The order in which the subdirectories are read is not defined as per this comment in the Java Tutorial: A file tree is walked depth first, but you cannot

Reading files in a SpringBoot 2.0.1.RELEASE app

北战南征 提交于 2019-12-05 22:44:47
I have a SpringBoot 2.0.1.RELEASE mvc application. In the resources folder I have a folder named /elcordelaciutat. In the controller I have this method to read all the files inside the folder ClassLoader classLoader = this.getClass().getClassLoader(); Path configFilePath = Paths.get(classLoader.getResource("elcordelaciutat").toURI()); List<String> cintaFileNames = Files.walk(configFilePath) .filter(s -> s.toString().endsWith(".txt")) .map(p -> p.subpath(8, 9).toString().toUpperCase() + " / " + p.getFileName().toString()) .sorted() .collect(toList()); return cintaFileNames; running the app.

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

百般思念 提交于 2019-12-05 14:05:14
问题 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 it, however after some research I found out that a reference to an interface is allowed but it must point to a class that implements the interface. Looking on from this I looked at the Paths class and saw that it didn't implement Path. Looking at the

Issue with Jenkins pipeline and java.nio.file.* methods

独自空忆成欢 提交于 2019-12-05 07:08:02
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 various node blocks are correct--they uniquely identify specific nodes. However, pathExists (and other code that moves, copies, or deletes files) always executes on the master node. Any ideas what's happening or how to fix it? import java.nio.file.* String slavePath = 'C:\\Something\\only\\on\\slave\\node' String masterPath = 'D:\\Something\\only\\on\