Java Questions
Java Questions

Reputation: 7953

File handling in java with permission denied and wrong path

i have a java class which reads and gives me the list of files available for the particular given path.

this is fine when the right path is given but there is no exception or error if the file path is not accessible or wrong path,

how to handle the following,

  1. Given path does not have a permission to access
  2. wrong path is given.

thanks

This is my code what i tried and it is the jdk version is 1.6:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExceptionInFileHandling {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void GetDirectory(String a_Path, List a_files, List a_folders) throws IOException {
        try {
            File l_Directory = new File(a_Path);
            File[] l_files = l_Directory.listFiles();

            for (int c = 0; c < l_files.length; c++) {
                if (l_files[c].isDirectory()) {
                    a_folders.add(l_files[c].getName());
                } else {
                        a_files.add(l_files[c].getName());
                }
            }
        } catch (Exception ex){
            ex.printStackTrace();
        }

    }
    @SuppressWarnings("rawtypes")
    public static void main(String args[]) throws IOException {

        String filesLocation = "asdfasdf/sdfsdf/";
        List l_Files = new ArrayList(), l_Folders = new ArrayList();
        GetDirectory(filesLocation, l_Files, l_Folders);

        System.out.println("Files");
        System.out.println("---------------------------");
        for (Object file : l_Files) {
            System.out.println(file);
        }
        System.out.println("Done");

    }
}

Upvotes: 0

Views: 2261

Answers (3)

Solution 1: To replace the file separator based on the OS, answers for similar thread

Java regex to replace file path based on OS

Platform independent paths in Java

Solution 2: To check wrong path, you may use below code.

  @SuppressWarnings({ "rawtypes", "unchecked" })
        public static void GetDirectory(String a_Path, List a_files, List a_folders) throws IOException {
            try {
                if (!new File(a_Path).exists())
                {
                   throw new FileNotFoundException("Given path '"+a_Path+"' is not available");
                }
                File l_Directory = new File(a_Path);
                File[] l_files = l_Directory.listFiles();

                for (int c = 0; c < l_files.length; c++) {
                    if (l_files[c].isDirectory()) {
                        a_folders.add(l_files[c].getName());
                    } else {
                            a_files.add(l_files[c].getName());
                    }
                }
            } 
            catch (FileNotFoundException ex){
               ex.printStackTrace();
            }
            catch (Exception ex){
                ex.printStackTrace();
            }            
        }

I hope this will help you.

Upvotes: 0

sunysen
sunysen

Reputation: 2351

@SuppressWarnings({ "rawtypes", "unchecked" })
    public static void GetDirectory(String a_Path, List a_files, List a_folders) throws IOException {
        try {
            File l_Directory = new File(a_Path);
            **if(l_Directory.exists()){**
                File[] l_files = l_Directory.listFiles();

                for (int c = 0; c < l_files.length; c++) {
                    if (l_files[c].isDirectory()) {
                        a_folders.add(l_files[c].getName());
                    } else {
                            a_files.add(l_files[c].getName());
                    }
                }
            }
        } catch (Exception ex){
            ex.printStackTrace();
        }

    }

Upvotes: 0

Antoniossss
Antoniossss

Reputation: 32535

You will have to use so e methods of File class. Exists will tell you if the path given is valid and canRead will determine if you have read permision to particular file/directory. Here you have some documentation.

Upvotes: 1

Related Questions