本文整理了Java中java.io.File.listFiles()
方法的一些代码示例,展示了File.listFiles()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。File.listFiles()
方法的具体详情如下:
包路径:java.io.File
类名称:File
方法名:listFiles
[英]Returns an array of files contained in the directory represented by this file. The result is null if this file is not a directory. The paths of the files in the array are absolute if the path of this file is absolute, they are relative otherwise.
[中]返回包含在此文件表示的目录中的文件数组。如果此文件不是目录,则结果为null。数组中文件的路径为绝对路径如果此文件的路径为绝对路径,则为相对路径。
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();
}
代码示例来源:origin: google/guava
private static Iterable<File> fileTreeChildren(File file) {
// check isDirectory() just because it may be faster than listFiles() on a non-directory
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
return Collections.unmodifiableList(Arrays.asList(files));
}
}
return Collections.emptyList();
}
代码示例来源:origin: stackoverflow.com
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());
}
}
代码示例来源:origin: stackoverflow.com
String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
}
代码示例来源:origin: libgdx/libgdx
static private boolean deleteDirectory (File file) {
if (file.exists()) {
File[] files = file.listFiles();
if (files != null) {
for (int i = 0, n = files.length; i < n; i++) {
if (files[i].isDirectory())
deleteDirectory(files[i]);
else
files[i].delete();
}
}
}
return file.delete();
}
代码示例来源:origin: libgdx/libgdx
public static void main (String[] args) {
TexturePacker.Settings settings = new TexturePacker.Settings();
settings.scale = new float[] {4};
settings.scaleResampling = new TexturePacker.Resampling[] {TexturePacker.Resampling.nearest};
TexturePacker packer = new TexturePacker(settings);
packer.addImage(new File("tests/gdx-tests-gwt/war/assets/data/bobrgb888-32x32.png"));
File out = new File("tmp/packout");
// Create or clean up packout directory
if (out.exists()) {
for (File f : out.listFiles()) {
f.delete();
}
} else {
out.mkdirs();
}
packer.pack(out, "main");
}
}
代码示例来源:origin: apache/storm
private List<String> listDir(String dir) throws IOException {
List<String> ret = new ArrayList<String>();
File[] contents = new File(dir).listFiles();
if (contents != null) {
for (File f : contents) {
ret.add(f.getAbsolutePath());
}
}
return ret;
}
}
代码示例来源:origin: redisson/redisson
/**
* Creates a new iterator representation for all files within a folder.
*
* @param folder The root folder.
*/
protected FolderIterator(File folder) {
files = new LinkedList<File>(Collections.singleton(folder));
File candidate;
do {
candidate = files.removeFirst();
File[] file = candidate.listFiles();
if (file != null) {
files.addAll(0, Arrays.asList(file));
}
} while (!files.isEmpty() && (files.peek().isDirectory() || files.peek().equals(new File(folder, JarFile.MANIFEST_NAME))));
}
代码示例来源:origin: lets-blade/blade
/**
* Filter the file rules
*
* @param file
* @param recursive
* @return
*/
private File[] accept(File file, final boolean recursive) {
// Custom filtering rules If you can loop (include subdirectories) or is the end of the file. Class (compiled java class file)
return file.listFiles(file1 -> (recursive && file1.isDirectory()) || (file1.getName().endsWith(".class")));
}
代码示例来源:origin: libgdx/libgdx
/** 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);
}
代码示例来源:origin: libgdx/libgdx
/** Looks for subdirectories inside parentHandle, processes maps in subdirectory, repeat.
* @param currentDir The directory to look for maps and other directories
* @throws IOException */
private void processSubdirectories (FileHandle currentDir, Settings texturePackerSettings) throws IOException {
File parentPath = new File(currentDir.path());
File[] directories = parentPath.listFiles(new DirFilter());
for (File directory : directories) {
currentDir = new FileHandle(directory.getCanonicalPath());
File[] mapFilesInCurrentDir = directory.listFiles(new TmxFilter());
for (File mapFile : mapFilesInCurrentDir) {
processSingleMap(mapFile, currentDir, texturePackerSettings);
}
processSubdirectories(currentDir, texturePackerSettings);
}
}
代码示例来源:origin: facebook/stetho
public void cleanupFiles() {
for (File file : mContext.getFilesDir().listFiles()) {
if (file.getName().startsWith(FILENAME_PREFIX)) {
if (!file.delete()) {
LogRedirector.w(TAG, "Failed to delete " + file.getAbsolutePath());
}
}
}
LogRedirector.i(TAG, "Cleaned up temporary network files.");
}
代码示例来源:origin: apache/pulsar
public static void deleteFile(final File file, final boolean recurse) throws IOException {
final File[] list = file.listFiles();
if (file.isDirectory() && recurse && list != null) {
FileUtils.deleteFiles(Arrays.asList(list), recurse);
}
//now delete the file itself regardless of whether it is plain file or a directory
if (!FileUtils.deleteFile(file, null, 5)) {
throw new IOException("Unable to delete " + file.getAbsolutePath());
}
}
代码示例来源:origin: eclipse-vertx/vert.x
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;
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Determine a sorted list of files in the given directory.
* @param dir the directory to introspect
* @return the sorted list of files (by default in alphabetical order)
* @since 5.1
* @see File#listFiles()
*/
protected File[] listDirectory(File dir) {
File[] files = dir.listFiles();
if (files == null) {
if (logger.isInfoEnabled()) {
logger.info("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]");
}
return new File[0];
}
Arrays.sort(files, Comparator.comparing(File::getName));
return files;
}
代码示例来源:origin: stackoverflow.com
public void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(fileEntry.getName());
}
}
}
final File folder = new File("/home/you/Desktop");
listFilesForFolder(folder);
代码示例来源:origin: libgdx/libgdx
static private boolean deleteDirectory (File file) {
if (file.exists()) {
File[] files = file.listFiles();
if (files != null) {
for (int i = 0, n = files.length; i < n; i++) {
if (files[i].isDirectory())
deleteDirectory(files[i]);
else
files[i].delete();
}
}
}
return file.delete();
}
代码示例来源:origin: stackoverflow.com
File folder = new File("/Users/you/folder/");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
System.out.println(file.getName());
}
}
代码示例来源:origin: libgdx/libgdx
public static void main (String[] args) {
TexturePacker.Settings settings = new TexturePacker.Settings();
settings.scale = new float[] {4};
settings.scaleResampling = new TexturePacker.Resampling[] {TexturePacker.Resampling.nearest};
TexturePacker packer = new TexturePacker(settings);
packer.addImage(new File("tests/gdx-tests-gwt/war/assets/data/bobrgb888-32x32.png"));
File out = new File("tmp/packout");
// Create or clean up packout directory
if (out.exists()) {
for (File f : out.listFiles()) {
f.delete();
}
} else {
out.mkdirs();
}
packer.pack(out, "main");
}
}
代码示例来源:origin: alibaba/jstorm
private List<String> listDir(String dir) throws IOException {
List<String> ret = new ArrayList<>();
File[] contents = new File(dir).listFiles();
if (contents != null) {
for (File f : contents) {
ret.add(f.getAbsolutePath());
}
}
return ret;
}
}
内容来源于网络,如有侵权,请联系作者删除!