Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Directory' - 7 code snippet(s) found

 Sample 1. Method to get all files from a directory recursively

List<File> fileList = new ArrayList();

List<File> read(String dir) throws IOException{
File directory = new File(dir);
File[] fList = directory.listFiles();
for(File file:fList){
if(file.isDirectory()){
read(file.getPath());
} else {
fileList.add(file);
}
}
return fileList;
}

   Like      Feedback     file handling   file   isDirectory()  .getPath()   recursion   java.io.File


 Sample 2. Method to get specific format files from a directory

private Collection<File> getDocAndTextFiles() {
File directory = new File("C:DocDir");
if (directory.exists() && directory.isDirectory()) {
Collection<File> files = FileUtils.listFiles(directory, new String[] { "doc","txt" }, false);
}
return files;
}

   Like      Feedback     file handling  file  directory  FileUtils.listFiles  fileutils


 Sample 3. Create a new Directory

File dir = new File("c:xyz");
boolean status = file.mkdir();

   Like      Feedback     file handling  creating new directory  file.mkdir


 Sample 4. Method to check if the the file exists and not a directory

public static boolean isOleFile(File file)
{
   if ((file == null) || (!file.exists()) || (file.isDirectory())) {
      return false;
   }

   return true;
}

   Like      Feedback     file handling  check if file exists  File class  check if file is a directory  file.isDirectory  file.exists


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 5. Code Sample / Example / Snippet of org.apache.hadoop.hdfs.server.common.Storage.StorageDirectory

  public File getFsEditName() throws IOException {

return getEditLog().getFsEditName();

}



File getFsTimeName() {

StorageDirectory sd = null;

for (Iterator<StorageDirectory> it =

dirIterator(); it.hasNext();)

sd = it.next();

return getImageFile(sd, NameNodeFile.TIME);

}


   Like      Feedback      org.apache.hadoop.hdfs.server.common.Storage.StorageDirectory


 Sample 6. Get only files from a directory using FileFilter

File dir = new File("C:/Folder");

File[] files = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isFile();
}
});

   Like      Feedback     FileFilter


 Sample 7. Get only sub directories in a directory using FileFilter

File dir = new File("C:/Folder");

File[] subdir = dir.listFiles(new FileFilter() {
   @Override
   public boolean accept(File file) {
      return file.isDirectory();
   }
});

   Like      Feedback     FileFilter



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus