我想要的是检查文本文件是否存在的安全代码。我在用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;
}
}
发布于 2015-09-27 08:05:30
File
有存在()方法,它可以满足您的需要。
File file = new File(fileDirectory, "file.txt");
if (file.exists()) {
}
其中fileDirectory
是存储文件的目录。
编辑:
在您的示例中,您可以使用getFileStreamPath,它返回文件系统上的绝对路径,其中存储用openFileOutput(String,int)创建的文件。
例如。
File file = getFileStreamPath("file.txt");
if (file.exists()) {
}
https://stackoverflow.com/questions/32805833
复制相似问题