canonical example by Tabnine
public long getDirectorySize(File file) { if (!file.exists()) { return 0; } if (file.isFile()) { return file.length(); } File[] files; if (!file.isDirectory() || (files = file.listFiles()) == null) { return 0; } return Arrays.stream(files).mapToLong(f -> getDirectorySize(f)).sum(); }
File folder = new File("your/path"); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { System.out.println("File " + listOfFiles[i].getName()); } else if (listOfFiles[i].isDirectory()) { System.out.println("Directory " + listOfFiles[i].getName()); } }
public int compare(File file1, File file2) { if (file1.isFile() && file2.isDirectory()) { return order; } if (file1.isDirectory() && file2.isFile()) { return -order; } return 0; } }
private File getJava() { File java; File home = new File(System.getProperty("java.home")); if (ExecUtils.isWindows()) { java = new File(home, "bin/java.exe"); } else { java = new File(home, "bin/java"); } if (!java.isFile()) { out.println("Cannot find java executable - " + java.getAbsolutePath() + " does not exist"); ExecUtils.exitBecauseOfSystemConfigurationIssue(); } return java; }
@Override boolean isRepositoryFor(File file, boolean interactive) { if (file.isDirectory()) { File f = new File(file, MYSCMSERVERINFO_FILE); return f.exists() && f.isFile(); } return false; }
@Test public void shouldZipFileAndUnzipIt() throws IOException { zipFile = zipUtil.zip(srcDir, temporaryFolder.newFile(), Deflater.NO_COMPRESSION); assertThat(zipFile.isFile(), is(true)); zipUtil.unzip(zipFile, destDir); File baseDir = new File(destDir, srcDir.getName()); assertIsDirectory(new File(baseDir, emptyDir.getName())); assertIsDirectory(new File(baseDir, childDir1.getName())); File actual1 = new File(baseDir, file1.getName()); assertThat(actual1.isFile(), is(true)); assertThat(fileContent(actual1), is(fileContent(file1))); File actual2 = new File(baseDir, childDir1.getName() + File.separator + file2.getName()); assertThat(actual2.isFile(), is(true)); assertThat(fileContent(actual2), is(fileContent(file2))); }
public static final boolean deleteDir(File file) { if (file == null || (!file.exists())) { return false; } if (file.isFile()) { file.delete(); } else if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { deleteDir(files[i]); } } file.delete(); return true; }
/** * Returns the log file. * @return The file may reference both uncompressed or compressed logs */ public @Nonnull File getLogFile() { File rawF = new File(getRootDir(), "log"); if (rawF.isFile()) { return rawF; } File gzF = new File(getRootDir(), "log.gz"); if (gzF.isFile()) { return gzF; } //If both fail, return the standard, uncompressed log file return rawF; }
/** Processes the specified input file or directory. * @param outputRoot May be null if there is no output from processing the files. * @return the processed files added with {@link #addProcessedFile(Entry)}. */ public ArrayList<Entry> process (File inputFileOrDir, File outputRoot) throws Exception { if (!inputFileOrDir.exists()) throw new IllegalArgumentException("Input file does not exist: " + inputFileOrDir.getAbsolutePath()); if (inputFileOrDir.isFile()) return process(new File[] {inputFileOrDir}, outputRoot); else return process(inputFileOrDir.listFiles(), outputRoot); }
/** * Returns the class file for the given class (which has been verified to exist in the returned location), * or null if the class file could not be found (e.g. because it is contained in a Jar). */ public static File getClassFile(Class<?> clazz) { File dir = new File(clazz.getProtectionDomain().getCodeSource().getLocation().getPath()); if (!dir.isDirectory()) return null; // class file might be contained in Jar File clazzFile = new File(dir, clazz.getName().replace('.', File.separatorChar) + ".class"); return clazzFile.isFile() ? clazzFile : null; }
private static void mockFiles( String[] filenames, ArrayList<File> files, boolean isDirectories ) { for ( String filename : filenames ) { File file = mock( File.class ); String[] fileNameParts = filename.split( "/" ); when( file.getName() ).thenReturn( fileNameParts[fileNameParts.length - 1] ); when( file.isFile() ).thenReturn( !isDirectories ); when( file.isDirectory() ).thenReturn( isDirectories ); when( file.exists() ).thenReturn( true ); when( file.getPath() ).thenReturn( filename ); files.add( file ); } }
private static List<JavaFileObject> browseDir(String packageName, File directory) { List<JavaFileObject> result = new ArrayList<>(); for (File childFile : directory.listFiles()) { if (childFile.isFile() && childFile.getName().endsWith(CLASS_FILE)) { String binaryName = packageName + "." + childFile.getName().replaceAll(CLASS_FILE + "$", ""); result.add(new CustomJavaFileObject(childFile.toURI(), JavaFileObject.Kind.CLASS, binaryName)); } } return result; }
private void checkFilesExist(Collection<File> dependencies) { for (File dependency : dependencies) { if (!dependency.isFile() || !dependency.exists()) { throw new FileNotAvailableException(dependency.getAbsolutePath()); } } } }
public static FileInfo from(File file) { FileInfo info = new FileInfo(); info.setName(file.getName()); info.setLength(file.length()); info.setParent(file.getParent()); info.setAbsolutePath(file.getAbsolutePath()); info.setFile(file.isFile()); info.setDir(file.isDirectory()); return info; } }
public int compare(File file1, File file2) { if (file1.isDirectory() && file2.isDirectory() || file1.isFile() && file2.isFile()) { return file1.getName().compareTo(file2.getName()); } else { return file1.isDirectory() ? -1 : 1; } } }
public static void delete(File file) throws IOException { if (file.isDirectory()) { for (File f : file.listFiles()) { delete(f); } } if (file.isDirectory() || file.isFile()) { if (!file.delete()) { throw new IllegalStateException("Deletion of " + file.getPath() + " failed"); } } }