List of usage examples for java.nio.file Files isDirectory
public static boolean isDirectory(Path path, LinkOption... options)
From source file:org.dia.kafka.isatools.producer.DirWatcher.java
/** * Process all events for keys queued to the watcher * @param isatProd/*from w w w. j a v a 2 s . c o m*/ */ void processEvents(ISAToolsKafkaProducer isatProd) { for (;;) { // wait for key to be signalled WatchKey key; try { key = watcher.take(); } catch (InterruptedException x) { return; } Path dir = keys.get(key); if (dir == null) { System.err.println("WatchKey not recognized!!"); continue; } List<JSONObject> jsonParsedResults = new ArrayList<JSONObject>(); for (WatchEvent<?> event : key.pollEvents()) { WatchEvent.Kind kind = event.kind(); // TBD - provide example of how OVERFLOW event is handled if (kind == OVERFLOW) { continue; } // Context for directory entry event is the file name of entry WatchEvent<Path> ev = cast(event); Path name = ev.context(); Path child = dir.resolve(name); // If an inner file has been modify then, recreate the entry if (kind == ENTRY_MODIFY || kind == ENTRY_CREATE) { File fileCheck = child.getParent().toFile(); if (child.toFile().isDirectory()) { fileCheck = child.toFile(); } System.out.format("[%s] %s : %s\n", this.getClass().getSimpleName(), kind.toString(), fileCheck.getAbsolutePath()); List<String> folderFiles = ISAToolsKafkaProducer.getFolderFiles(fileCheck); List<JSONObject> jsonObjects = ISAToolsKafkaProducer.doTikaRequest(folderFiles); if (!jsonObjects.isEmpty()) { // jsonParsedResults.addAll(jsonObjects); isatProd.sendISAToolsUpdates(jsonObjects); } } // TODO this event has still to be specified for documents if (kind == ENTRY_DELETE) { System.err.println(String.format("Delete event not supported %s", child.toAbsolutePath())); } // if directory is created, and watching recursively, then // register it and its sub-directories if (kind == ENTRY_CREATE) { try { if (Files.isDirectory(child, NOFOLLOW_LINKS)) { registerAll(child); } } catch (IOException x) { // ignore to keep sample readbale System.err.format("IOException when creating %s \n", child.toAbsolutePath()); } } } // reset key and remove from set if directory no longer accessible boolean valid = key.reset(); if (!valid) { keys.remove(key); // all directories are inaccessible if (keys.isEmpty()) { break; } } } }
From source file:org.codice.ddf.configuration.migration.ImportMigrationExternalEntryImpl.java
/** * Verifies the corresponding existing file or directory to see if it matches the original one * based on the exported info.//from www . j a v a2 s .co m * * @param required <code>true</code> if the file or directory was required to be exported; <code> * false</code> if it was optional * @return <code>false</code> if an error was detected during verification; <code>true</code> * otherwise */ @SuppressWarnings("squid:S3725" /* Files.isRegularFile() is used for consistency and to make sure that softlinks are not followed. not worried about performance here */) private boolean verifyRealFileOrDirectory(boolean required) { return AccessUtils.doPrivileged(() -> { final MigrationReport report = getReport(); final Path apath = getAbsolutePath(); final File file = getFile(); if (!file.exists()) { if (required) { report.record(new MigrationException(Messages.IMPORT_PATH_ERROR, apath, "does not exist")); return false; } return true; } if (softlink) { if (!Files.isSymbolicLink(apath)) { report.record( new MigrationWarning(Messages.IMPORT_PATH_WARNING, apath, "is not a symbolic link")); return false; } } else if (isFile() && !Files.isRegularFile(apath, LinkOption.NOFOLLOW_LINKS)) { report.record(new MigrationWarning(Messages.IMPORT_PATH_WARNING, apath, "is not a regular file")); } else if (isDirectory() && !Files.isDirectory(apath, LinkOption.NOFOLLOW_LINKS)) { report.record( new MigrationWarning(Messages.IMPORT_PATH_WARNING, apath, "is not a regular directory")); } return verifyChecksum(); }); }
From source file:io.mangoo.build.Watcher.java
@SuppressWarnings("all") private void handleEvents(WatchKey watchKey, Path path) { for (WatchEvent<?> watchEvent : watchKey.pollEvents()) { WatchEvent.Kind<?> watchEventKind = watchEvent.kind(); if (OVERFLOW.equals(watchEventKind)) { continue; }// www.j a v a 2s. c o m WatchEvent<Path> ev = (WatchEvent<Path>) watchEvent; Path name = ev.context(); Path child = path.resolve(name); if (ENTRY_MODIFY.equals(watchEventKind) && !child.toFile().isDirectory()) { handleNewOrModifiedFile(child); } if (ENTRY_CREATE.equals(watchEventKind)) { if (!child.toFile().isDirectory()) { handleNewOrModifiedFile(child); } try { if (Files.isDirectory(child, NOFOLLOW_LINKS)) { registerAll(child); } } catch (IOException e) { LOG.error("Something fishy happened. Unable to register new dir for watching", e); } } } }
From source file:com.drunkendev.io.recurse.tests.RecursionTest.java
/** * Answer provided by Brett Ryan.//from w w w . ja v a 2 s . c o m * * This test uses Java-8's java {@link java.util.stream.Stream Stream API} * by {@link Files#walk}. * * This is the parallel version. * * @see <a href="http://stackoverflow.com/a/24006711/140037">Stack-Overflow answer by Brett Ryan</a> */ // @Test public void testJava8StreamParallel() { System.out.println("\nTEST: Java 8 Stream Parallel"); time(() -> { try { Map<Integer, Long> stats = Files.walk(startPath).parallel().collect( groupingBy(n -> Files.isDirectory(n, LinkOption.NOFOLLOW_LINKS) ? 1 : 2, counting())); System.out.format("Files: %d, dirs: %d. ", stats.get(2), stats.get(1)); } catch (IOException ex) { fail(ex.getMessage()); } }); }
From source file:org.discosync.ApplySyncPack.java
/** * Apply a syncpack to a target directory. *//*from w w w . ja v a2s .co m*/ protected void applySyncPack(String syncPackDir, String targetDir) throws SQLException, IOException { // read file operations from database File fileOpDbFile = new File(syncPackDir, "fileoperations"); FileOperationDatabase db = new FileOperationDatabase(fileOpDbFile.getAbsolutePath()); db.open(); Iterator<FileListEntry> it = db.getFileListOperationIterator(); Path syncFileBaseDir = Paths.get(syncPackDir, "files"); String syncFileBaseDirStr = syncFileBaseDir.toAbsolutePath().toString(); int filesCopied = 0; int filesReplaced = 0; int filesDeleted = 0; long copySize = 0L; long deleteSize = 0L; // Collect directories during processing. List<FileListEntry> directoryOperations = new ArrayList<>(); // First process all files, then the directories while (it.hasNext()) { FileListEntry e = it.next(); // Remember directories if (e.isDirectory()) { directoryOperations.add(e); continue; } String path = e.getPath(); Path sourcePath = Paths.get(syncFileBaseDirStr, path); // may not exist Path targetPath = Paths.get(targetDir, path); // may not exist if (e.getOperation() == FileOperations.COPY) { // copy new file, target files should not exist if (Files.exists(targetPath)) { System.out .println("Error: the file should not exist: " + targetPath.toAbsolutePath().toString()); } else { if (!Files.exists(targetPath.getParent())) { Files.createDirectories(targetPath.getParent()); } Files.copy(sourcePath, targetPath); filesCopied++; copySize += e.getSize(); } } else if (e.getOperation() == FileOperations.REPLACE) { // replace existing file if (!Files.exists(targetPath)) { System.out.println("Info: the file should exist: " + targetPath.toAbsolutePath().toString()); } Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING); filesReplaced++; copySize += e.getSize(); } else if (e.getOperation() == FileOperations.DELETE) { // delete existing file if (!Files.exists(targetPath)) { System.out.println("Info: the file should exist: " + targetPath.toAbsolutePath().toString()); } else { long fileSize = Files.size(targetPath); if (fileSize != e.getSize()) { // show info, delete anyway System.out.println( "Info: the file size is different: " + targetPath.toAbsolutePath().toString()); } deleteSize += fileSize; Files.delete(targetPath); filesDeleted++; } } } db.close(); // Sort directory list to ensure directories are deleted bottom-up (first /dir1/dir2, then /dir1) Collections.sort(directoryOperations, new Comparator<FileListEntry>() { @Override public int compare(FileListEntry e1, FileListEntry e2) { return e2.getPath().compareTo(e1.getPath().toString()); } }); // Now process directories - create and delete empty directories for (FileListEntry e : directoryOperations) { String path = e.getPath(); Path targetPath = Paths.get(targetDir, path); // may not exist if (e.getOperation() == FileOperations.COPY) { // create directory if needed if (!Files.exists(targetPath)) { Files.createDirectories(targetPath); } } else if (e.getOperation() == FileOperations.DELETE) { if (!Files.exists(targetPath)) { System.out.println( "Info: Directory to DELETE does not exist: " + targetPath.toAbsolutePath().toString()); } else if (!Files.isDirectory(targetPath, LinkOption.NOFOLLOW_LINKS)) { System.out.println("Info: Directory to DELETE is not a directory, but a file: " + targetPath.toAbsolutePath().toString()); } else if (!Utils.isDirectoryEmpty(targetPath)) { System.out.println("Info: Directory to DELETE is not empty, but should be empty: " + targetPath.toAbsolutePath().toString()); } else { // delete directory Files.delete(targetPath); } } } System.out.println("Apply of syncpack '" + syncPackDir + "' to directory '" + targetDir + "' finished."); System.out.println("Files copied : " + String.format("%,d", filesCopied)); System.out.println("Files replaced: " + String.format("%,d", filesReplaced)); System.out.println("Files deleted : " + String.format("%,d", filesDeleted)); System.out.println("Bytes copied : " + String.format("%,d", copySize)); System.out.println("Bytes deleted : " + String.format("%,d", deleteSize)); }
From source file:de.teamgrit.grit.checking.compile.JavaCompileChecker.java
/** * Runs a command specified by a compiler invocation. * * @param compilerInvocation/*from ww w .j a va 2 s . co m*/ * specifies what program with which flags is being executed * @param pathToSourceFolder * the path to the source folder of the submission * @return CompilerOutput with fields initialized according to the outcome * of the process * @throws BadFlagException * if a flag is not known to javac */ private CompilerOutput runJavacProcess(List<String> compilerInvocation, Path pathToSourceFolder, boolean junit) throws BadFlagException { Process compilerProcess = null; try { ProcessBuilder compilerProcessBuilder = new ProcessBuilder(compilerInvocation); // make sure the compiler stays in its directory. if (Files.isDirectory(pathToSourceFolder, LinkOption.NOFOLLOW_LINKS)) { compilerProcessBuilder.directory(pathToSourceFolder.toFile()); } else { compilerProcessBuilder.directory(pathToSourceFolder.getParent().toFile()); } compilerProcess = compilerProcessBuilder.start(); } catch (IOException e) { // If we cannot call the compiler we return a CompilerOutput // which is // initialized with false, false, indicating // that the compiler wasn't invoked properly and that there was no // clean Compile. CompilerOutput compilerInvokeError = new CompilerOutput(); compilerInvokeError.setClean(false); compilerInvokeError.setCompilerInvoked(false); return compilerInvokeError; } // Now we read compiler output. If everything is ok javac reports // nothing at all. InputStream compilerOutputStream = compilerProcess.getErrorStream(); InputStreamReader compilerStreamReader = new InputStreamReader(compilerOutputStream); BufferedReader compilerOutputBuffer = new BufferedReader(compilerStreamReader); String line; CompilerOutput compilerOutput = new CompilerOutput(); compilerOutput.setCompilerInvoked(true); List<String> compilerOutputLines = new LinkedList<>(); try { while ((line = compilerOutputBuffer.readLine()) != null) { compilerOutputLines.add(line); } compilerOutputStream.close(); compilerStreamReader.close(); compilerOutputBuffer.close(); compilerProcess.destroy(); } catch (IOException e) { // Reading might go wrong here if javac should unexpectedly // terminate LOGGER.severe("Could not read compiler ourput from its output stream." + " Aborting compile of: " + pathToSourceFolder.toString() + " Got message: " + e.getMessage()); compilerOutput.setClean(false); compilerOutput.setCompileStreamBroken(true); return compilerOutput; } splitCompilerOutput(compilerOutputLines, compilerOutput); if (compilerOutputLines.size() == 0) { compilerOutput.setClean(true); } return compilerOutput; }
From source file:nl.minvenj.pef.stream.LiveCapture.java
private static boolean checkConfiguration(final XMLConfiguration config) { final long MAX_FILE_SIZE = 1000; //Test if all fields are available. For now it exits per field. try {//from w ww . ja v a 2s . c o m //First all normal fields. The library field is optional, default to metal. final boolean live = config.getBoolean("live"); final String inputFile = config.getString("input"); if (!live) { if (inputFile == null) { throw new NoSuchElementException( "For offline use, the parameter 'input' file needs to be specified"); } File testInput = new File(inputFile); if (!testInput.exists()) { logger.severe("The specified input file " + testInput.toPath().toAbsolutePath().toString() + " does not exist."); return false; } if (!testInput.canRead()) { logger.severe("The user is not allowed to read the file"); } } // Will throw an error if conversion fails. config.getBoolean("remove_failing_packets"); config.getBoolean("remove_unknown_protocols"); config.getBoolean("checksum_reset"); config.getBoolean("timer", false); final String outputDir = config.getString("output_directory"); // The file size does not need to be specified. Therefore a default is provided. final long fileSize = config.getLong("file_size", 120); if (fileSize > MAX_FILE_SIZE) { logger.severe(" A file size over 1000 MB is not supported."); return false; } if (outputDir == null) { throw new NoSuchElementException("The output_directory parameter is not set"); } final Path path = Paths.get(outputDir); if (!Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) { logger.severe("The output directory configured is not a path."); return false; } if (!Files.isWritable(path)) { logger.severe("The selected output directory is not writable."); return false; } final String parseLibrary = config.getString("parse_library"); if (!parseLibrary.equals("metal")) { logger.severe("Only the metal library is currently implemented as parse library."); return false; } String[] packetInputLibraryOptions = { "metal", "jnetpcap" }; final String packetInputLibrary = config.getString("packets_input_library"); if (!Arrays.asList(packetInputLibraryOptions).contains(packetInputLibrary)) { logger.severe("Packet Input Library: " + packetInputLibrary + " is not implemented."); } // Test all algorithms and their parameters. testPseudonymizationSettings(config); } catch (NoSuchElementException e) { logger.severe("Input element is missing: " + e.getMessage()); return false; } catch (InvalidPathException e) { logger.severe("The path specified in the config file is invalid: " + e.getMessage()); return false; } catch (ConversionException e) { logger.severe("Conversion failed: " + e.getMessage()); return false; } catch (ClassNotFoundException e) { logger.severe("Error in creating a parameter: the class could not be found: " + e.getMessage()); return false; } catch (ConfigurationException e) { logger.severe(e.getMessage()); return false; } return true; }
From source file:de.teamgrit.grit.checking.compile.HaskellCompileChecker.java
/** * checkProgram invokes the Haskell compiler on a given file and reports * the output.//from w ww.j a v a 2 s . c om * * @param pathToProgramFile * Specifies the file or folder containing that should be * compiled. (accepts .lhs and .hs files) * @param compilerName * The compiler to be used (usually ghc). * @param compilerFlags * Additional flags to be passed to the compiler. * @throws FileNotFoundException * Is thrown when the file in pathToProgramFile cannot be * opened * @throws BadCompilerSpecifiedException * Is thrown when the given compiler cannot be called * @return A {@link CompilerOutput} that contains all compiler messages and * flags on how the compile run went. * @throws BadFlagException * When ghc doesn't recognize a flag, this exception is thrown. */ @Override public CompilerOutput checkProgram(Path pathToProgramFile, String compilerName, List<String> compilerFlags) throws FileNotFoundException, BadCompilerSpecifiedException, BadFlagException { Process compilerProcess = null; try { // create compiler invocation. List<String> compilerInvocation = createCompilerInvocation(pathToProgramFile, compilerName, compilerFlags); ProcessBuilder compilerProcessBuilder = new ProcessBuilder(compilerInvocation); // make sure the compiler stays in its directory. compilerProcessBuilder.directory(pathToProgramFile.getParent().toFile()); compilerProcess = compilerProcessBuilder.start(); // this will never happen because createCompilerInvocation never // throws this Exception. Throw declaration needs to be in method // declaration because of the implemented Interface although we // never use it in the HaskellCompileChecker } catch (CompilerOutputFolderExistsException e) { LOGGER.severe("A problem while compiling, which never should happen, occured" + e.getMessage()); } catch (BadCompilerSpecifiedException e) { throw new BadCompilerSpecifiedException(e.getMessage()); } catch (IOException e) { // If we cannot call the compiler we return a CompilerOutput // initialized with false, false, indicating // that the compiler wasn't invoked properly and that there was no // clean Compile. CompilerOutput compilerInvokeError = new CompilerOutput(); compilerInvokeError.setClean(false); compilerInvokeError.setCompilerInvoked(false); return compilerInvokeError; } // Now we read compiler output. If everything is ok ghc reports // nothing in the errorStream. InputStream compilerOutputStream = compilerProcess.getErrorStream(); InputStreamReader compilerStreamReader = new InputStreamReader(compilerOutputStream); BufferedReader compilerOutputBuffer = new BufferedReader(compilerStreamReader); String line; CompilerOutput compilerOutput = new CompilerOutput(); compilerOutput.setCompilerInvoked(true); List<String> compilerOutputLines = new LinkedList<>(); try { while ((line = compilerOutputBuffer.readLine()) != null) { compilerOutputLines.add(line); } // Errors are separated via an empty line (""). But after the // the last error the OutputBuffer has nothing more to write. // In order to recognize the last error we insert an empty String // at the end of the list. // Only needs to be done when there are errors. if (compilerOutputLines.size() != 0) { line = ""; compilerOutputLines.add(line); } compilerOutputStream.close(); compilerStreamReader.close(); compilerOutputBuffer.close(); compilerProcess.destroy(); } catch (IOException e) { // Reading might go wrong here if ghc should unexpectedly die LOGGER.severe("Error while reading from compiler stream."); compilerOutput.setClean(false); compilerOutput.setCompileStreamBroken(true); return compilerOutput; } // ghc -c generates a .o(object) and a .hi(haskell interface) file. // But we don't need those files so they can be deleted. // The generated files have the same name like our input file so we // can just exchange the file endings in order to get the // correct file paths for deletion if (Files.isDirectory(pathToProgramFile, LinkOption.NOFOLLOW_LINKS)) { // we use a file walker in order to find all files in the folder // and its subfolders RegexDirectoryWalker dirWalker = new RegexDirectoryWalker(".+\\.([Ll])?[Hh][Ss]"); try { Files.walkFileTree(pathToProgramFile, dirWalker); } catch (IOException e) { LOGGER.severe("Could not walk submission " + pathToProgramFile.toString() + " while building copiler invocation: " + e.getMessage()); } for (Path candidatePath : dirWalker.getFoundFiles()) { File candidateFile = candidatePath.toFile(); if (!candidateFile.isDirectory()) { String extension = FilenameUtils.getExtension(candidateFile.toString()); if (extension.matches("[Ll]?[Hh][Ss]")) { File ghcGeneratedObject = new File( FilenameUtils.removeExtension(candidateFile.toString()) + ".o"); File ghcGeneratedInterface = new File( FilenameUtils.removeExtension(candidateFile.toString()) + ".hi"); ghcGeneratedObject.delete(); ghcGeneratedInterface.delete(); } } } } else { String extension = FilenameUtils.getExtension(pathToProgramFile.toString()); if (extension.matches("[Ll]?[Hh][Ss]")) { File ghcGeneratedObject = new File( FilenameUtils.removeExtension(pathToProgramFile.toString()) + ".o"); File ghcGeneratedInterface = new File( FilenameUtils.removeExtension(pathToProgramFile.toString()) + ".hi"); ghcGeneratedObject.delete(); ghcGeneratedInterface.delete(); } } // if there are no errors there is no Output to handle if (compilerOutputLines.size() != 0) { compilerOutput = splitCompilerOutput(compilerOutputLines, compilerOutput); } else { compilerOutput.setClean(true); } return compilerOutput; }
From source file:com.garyclayburg.filesystem.WatchDir.java
@Override public void run() { try {// w w w . j a v a 2 s . com init(watchDir, recursive); for (;;) { log.debug("Listening for File Events..."); // wait for key to be signalled WatchKey key; try { key = watcher.take(); } catch (InterruptedException x) { return; } Path dir = keys.get(key); if (dir == null) { log.warn("WatchKey not recognized!! " + key); continue; } for (WatchEvent<?> event : key.pollEvents()) { WatchEvent.Kind kind = event.kind(); // TBD - provide example of how OVERFLOW event is handled if (kind == OVERFLOW) { continue; } // Context for directory entry event is the file name of entry WatchEvent<Path> ev = cast(event); Path name = ev.context(); Path child = dir.resolve(name); // print out event log.info("{}: {}", event.kind().name(), child); if (kind == ENTRY_CREATE || kind == ENTRY_MODIFY) { attributeService.reloadGroovyClass(child); } else if (kind == ENTRY_DELETE) { attributeService.removeGroovyClass(child); } // if directory is created, and watching recursively, then // register it and its sub-directories if (recursive && (kind == ENTRY_CREATE)) { try { if (Files.isDirectory(child, NOFOLLOW_LINKS)) { registerAll(child); } } catch (IOException x) { log.warn("Cannot register possible new directory for groovy changes: {}", child); } } } // reset key and remove from set if directory no longer accessible boolean valid = key.reset(); if (!valid) { keys.remove(key); // all directories are inaccessible if (keys.isEmpty()) { break; } } } log.info("Stopped listening for file events"); } catch (IOException e) { log.warn("Could not start File Watch service", e); } }
From source file:edu.ehu.galan.lite.model.Document.java
/** * Save results in a plain text file following the pattern.... topic + \tab + * Source_knowledge_id+ \tab + Source_knowledge_title * * @param pFolder - where you want to save the results * @param pDoc// w w w . j ava 2 s .c om */ public static void saveResults(String pFolder, Document pDoc) { if (Files.isDirectory(Paths.get(pFolder), LinkOption.NOFOLLOW_LINKS)) { PrintWriter printWriter = null; try { printWriter = new PrintWriter(new File(pFolder + "/" + pDoc.name), "UTF-8"); for (Topic topic : pDoc.topicList) { printWriter.println(topic.getTopic() + "\t" + topic.getId() + "\t" + topic.getSourceTitle()); } printWriter.close(); } catch (FileNotFoundException | UnsupportedEncodingException ex) { logger.error("error printing the file: " + pFolder, ex); } logger.info(pDoc.path + " Saved... "); } else if (Files.exists(Paths.get(pFolder), LinkOption.NOFOLLOW_LINKS)) { logger.error("The folder exists but it isn't a directory...maybe a file?"); } else { logger.warn("The directory doesn't exist... will be created"); try { FileUtils.forceMkdir(new File(pFolder)); PrintWriter printWriter = null; printWriter = new PrintWriter(new File(pFolder + "/" + pDoc.name), "UTF-8"); for (Topic topic : pDoc.topicList) { printWriter.println(topic.getTopic() + "\t" + topic.getId() + "\t" + topic.getSourceTitle()); } printWriter.close(); logger.info(pDoc.path + " Saved... "); } catch (FileNotFoundException | UnsupportedEncodingException ex) { logger.error("error printing the file: " + pFolder, ex); } catch (IOException ex) { logger.error("Error while creating directories in or printing the file: " + pFolder, ex); } } }