Tabnine Logo
File.listFiles
Code IndexAdd Tabnine to your IDE (free)

How to use
listFiles
method
in
java.io.File

Best Java code snippets using java.io.File.listFiles (Showing top 20 results out of 60,615)

Refine searchRefine arrow

  • File.<init>
  • File.isDirectory
  • File.getName
  • File.exists
  • File.getAbsolutePath
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;
  }
}
java.ioFilelistFiles

Javadoc

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.

Popular methods of File

  • <init>
    Creates a new File instance by converting the givenfile: URI into an abstract pathname. The exact fo
  • exists
    Tests whether the file or directory denoted by this abstract pathname exists.
  • getAbsolutePath
    Returns the absolute pathname string of this abstract pathname. If this abstract pathname is already
  • getName
    Returns the name of the file or directory denoted by this abstract pathname. This is just the last n
  • isDirectory
  • mkdirs
  • delete
    Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a director
  • getParentFile
    Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not
  • getPath
    Converts this abstract pathname into a pathname string. The resulting string uses the #separator to
  • isFile
  • length
    Returns the length of the file denoted by this abstract pathname. The return value is unspecified if
  • toURI
  • length,
  • toURI,
  • createTempFile,
  • createNewFile,
  • toPath,
  • mkdir,
  • lastModified,
  • toString,
  • getCanonicalPath

Popular in Java

  • Start an intent from android
  • scheduleAtFixedRate (Timer)
  • runOnUiThread (Activity)
  • getSharedPreferences (Context)
  • Collection (java.util)
    Collection is the root of the collection hierarchy. It defines operations on data collections and t
  • Pattern (java.util.regex)
    Patterns are compiled regular expressions. In many cases, convenience methods such as String#matches
  • HttpServletRequest (javax.servlet.http)
    Extends the javax.servlet.ServletRequest interface to provide request information for HTTP servlets.
  • Response (javax.ws.rs.core)
    Defines the contract between a returned instance and the runtime when an application needs to provid
  • LogFactory (org.apache.commons.logging)
    Factory for creating Log instances, with discovery and configuration features similar to that employ
  • SAXParseException (org.xml.sax)
    Encapsulate an XML parse error or warning.> This module, both source code and documentation, is in t
  • Top plugins for WebStorm
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now