开发者

Check file is available or not in android?

I use the below code to create a file in my application.I need to check is the file is exist or not from my code ? Help me

FileOutputStream fOut = null;        
    OutputStreamWriter osw = null;
    String data="My file Test ";  
    try{
        fOut = openFileOutput("settings.dat",MODE_PRIVATE);     
        osw = new OutputStreamWriter(fOut);
        osw.write(data);
        osw.flush();           
        }
        开发者_如何转开发catch (Exception e) {      
        e.printStackTrace();          
        }


Have you tried the File.exists() method?

  public boolean exists()
Tests whether the file or directory denoted by this abstract pathname exists.

Returns:true if and only if the file or directory denoted by this abstract pathname exists; false otherwise

(Basically, you would do something like if (new File("settings.dat").exists()) { ... }.)

Otherwise, you could go through Context.fileList() and look for the file there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜