I'm trying to list all files in /data/dalvik-cache folder but i keep getting NullPointerException
List<String> dalvikFiles = new ArrayList<String>();
for (String dir : dalvikPath) {
File folder = new File(dir);
File list[] = folder.listFiles();
for( int i=0; i< list.length; i++)
{
dalvikFiles.add( list[i].getName() );
}
}
The array dalvikPath contains /data/dalvik-cache
I request su before trying to list and I think i have all the permissions in my manifest.
I think you need to check directory is exist or not . then you can get list of files
File folder = new File(dir);
if(folder.exists()){
File list[] = folder.listFiles();
if(list.length>0{
for( int i=0; i< list.length; i++){
}
}
}else{
}
Ok so i've modified my code and now i don't have javanullpointer but i don't "find" an files in the folders...
List<String> dalvikFiles = new ArrayList<String>();
for (String dir : dalvikPath) {
log.append("Reading " + dir + "\n");
File folder = new File(dir);
if (folder.exists() && folder.isDirectory()){
try{
File list[] = folder.listFiles();
for( int i=0; i< list.length; i++)
{
dalvikFiles.add( list[i].getName().toString() );
log.append(list[i].getName().toString() +"\n");
}
}
catch ( Exception e) {
}
}
else {
log.append("Folder " + dir + "doesn't exist.\n");
}
Related
This code works fine on android 4.4, 5.0, 5.1 , 6.0
File file = new File("/storage/emulated/0//Videos/");
String[] myFiles;
myFiles = file.list();
for (int i = 0; i < myFiles.length; i++) {
File myFile = new File(file, myFiles[i]);
myFile.delete();
}
But when i use this for android 4.0, 4.1, 4.2 i get java.lang.NullPointerException referring at line
for (int i = 0; i < myFiles.length; i++)
So i try to initialize the String,
String[] myFiles = new String[100] //just big value
But android studio shows initializer "new String[100]" is redundent and error is not resolved.
Why does this happen?
Thanks..!
The javadoc for File.list() says that it can return null. You should always check for this whenever you call it and handle it correctly unless you are absolutely certain it will not return null.
This is because you are not checking your folder is null or not make sure your directory contains file
File file = new File("/storage/emulated/0//Videos/");
String[] myFiles;
if (file == null) {
} else {
myFiles = file.list();
for (int i = 0; i < myFiles.length; i++) {
File myFile = new File(file, myFiles[i]);
if(myFile.exists())
myFile.delete();
}
}
And if you want to delete whole directory than you can use this sample method
public void deleteDirectory(File file) {
if( file.exists() ) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
file.delete();
}
}
public void Process(File aFile) throws IOException, ParseException {
if(aFile.isFile())
{
System.out.println("File name:"+aFile.getAbsolutePath());
}
else if (aFile.isDirectory()) {
File[] listOfFiles = aFile.listFiles((FileFilter) HiddenFileFilter.HIDDEN);
if(listOfFiles!=null) {
for (int i = 0; i < listOfFiles.length; i++)
Process(listOfFiles[i]);
}
File[] listOfFiles1 = aFile.listFiles((FileFilter) HiddenFileFilter.VISIBLE);
if(listOfFiles1!=null) {
for (int i = 0; i < listOfFiles1.length; i++)
Process(listOfFiles1[i]);
}
}
}
Call the function in main as follows
String nam = "E:\\";
File aFile = new File(nam);
Process(aFile);
I am using the above code to retrieve all the file details which is present in E:\. It does not retrieve the hidden folder file details. Can anyone help on this?
Just use aFile.listFiles() without any FileFilter then put path of each hidden folder in a list based on check on isHidden().
Sample code:
public static void process(File aFile){
if (aFile.isFile()) {
System.out.println("File name:" + aFile.getAbsolutePath());
} else if (aFile.isDirectory()) {
if(aFile.isHidden()){
System.out.println(aFile.getAbsolutePath()+"folder is hidden");
}
File[] listOfFiles = aFile.listFiles();
if (listOfFiles != null) {
for (int i = 0; i < listOfFiles.length; i++)
process(listOfFiles[i]);
}
}
}
Hi I am trying load a set of "list" into a String array, and the list are simply .txt documents and I want to use the names of the files as the list name on my display, therefore I need to get all of the files in the folder that I had created named "Lists" and then display them into an arrayadapter.
private String[] getListNames() {
//generates the file containing the list names
File file = new File(this.getFilesDir() +File.separator +"Lists");
System.out.println(file.getAbsolutePath());
File lists[] = file.listFiles();
String names[] = {};
if(lists.length >0){
for(int i = 0; i < lists.length; i ++){
names[i] = lists[i].getName();
}
}else{
names[0] = "Create New List";
}
line of code in question according to the stacktrace (line 108)
if(lists.length >0){
StackTrace
03-25 04:37:16.981: E/AndroidRuntime(2099): Caused by: java.lang.NullPointerException
03-25 04:37:16.981: E/AndroidRuntime(2099): at dev.shaw.MyShoppingPlanner.List_Activity.getListNames(List_Activity.java:108)
private List<String> myList = new ArrayList<String>();
String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File( root_sd + "/Lists" ) ;
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
}
You need to log if files.getFileList() is really returning anything or not:
also
change this:
File lists[] = file.listFiles();
String names[] = {};
if(lists.length >0){
for(int i = 0; i < lists.length; i ++){
names[i] = lists[i].getName();
}
}else{
names[0] = "Create New List";
}
to
File lists[] = file.listFiles();
String names[] = {};
if(lists != null && lists.length >0){
for(int i = 0; i < lists.length; i ++){
names[i] = lists[i].getName();
}
}else{
names[0] = "Create New List";
}
P.S: You cannot get the length if the lists array is null in the first place.!
try this one.
//use two boolean variables.
File file;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
//check if the sdcard is enable or disable in your device
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
private String[] getListNames() {
if (mExternalStorageAvailable) {
File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
if (!path.exists()) {
path.mkdirs();
}
file=path.getAbsolutePath();
}
else {
//use ContextWrapper it a onw way to access internal storage.
ContextWrapper cw = new ContextWrapper(getApplicationContext());
(or)
//generates the file containing the list names
file = new File(this.getFilesDir() +File.separator +"Lists");
if (!file.exists()) {
file.mkdirs();
}
System.out.println(file.getAbsolutePath());
}
File lists[] = file.listFiles();
String names[] = {};
try{
if (lists.length==0)
{
System.out.print("There is no files in the Directory");
}
else{
if(lists.length >0){
for(int i = 0; i < lists.length; i ++){
names[i] = lists[i].getName();
}
}else{
names[0] = "Create New List";
}
catch(Exception e)
{
e.printStackTrace();
}
}
I'm new to programming Android, and I want to delete Files on the sd-card. This is my current (working) code...
File appvc = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), "ApplifierVideoCache");
if (appvc.isDirectory()) {
String[] children = appvc.list();
for (int i = 0; i < children.length; i++) {
new File(appvc, children[i]).delete();
}
}
Now I want to delete multiple files, but dont want to mention each file with that big block. Am I able to combine all files in one variable? Thanks ;)
Make a recursive method:
/*
* NOTE: coded so as to work around File's misbehaviour with regards to .delete(),
* which does not throw an exception if it fails -- or why you should use Java 7's Files
*/
public void doDelete(final File base)
throws IOException
{
if (base.isDirectory()) {
for (final File entry: base.listFiles())
doDelete(entry);
return;
}
if (!file.delete())
throw new IOException ("Failed to delete " + file + '!');
}
Another possibility would be using the Apache commons-io library and calling
if (file.isDirectory())
FileUtils.deleteDirectory(File directory);
else {
if(!file.delete())
throw new IOException("Failed to delete " + file);
}
You should make a method out of this chunk of code, pass file name and call it whenever you like:
public void DeleteFile(String fileName) {
File appvc = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), fileName);
if (appvc.isDirectory()) {
String[] children = appvc.list();
for (int i = 0; i < children.length; i++) {
new File(appvc, children[i]).delete();
}
}
}
File dir = new File(android.os.Environment.getExternalStorageDirectory(),"ApplifierVideoCache");
Then call
deletedir(dir);
public void deletedir(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
listFile[i].delete();
}
}
}
or if your folder as sub folders then
public void walkdir(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++)
{
if (listFile[i].isDirectory())
{
walkdir(listFile[i]);
} else
{
listFile[i].delete();
}
}
}
For kotlin
Create a array of path list
val paths: MutableList<String> = ArrayList()
paths.add("Yor path")
paths.add("Yor path")
.
.
delete file for each path
try{
paths.forEach{
val file = File(it)
if(file.exists(){
file.delete()
}
}
}catch(e:IOException){
}
I have such code...
File fileDir = new File("/mnt/sdcard/dd");
if(!fileDir.exists() || !fileDir.isDirectory()){
return;
}
String[] files = fileDir.list();
So, I have an array of files' names...
But I want to GET an array of "path to each file"+fileDir.list()
For example
I have - "/09.jpg"
I want - "/mnt/sdcard/dd/09.jpg"
How can I do it? Thanks
try following code,
String path = "/mnt/sdcard/dd";
File fileDir = new File( path );
if(!fileDir.exists() || !fileDir.isDirectory())
{
return;
}
String[] files = fileDir.list();
for ( int i = 0 ; i < files.length ; i++ )
{
files[i] = path + "/" + files[i];
}
Now the array files contains the updated value with path.
File fileDir = new File("/mnt/sdcard/dd");
if(!fileDir.exists() || !fileDir.isDirectory()){
return;
}
File[] files = fileDir.listFiles();
for(File f: files){
Log.i("", f.getAbsolutePath());
}
What you need is getAbsolutePath(),
File file = new File("/mnt/sdcard/dd");
Files[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
Log.e("Root Path of file:" + i, files[i].getAbsolutePath());
}