开发者

Check file is available or not in android?

开发者 https://www.devze.com 2023-01-08 12:19 出处:网络
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

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

精彩评论

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