NewInJava
NewInJava

Reputation: 37

FileNotFoundException solution?

Creating a program that will write the data from the array list to text file and I'm experiencing this FileNotFoundException even though the file exists. At the same time the computed data from my array is not written to it.

This is my code:

 public static void payrollReadFromFile(String filename) {

        // initializes br identifer as BufferedReader.
        BufferedReader br = null;

        payrolls.clear(); // removes all elements in arraylist employees

        try {

            br = new BufferedReader(new FileReader("payroll.txt")); 
            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 payroll arraylist
                    payrolls.add(new Person( name, gincome, 
sss, pagibig, phil,deduc, nincome));
                }
            } finally {
                br.close(); // closes BufferedReader
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    // 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("payroll.txt")); 

            try {

                // for each loop. each data from payrolls is     
written to parameter

                for (Person payroll : payrolls) {

                    pw.println(payroll.getName());
                    pw.println(payroll.getGincome());
                    pw.println(payroll.getSss());
                    pw.println(payroll.getPagibig());
                    pw.println(payroll.getPhil());
                    pw.println(payroll.getDeduc());
                    pw.println(payroll.getNincome());


                }
                saved = true;
            } finally {
                pw.close();
            }
        } catch (IOException e) {

            e.printStackTrace();
        }
        return saved;
    }

Upvotes: 1

Views: 393

Answers (3)

ameed
ameed

Reputation: 1160

You may need to make sure the file is in the right location. Paths are relative to the location of the .class file. For example, "payroll.txt" refers to "C:\path\to\class\file\payroll.txt".

Also, do reconfigure the BufferedReader to use the filename parameter, as others have pointed out.

Do not just create a file in the program; this is just masking the problem, which is a file stored in the wrong place. You could use this for testing to find where the file should go, but then remove that line and just place the file you need to use there.

Upvotes: 0

syb0rg
syb0rg

Reputation: 8247

When I took your code and modified it a little, this worked. Make sure to use the parameter filename that is being passed to your method, right now you are hard-coding it in the method.

File file = new File(filename);
if (!file.exists()) file.createNewFile();
br = new BufferedReader(new FileReader(file));

Upvotes: 0

Richard Wеrеzaк
Richard Wеrеzaк

Reputation: 1561

Your file open statement:

   br = new BufferedReader(new FileReader("payroll.txt")); 

is not using the variable filename that you passed into the method.

You need:

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

You may also want to use the File object instead.

Upvotes: 1

Related Questions