[Resolvido] Verificar existência de arquivos

Amigos,

Construí o método abaixo par fazer upload de determinado arquivo para o ftp.

A única coisa que preciso saber agora é como verificar se o diretório que vou pegar o está vazio.

Alguém pode me dar uma luz.

Segue o método.


package br.com.mylims.auxiliares;

import java.io.FileInputStream;

import org.apache.commons.net.ftp.FTPClient;

import android.util.Log;

public class UploadFtp {
    
    private static final String CATEGORIA = "coleta";

    public boolean uploadFTP(String ipFTP, String loginFTP, String senhaFTP, String diretorioFTP, String diretorioAndroid, String arquivoFTP){
    
        try {
            
            FTPClient ftp = new FTPClient();
            
            ftp.connect(ipFTP);
            ftp.login(loginFTP, senhaFTP);
            
            ftp.enterLocalPassiveMode();
               
            ftp.setFileTransferMode(FTPClient.ASCII_FILE_TYPE);
            ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
            
            ftp.changeWorkingDirectory(diretorioFTP);
            
            FileInputStream arqEnviar = new FileInputStream( diretorioAndroid + arquivoFTP);
            
            if (ftp.storeFile(arquivoFTP, arqEnviar)) {
                
                ftp.logout();
                ftp.disconnect();
                
                return true;
                
            } else {
                
                ftp.logout();
                ftp.disconnect();

                return false;
            }
            
            
        } catch (Exception e) {
            
            Log.i(CATEGORIA, "ERRO FTP: " + e);
            return false;
            
        }
        
    }
    
    
}

Boa tarde

Usa um list e verifica seu retorno, se retornar null e por que o diretorio está vazio.

http://docs.oracle.com/javase/1.5.0/docs/api/java/io/File.html

public String[] list()
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of strings is returned, one for each file or directory in the directory. Names denoting the directory itself and the directory’s parent directory are not included in the result. Each string is a file name rather than a complete path.

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Returns:
An array of strings naming the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
Throws:
SecurityException - If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the directory

File dir = new File("/mnt/sdcard/MINHA_PASTA");
if(dir.exist() || dir.mkdirs()) {

   String[] arquivos = dir.list();
   if(arquivos != null && arquivos.length() > 0) {
      // Existem arquivos na pasta...
   
   } else {
      // Não existem arquivos...
   }

}

Pessoal resolvi desta forma.


public boolean uploadFTP(String ipFTP, String loginFTP, String senhaFTP, String diretorioFTP, String diretorioAndroid, String arquivoFTP){

		try {

			File file = new File(diretorioAndroid);
			File file2 = new File(diretorioAndroid + arquivoFTP);

			if (file.isDirectory()) {

				if(file.list().length > 0){

					FTPClient ftp = new FTPClient();

					ftp.connect(ipFTP);
					ftp.login(loginFTP, senhaFTP);

					ftp.enterLocalPassiveMode();

					ftp.setFileTransferMode(FTPClient.ASCII_FILE_TYPE);
					ftp.setFileType(FTPClient.ASCII_FILE_TYPE);

					ftp.changeWorkingDirectory(diretorioFTP);

					FileInputStream arqEnviar = new FileInputStream( diretorioAndroid + arquivoFTP);

					if (ftp.storeFile(arquivoFTP, arqEnviar)) {

						ftp.logout();
						ftp.disconnect();

						if (file2.delete()) {

							Log.i(CATEGORIA, "Arquivo " + arquivoFTP + " excluído com sucesso");
							return true;

						} else {

							Log.i(CATEGORIA, "Erro ao excluir o arquivo " + arquivoFTP);
							return false;

						}

					} else {

						ftp.logout();
						ftp.disconnect();

						Log.i(CATEGORIA, "ERRO: arquivo " + arquivoFTP + "não foi enviado!");

						return false;
					}


				}
				else
				{
					Log.i(CATEGORIA, "Não existe o arquivo " + arquivoFTP + "neste diretório!");

					return false;
				}

			}
			else
			{

				Log.i(CATEGORIA, "Não é diretório");
				return false;

			}

		} catch (Exception e) {

			Log.i(CATEGORIA, "ERRO FTP: " + e);
			return false;

		}

	}

Valeu pela força.