GuilhermeRamalho

Listando Arquivos

Mar 30th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.nanothings.files;
  7.  
  8. import java.io.File;
  9. import javax.swing.JOptionPane;
  10.  
  11. /**
  12.  *
  13.  * @author guilherme
  14.  */
  15. public class ListandoDir {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public static void main(String[] args) {
  21.         // TODO code application logic here
  22.        
  23.         String dir;
  24.        
  25.         dir = JOptionPane.showInputDialog("Digite o diretório a ser pesquisado: ");
  26.        
  27.         /*
  28.         Caso queira definir o diretório por padrão é só comentar o JOptionPane.
  29.         Obs: Mudar o diretório se o SO for Windows ou somento o usuário caso seja Linux.
  30.         dir = "/home/guilherme";
  31.         */
  32.         File diretorio = new File(dir);
  33.         File[] arquivos = diretorio.listFiles();
  34.         int qtd = arquivos.length;
  35.        
  36.         System.out.println("Número de arquivos e diretório: "+ qtd);
  37.        
  38.         if(arquivos != null)
  39.         {
  40.             for(int i=0 ; i<qtd ; ++i)
  41.             {
  42.                 File f = arquivos[i];
  43.                
  44.                 if(f.isFile())
  45.                 {
  46.                     System.out.println(f.getName()+" é pasta.");
  47.                 }
  48.                 else if(f.isDirectory())
  49.                 {
  50.                     System.out.println(f.getName()+" é diretório.");
  51.                 }
  52.                 else
  53.                 {
  54.                     System.out.println("Essa bagaça nem é pasta nem arquivo! :D");
  55.                 }
  56.             }
  57.         }
  58.         else
  59.         {
  60.             System.out.println("Pasta vazia!");
  61.         }
  62.    }    
  63. }
Advertisement
Add Comment
Please, Sign In to add comment