java - FileNotFoundException

  • user2014814

    Hi guys I'm new to java and one of my requirements is to create a program that will enable user to add edit delete view record such as employee, address, gender, status, days of work and rate per day that is saved in a notepad , after adding that up the program will compute for the taxes and sss contribution etc that will be also save to a different txtfile and also can be view and edit. my problem is i cant write the computation on the txtfile and getting "FileNotFoundException". heres my code in reading and writting in the txt file

    public static void payrollReadFromFile(String filename) {
            // initializes br identifer as BufferedReader.
            BufferedReader br = null;
            payrolls.clear(); // removes all elements in arraylist employees
            try {
                // instantiate br as FileReader with filename param
                br = new BufferedReader(new FileReader(filename)); 
                try {
                     String name;               
                    double   gincome, nincome, deduc, sss, pagibig, phil = 0; // initialize identifiers
                    // reads each line through br identifier, and stores it on
                    // temporary identifiers
                    // loop continues until null is encountered
                while ((name = br.readLine()) != null) {
                        gincome = Double.parseDouble(br.readLine());
                        sss = Double.parseDouble(br.readLine());
                        pagibig = Double.parseDouble(br.readLine());
                        phil = Double.parseDouble(br.readLine());
                        deduc = Double.parseDouble(br.readLine());
                        nincome = Double.parseDouble(br.readLine());                    
                        // adds the data to employees arraylist
                        payrolls.add(new Person( name, gincome, sss, pagibig, phil,deduc, nincome));
                } finally {
                    br.close(); // closes BufferedReader
            } catch (IOException e) {
        // method which writes data into parameter 'filename'
        // uses PrintWriter and FileWriter
        public static boolean payrollWriteToFile(String filename) {
            boolean saved = false;
            PrintWriter pw = null; // pw is a PrintWriter identifier
            try {
                // instantiate pw as PrintWriter, FileWriter
                pw = new PrintWriter(new FileWriter(filename)); 
                try {
                    // for each loop. each data from employees is written to parameter
                    // filename
                    for (Person payroll : payrolls) {
                                           // pw.println(employee.getId());
                    saved = true;
                } finally {
            } catch (IOException e) {
            return saved;

    can someone help me? i dont know what to do. thanks in advance

  • Answers
  • Thorn

    The nice thing about exceptions is that the stack trace (which is probably what you are seeing in your console output) tells you exactly which line of code threw the exception. Most likely it was this line:

    br = new BufferedReader(new FileReader(filename)); 

    Look at the code that passes the file to your payrollReadFromFile method. The file you are passing is not where you think it is. It might make more sense to pass a File object instead of String since the File object contains not just the name of the file but the complete path to that File as well.

  • Pete Belford

    The key to this question and answer is that the level of code, and the level of question are quite different. It seems to me that this code was written by someone with a decent amount of java knowledge, but the person asking (by their own admission) is a novice. So the choices are two:

    1) Hire one of us to fix it for you. You can hire me, but I am expensive, there are others that will do it on the cheap.

    2) Learn Java, if you are student taking a class, then you should be learning this. Break it down take it slow.

    Here is a debugging tip:


    This way you can see what the filename you are looking for is.

    It is not the responsibilty of stackoverflow contributers to fix your problems. We are here to help you learn.

  • ObedMarsh

    You can throw exceptions, like this:

    public static void payrollReadFromFile(String filename) throws FileNotFoundException {

    Also I suggest you look up the try and catch method.

  • Related Question

    java - Get FileNotFoundException when initialising FileInputStream with File object
  • Ankur

    I am trying to initialise a FileInputStream object using a File object. I am getting a FileNotFound error on the line

    fis = new FileInputStream(file);

    This is strange since I have opened this file through the same method to do regex many times.

    My method is as follows:

    private BufferedInputStream fileToBIS(File file){
    	FileInputStream fis = null;
    	BufferedInputStream bis =null; 
    	try {
    		fis = new FileInputStream(file);
    		bis = new BufferedInputStream(fis);
    	} catch (FileNotFoundException e) {
    		// TODO Auto-generated catch block
    	return bis;
 C:\dev\server\tomcat6\webapps\sample-site (Access is denied)
        at Method)
        at Source)
        at Source)
        at controller.ScanEditRegions.fileToBIS(
        at controller.ScanEditRegions.tidyHTML(
        at controller.ScanEditRegions.process(
        at controller.ScanEditRegions.visitAllDirsAndFiles(
        at controller.Manager.main(

  • Related Answers
  • toolkit

    Judging by the stacktrace you pasted in your post I'd guess that you do not have the rights to read the file.

    The File class allows you to performs useful checks on a file, some of them:

    boolean canExecute();
    boolean canRead();
    boolean canWrite();
    boolean exists();
    boolean isFile();
    boolean isDirectory();

    For example, you could check for: exists() && isFile() && canRead() and print a better error-message depending on the reason why you cant read the file.

  • kgiannakakis

    This is has to do with file permissions settings in the OS. You've started the java process as a user who has no access rights to the specific directory.

  • Bryan Kyle

    You might want to make sure that (in order of likely-hood):

    1. The file exists.
    2. The file is not a directory.
    3. You or the Java process have permissions to open the file.
    4. Another process doesn't have a lock on the file (likely, as you would probably receive a standard IOException instead of FileNotFoundException)
  • JAM

    I think you are executing the statement from eclipse or any java IDE and target file is also present in IDE workspace. You are getting the error as Eclipse cant read the target file in the same workspace. You can run your code from command prompt. It should not through any exception.