private boolean checkDirectoryValidity (String directory) { File checkFile = new File(directory); boolean path = true; // try to get the canonical path, if this fails the path is not valid try { checkFile.getCanonicalPath(); } catch (Exception e) { path = false; } return path; }
private static String toString(File path) { try { return path.getCanonicalPath(); } catch(IOException e) { return path.getAbsolutePath(); } } }
public static String currentWorkingDirectory() { String location; File file = new File("."); try { location = file.getCanonicalPath(); } catch (IOException e) { location = file.getAbsolutePath(); } return location; }
public static boolean isSymbolicLink(File parent, String name) throws IOException { if (parent == null) { File f = new File(name); parent = f.getParentFile(); name = f.getName(); } File toTest = new File(parent.getCanonicalPath(), name); return !toTest.getAbsolutePath().equals(toTest.getCanonicalPath()); }
System.out.println(new File(".").getCanonicalPath());
/** * Return a metadata file (worker.yaml) for given worker log directory. * @param logDir worker log directory */ public Optional<File> getMetadataFileForWorkerLogDir(File logDir) throws IOException { File metaFile = new File(logDir, WORKER_YAML); if (metaFile.exists()) { return Optional.of(metaFile); } else { LOG.warn("Could not find {} to clean up for {}", metaFile.getCanonicalPath(), logDir); return Optional.empty(); } }
public static void main(String[] args) throws IOException { // find the file in the file system.. probably not a good idea File f = new File("images/test.jpg"); System.out.println(f.getCanonicalPath()+" "+f.exists());
import java.io.File; public class PathTesting { public static void main(String [] args) { File f = new File("test/.././file.txt"); System.out.println(f.getPath()); System.out.println(f.getAbsolutePath()); try { System.out.println(f.getCanonicalPath()); } catch(Exception e) {} } }
private void verifyLogFileContents(Set<String> acceptedLogLevels, File dirForMember) throws IOException { String memberName = dirForMember.getName(); MemberVM member = expectedMessages.keySet().stream() .filter((Member aMember) -> aMember.getName().equals(memberName)).findFirst().get(); assertThat(member).isNotNull(); Set<String> fileNamesInDir = Stream.of(dirForMember.listFiles()).map(File::getName).collect(toSet()); System.out.println(dirForMember.getCanonicalPath() + " : " + fileNamesInDir); File logFileForMember = new File(dirForMember, memberName + ".log"); assertThat(logFileForMember).exists(); String logFileContents = FileUtils.readLines(logFileForMember, Charset.defaultCharset()) .stream().collect(joining("\n")); for (LogLine logLine : expectedMessages.get(member)) { boolean shouldExpectLogLine = acceptedLogLevels.contains(logLine.level) && !logLine.shouldBeIgnoredDueToTimestamp; if (shouldExpectLogLine) { assertThat(logFileContents).contains(logLine.getMessage()); } else { assertThat(logFileContents).doesNotContain(logLine.getMessage()); } } }
protected static String getAbsolutePath(String path) throws IOException { File file = new File(path); if (file.isDirectory() && (path.endsWith(".xcodeproj/") || path.endsWith(".xcodeproj"))) { return file.getCanonicalPath(); } else { throw new IOException(path + " is not a valid .xcodeproj"); } }
if (!directory.isDirectory()) { throw new IllegalArgumentException("Not a directory: " + directory); if (!directory.exists() || !child.exists()) { return false; final String canonicalParent = directory.getCanonicalPath(); final String canonicalChild = child.getCanonicalPath();
public boolean delete () { if (!exists()) { return false; } LocalStorage.removeItem(getCanonicalPath()); return true; }
private void renameJarWithOldNamingConvention(File oldJar) throws IOException { Matcher matcher = oldNamingPattern.matcher(oldJar.getName()); if (!matcher.matches()) { throw new IllegalArgumentException("The given jar " + oldJar.getCanonicalPath() + " does not match the old naming convention"); } String unversionedJarNameWithoutExtension = matcher.group(1); String jarVersion = matcher.group(2); String newJarName = unversionedJarNameWithoutExtension + ".v" + jarVersion + ".jar"; File newJar = new File(this.deployDirectory, newJarName); logger.debug("Renaming deployed jar from {} to {}", oldJar.getCanonicalPath(), newJar.getCanonicalPath()); FileUtils.moveFile(oldJar, newJar); }
private static void createTempDirWithSpecialCharactersInName() throws IOException { if ( !TEMP_DIR_WITH_REP_FILE.exists() ) { if ( TEMP_DIR_WITH_REP_FILE.mkdir() ) { System.out.println( "CREATED: " + TEMP_DIR_WITH_REP_FILE.getCanonicalPath() ); } else { System.out.println( "NOT CREATED: " + TEMP_DIR_WITH_REP_FILE.toString() ); } } }
private int execute(File dir) { int fails = 0; File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { File f = files[i]; if (f.isDirectory()) { fails += execute(f); } else if (f.getName().endsWith(".class")) { try { boolean ok = readClass(f.getCanonicalPath()); if (!ok) fails++; } catch (IOException ioe) { log(ioe.getMessage()); throw new BuildException(ioe); } } } return fails; }
private void importOnMember(File snapshot, SnapshotFormat format, SnapshotOptions<K, V> options) throws IOException, ClassNotFoundException { final LocalRegion local = getLocalRegion(region); if (getLogger().infoEnabled()) getLogger().info(String.format("Importing region %s", region.getName())); if (snapshot.isDirectory()) { File[] snapshots = snapshot.listFiles((File f) -> f.getName().endsWith(SNAPSHOT_FILE_EXTENSION)); if (snapshots == null) { throw new IOException("Unable to access " + snapshot.getCanonicalPath()); } else if (snapshots.length == 0) { throw new IllegalArgumentException("Failure to import snapshot: " + snapshot.getAbsolutePath() + " contains no valid .gfd snapshot files"); } for (File snapshotFile : snapshots) { importSnapshotFile(snapshotFile, options, local); } } else if (snapshot.getName().endsWith(SNAPSHOT_FILE_EXTENSION)) { importSnapshotFile(snapshot, options, local); } else { throw new IllegalArgumentException("Failure to import snapshot: " + snapshot.getCanonicalPath() + " is not .gfd file or directory containing .gfd files"); } }
private File[] getSnapshotFiles(File dir) throws IOException { File[] snapshotFiles = dir.listFiles(pathname -> pathname.getName().endsWith(".gfd")); if (snapshotFiles == null) { throw new IOException("Unable to access " + dir.getCanonicalPath()); } else if (snapshotFiles.length == 0) { throw new FileNotFoundException( String.format("No snapshot files found in %s", dir)); } return snapshotFiles; }
/** Delete a folder and all its content or throw. Don't follow symlinks. */ static void deleteFolder(File fileOrDirectory) throws IOException { if (fileOrDirectory.getCanonicalPath().equals(fileOrDirectory.getAbsolutePath()) && fileOrDirectory.isDirectory()) { File[] children = fileOrDirectory.listFiles(); if (children != null) { for (File child : children) { deleteFolder(child); } } } if (!fileOrDirectory.delete()) { throw new RuntimeException("Unable to delete " + (fileOrDirectory.isDirectory() ? "directory " : "file ") + fileOrDirectory.getAbsolutePath()); } }
@Override public WatchedResource watch( File file ) throws IOException { if ( !file.isDirectory() ) { throw new IllegalArgumentException( format( "File `%s` is not a directory. Only directories can be " + "registered to be monitored.", file.getCanonicalPath() ) ); } WatchKey watchKey = file.toPath().register( watchService, OBSERVED_EVENTS, SensitivityWatchEventModifier.HIGH ); return new WatchedFile( watchKey ); }
/** * Determines if the app is running in a portable mode. i.e. on a USB stick */ private static boolean portableMode() { try { File file = new File(new File(".").getCanonicalPath() + File.separator + CONFIG_FILE); if (file.exists() && !file.isDirectory()) { return true; } } catch (IOException e) { return false; } return false; }