Android; Compruebe si el archivo existe sin crear uno nuevo


Quiero comprobar si el archivo existe en mi carpeta de paquetes, pero no quiero crear uno nuevo.

File file = new File(filePath);
if(file.exists()) 
     return true;

¿Comprueba este código sin crear un nuevo archivo?

Author: Kevin Guan, 2013-04-26

6 answers

Su fragmento de código no crea uno nuevo, solo comprueba si ya está allí y nada más.

File file = new File(filePath);
if(file.exists())      
//Do something
else
// Do something else.
 356
Author: Maikel Bollemeijer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-08 15:51:08

Cuando usa este código, no está creando un nuevo archivo, solo está creando una referencia de objeto para ese archivo y probando si existe o no.

File file = new File(filePath);
if(file.exists()) 
    //do something
 23
Author: Victor Laerte,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-28 03:08:21

Funcionó para mí:

File file = new File(getApplicationContext().getFilesDir(),"whatever.txt");
    if(file.exists()){
       //Do something
    }
    else{
       //Nothing
     }
 6
Author: Jordi Vicens,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-29 11:20:04

Cuando dices "en tu carpeta de paquetes", ¿te refieres a tus archivos de aplicaciones locales? Si es así, puede obtener una lista de ellos usando el contexto .Método FileList () . Simplemente itera y busca tu archivo. Eso es asumiendo que guardaste el archivo original con el contexto .openFileOutput () .

Código de ejemplo (en una actividad):

public void onCreate(...) {
    super.onCreate(...);
    String[] files = fileList();
    for (String file : files) {
        if (file.equals(myFileName)) {
            //file exits
        }
    }
}
 5
Author: thomas88wp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-24 22:00:36

Los methods en la clase Path son sintácticos, lo que significa que operan en la instancia Path. Pero eventualmente debe acceder al sistema file para verificar que existe una ruta en particular

 File file = new File("FileName");
 if(file.exists()){
 System.out.println("file is already there");
 }else{
 System.out.println("Not find file ");
 }
 2
Author: Anand Dwivedi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-01 09:49:41
public boolean FileExists(String fname) {
        File file = getBaseContext().getFileStreamPath(fname);
        return file.exists();
}
 0
Author: HomieZ2,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-14 04:08:49