如何检查android中是否存在txt文件

如何检查android中是否存在txt文件,android,Android,我想要的是检查文本文件是否存在的代码和平。我正在使用java 我想要的是这样的东西: if (the file.txt exists) { do a action } else {do another action } 该文件位于应用程序数据文件夹的文件目录中。谢谢P 编辑:我按如下方式保存文件: public class crear_data_mes { String nArchivo_mes = "mes.txt"; Context ctx_mes; FileOu

我想要的是检查文本文件是否存在的代码和平。我正在使用java

我想要的是这样的东西:

if (the file.txt exists)  { do a action } else {do another action }
该文件位于应用程序数据文件夹的文件目录中。谢谢P

编辑:我按如下方式保存文件:

public class crear_data_mes {
    String nArchivo_mes = "mes.txt";
    Context ctx_mes;
    FileOutputStream fos_mes;
    FileInputStream fis_mes;
    public crear_data_mes(Context ctx) {
        this.ctx_mes=ctx;
    }
    public void escribir_mes(String textoarchivo_mes) {
        try {
            fos_mes= ctx_mes.openFileOutput(nArchivo_mes, Context.MODE_PRIVATE);
            fos_mes.write(textoarchivo_mes.getBytes());
            fos_mes.close();
        } catch (FileNotFoundException e) {
            Log.e("Hola", "Hola" + e.getMessage());
        } catch (IOException ex) {
            Log.e("Hola", "Hola"+ex.getMessage());
        }
    }

    public String leer_mes () {
        String lectura_mes = "";
        try {

            fis_mes = ctx_mes.openFileInput(nArchivo_mes);
            int i;
            int n_mes = 0;
            char caracter_mes='a';
            do {
                i = fis_mes.read();
                if (i!='\n') {
                    caracter_mes = (char)i;
                    lectura_mes=lectura_mes+caracter_mes;
                }
            } while (i>0);
            lectura_mes+="\n";
        } catch (Exception e) { }
        return lectura_mes;
    }
}
文件
具有满足您需要的方法

File file = new File(fileDirectory, "file.txt");
if (file.exists()) {

}
其中
fileDirectory
是存储文件的目录

编辑:

在您的示例中,可以使用,它返回文件系统上存储用openFileOutput(String,int)创建的文件的绝对路径

例如

文件
具有满足您需要的方法

File file = new File(fileDirectory, "file.txt");
if (file.exists()) {

}
其中
fileDirectory
是存储文件的目录

编辑:

在您的示例中,可以使用,它返回文件系统上存储用openFileOutput(String,int)创建的文件的绝对路径

例如


文件目录在哪里?我的意思是,它只是预先确定的目录。你是如何创建文件的?你是如何存储它的?文件目录在哪里?我的意思是,它只是预先确定的目录。你是如何创建这个文件的?你是如何存储它的?