Android Studio All About File Class | java.io.File Basic Tutorial And Some Useful Functions
Hello everyone, Today you will learn about File class which is importe by java.io.file in Android studio. File class is very useful in accessing files and making modifications. Also there are a lot of uses of File class. In Android studio we can import File class by writting
import java.io.File
Now if you learn java.io.File class, you will be able to make a file manager app, gallery app, music player and many other things. File is one of the important classes in Android so please learn it soon. Now lets see you have a path of file in string and you have to convert it to File so that we can use it as a file.
Before starting, make sure your app have permission to read and write external storage. Write these lines in your AndroidManifest.xml above Application tag. Without permission , when you try to acess files from external storage, it will cause a error and app will crash which is not healthy for app.
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Now lets start the main class. Lets us assume that you have a path lf file in String format.
String path = Environment.getExternalStorageDirectory()+"/MyFile.jpg";
File file = new File(path);
You can get File from a url. If you have a Uri of a file then you can access the file.
Uri uri = Uri.parse("https://mydomain.com/file.jpg");File file = new File(uri.getPath());
Please Note that, we are using Uri above and not URI. Uri is a Android class imported from android.net.Uri while URI is a java class which is java.net.URI.
Also you can get a file from directory and name of the file. Lets see the example.
String dir = Environment.getExternalStorageDirectory()+"/";
File file = new File(dir,"MyFile.jpg");
Now we have a file variable with a path. From this “file” variable we can rename or delete the file by simple methods.
How to Rename a file in Android Studio
String dir = Environment.getExternalStorageDirectory()+"/";
File file = new File(dir,"MyFile.jpg");
String newName = "NewFileName.jpg";
file.renameTo(new File(dir,newName));
Now your file will be renamed to “NewFileName.jpg”;
How To delete File in Android studio
Deleting a file in Android studio is very easy. Just put Delete() after file.
File file = new File(dir,"MyFile.jpg");
file.delete();
Remember that if you want to delete a whole directory ( folder ), make sure it is an empty folder. You can Not delete a non-empty folder. So if you want to delete a folder, first delete all file inside them.
List all files in Folder Android Studio
If you have a path of folder in your storage and want a array of files in that directory then use below method.
File dir = new File(dirPath);
dir.listFiles();
This will return File[] Array of all the files inside directory. You can also filter files in listFiles() method.
List all mp3 files in folder
File dir = new File(dirPath);
dir.listFiles(new FileFilter(){@Override
public boolean accept(File file)
{
// TODO: Implement this method
return file.getName().endsWith(".mp3");
}
});
This will return array of all mp3 files in your desired directory. Similarly you can get list of all image files in directory.
Search specific files in a directory Android Studio
You can search a keyword within files. Lets see you have some files in directory and want to search file with “wolf.jpg” , you can use following code.
File dir = new File(dir);
dir.listFiles(new FileFilter(){@Override
public boolean accept(File file)
{
// TODO: Implement this method
return file.getName().contains("wolf.jpg");
}
});
Here is a readymade function fo searching a file in a directory.
File[] filterFiles(File dirPath,final String text){
File[] files = dirPath.listFiles(new FileFilter(){@Override
public boolean accept(File file)
{
// TODO: Implement this method
return file.getName().contains(text);
}
});
return files;
}
You can use this function easily and it will make your work easy.
If you want to know that a File is a directory or a file the a simple method is available for it.
boolean isFile=file.isFile(); // return true if given File is a file.
boolean isDirectory = file.isDirectory();// return true if given file is a directory.
How to create a directory in Android studio
Lets see we have a directory to make which path is /sdcard/NewFolder then you can use mkDir() method for this.
file.mkdir();
How to save all files in a ArrayList
If you have a folder and want to save all paths in ArrayList then it is a little bit complicated. First we have to get a File[] array and then we have to put it in ArrayList.
final ArrayList<String> pathArray = new ArrayList();
new File(dirPath).listFiles(new FileFilter(){@Override
public boolean accept(File file)
{
// TODO: Implement this method
pathArray.add(file.getAbsolutePath());
return true;
}
});
These are some basic but useful function that you can use in your application. File is a important class for making apps so learn as soon as possible. By learning File you will be able to create a File Manager application, a video player application or a music player. Also there are more important role of File class in app development. So keep learning from different places, keep practicing and make yourself perfect. Thanks for visiting us.