1

FileNotFoundException创建一个程序,将数组列表中的数据写入文本文件,即使文件存在,我也会遇到这种情况。同时,我的数组中的计算数据没有写入它。

这是我的代码:

 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;
    }
4

3 回答 3

1

您的文件打开声明:

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

没有使用您传递给方法的变量文件名。

你需要:

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

您可能还想改用 File 对象。

于 2013-04-02T23:09:26.693 回答
0

当我拿走你的代码并稍作修改时,这行得通。确保使用filename传递给您的方法的参数,现在您在方法中对其进行硬编码。

File file = new File(filename);
if (!file.exists()) file.createNewFile();
br = new BufferedReader(new FileReader(file));
于 2013-04-02T23:34:21.753 回答
0

您可能需要确保文件位于正确的位置。路径相对于.class文件的位置。例如,“ payroll.txt”指的是“ C:\path\to\class\file\payroll.txt”。

另外,正如其他人指出的那样,请重新配置BufferedReader以使用该参数。filename

不要只在程序中创建文件;这只是掩盖了问题,这是一个存储在错误位置的文件。您可以使用它进行测试以查找文件应该去哪里,然后删除该行并将您需要使用的文件放在那里。

于 2013-04-08T22:50:11.703 回答