Android; Check If File Exists Without Creating a New One

Android; Check if file exists without creating a new one

Your chunk of code does not create a new one, it only checks if its already there and nothing else.

File file = new File(filePath);
if(file.exists())
//Do something
else
// Do something else.

Android: Check if file exist and if not create new one

You have a "rogue" semicolon

if(urltest.exists());

Instead:

if(urltest.exists()){
// do something
}
else{
// create an new file
File urlconfig = new File(myDir, "url.txt");
}

If you don't want to do something specific, you could rework it as:

if(!urltest.exists()){      
// create an new file
File urlconfig = new File(myDir, "url.txt");
}

Be careful with declaring variables inside control blocks. Remember that their scope is the control block itself. You might want this:

File urlconfig;
if(!urltest.exists()){
// create an new file
urlconfig = new File(myDir, "url.txt");
}

Check if file exists without creating it

When you instantiate a File, you're not creating anything on disk but just building an object on which you can call some methods, like exists().

That's fine and cheap, don't try to avoid this instantiation.

The File instance has only two fields:

private String path;
private transient int prefixLength;

And here is the constructor :

public File(String pathname) {
if (pathname == null) {
throw new NullPointerException();
}
this.path = fs.normalize(pathname);
this.prefixLength = fs.prefixLength(this.path);
}

As you can see, the File instance is just an encapsulation of the path. Creating it in order to call exists() is the correct way to proceed. Don't try to optimize it away.

How to check if a txt file exists in android

File has the exists() method, that does what you need.

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

}

where fileDirectory is the directory where you stored the file.

Edit:

in your case you can use getFileStreamPath, which returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored.

E.g.

File file = getFileStreamPath("file.txt");
if (file.exists()) {

}

Android how to check if file exist and else create one?

Does the path '/sdcard/appname' exist? You check for the file before you check for the sub-directory 'appname'. You need to check if that exists before you try to access a file inside it.

Also if you simply need the file to read-write application data why not just go with internal storage - one less manifest permission :) -> read here for internal storage

Check if a file exists before calling openFileInput

public boolean fileExists(Context context, String filename) {    
File file = context.getFileStreamPath(filename);
if(file == null || !file.exists()) {
return false;
}
return true;
}

EDIT:

Also, here is another way for files in external storage.

String fileUrl = "/appname/data.xml";
String file = android.os.Environment.getExternalStorageDirectory().getPath() + fileUrl;
File f = new File(file);

if(f.exists())
return;

Checking file existence before creating if it does not exist

Move the check to before you create the file writer which will create the file if it doesn't exist.

File newFile = new File ("C:/Documents and Settings/Admin/Desktop/Keys.txt");

if (newFile.exist()) {
System.out.print("File is existing");
}

BufferedWriter write = new BufferedWriter (new FileWriter (newFile));
BufferedReader read = new BufferedReader (new FileReader (newFile));

How to check file exist or not and if not create a new file in sdcard in async task

You can check whether File exists or not by using File.exists()

   File f = new File(filePathString);
if(f.exists())
{
/* do something */
}
else
{
file.mkdirs();
//And your other stuffs goes here
}

Note : exists() will return true for directories, too

If you want to check for a particular file that exists or not you have to use File.isFile()

boolean fileExists =  new File("path/to/file.txt").isFile();

new File("C:/").exists() will return true but will not allow you to open and read from it as a file.

The problem in your code is your filename is null.

Edit 2 :

Try this :

String filename="";
String PATH="";
File fileCheck=null;

public DownloadFile(String _filename) {
this.filename=_filename;
PATH = Environment.getExternalStorageDirectory()+"/pictures/"+filename;
filecheck=new File(PATH);
}

Or

or in onPreExecute() of AsyncTask you put this two statements

      PATH = Environment.getExternalStorageDirectory()+"/pictures/"+filename;
filecheck=new File(PATH);


Related Topics



Leave a reply



Submit