Java 无法写入该文件

Java 无法写入该文件,java,filewriter,printwriter,writetofile,Java,Filewriter,Printwriter,Writetofile,大家好,我正在创建一个将数据写入文本文件的程序。数据来自数组列表,它是从数组列表求解的数据,例如: public double getGincome(){ gincome=rpd*dwork; return gincome; } 问题是我无法写入txt文件。以下是我写入数据的代码: public static boolean payrollWriteToFile(String filename) { boolean saved =

大家好,我正在创建一个将数据写入文本文件的程序。数据来自数组列表,它是从数组列表求解的数据,例如:

    public double getGincome(){
        gincome=rpd*dwork;
        return gincome;
    }
问题是我无法写入txt文件。以下是我写入数据的代码:

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 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;
    }
这是我的阵列

public class Person {

private String name;
private String add;
private String gender;
private String status;
    public double rpd;
    public double dwork;
    public static int EmployeeCount;
    public double gincome;
    public double nincome;
    public double deduc;
    public double sss;
    public double pagibig;
    public double phil;



public Person(double gincome, double nincome, double deduc, double sss, double 
pagibig, double phil ) {
    this.gincome = gincome ;
    this.nincome = nincome;
    this.deduc = deduc;
    this.sss = sss;
            this.pagibig= pagibig;
            this.phil = phil;
}

Person( String name , double gincome, double sss, double pagibig, double phil,   
double deduc, double nincome){
    this.gincome = gincome;
    this.nincome = nincome;
    this.sss = sss;
    this.pagibig = pagibig;
    this.phil = phil;
    this.deduc = deduc;

}

Person(String name, String add, String gender, String status, double dwork, double rpd)    
{

            this.name = name;
    this.add = add;
    this.gender = gender;
    this.status = status;
            this.rpd = rpd;
            this.dwork = dwork;

}



    public double getGincome(){
        gincome=rpd*dwork;
        return gincome;
    }

    public double getDeduc(){
        double sss = gincome *.03 ;
        double pagibig = gincome *.02;
        double philhealth = gincome* .0125 ;
        deduc= sss + pagibig +philhealth;

        return deduc;
    }


     public double getNincome(){
         return nincome;
     }

     public double getSss(){
         return sss = getGincome() * .03;


     }
     public double getPagibig(){
         return pagibig = getGincome() * .02;


     }
     public double getPhil(){
         return phil = getGincome() * .0125;


     }
    public static int getEmployeeCount(){
    return EmployeeCount;
   }

public String getName() {
    return name;
}

public String getAdd() {
    return add;
}

public String getGender() {
    return gender;
}

public String getStatus() {
    return status;
}

   public double getRpd(){
            return rpd;
    }

    public double getDwork(){
            return dwork;
    }



public void setName(String name) {
    this.name = name;
}

public void setAdd(String add) {
    this.add = add;
}

public void setGender(String gender) {
    this.gender = gender;
}

public void setStatus(String status) {
    this.status = status;

}

    public void setRpd(double rpd){
        this.rpd = rpd;
    }

    public void setdWork(double dwork){
        this.dwork = dwork;
    }




}
我希望你们能帮我,伙计们。

试试
pw.flush()
before
pw.close()在finally块中。它可以解决这个问题。

否则代码在我看来是正确的

如果允许的话,可以使用
BufferedWriter
(可能不是为了家庭作业或其他原因)


如果您的
for
循环和所有getter方法都被正确使用,这应该可以工作。

什么是不能写?你需要更具体一些。你遇到错误了吗?如果是,它们是什么。。。它们发生在哪一行?您需要提供更多详细信息。如果没有更多的信息,我想这与文件系统的写入权限有关。为什么不能写入?抛出的错误是什么?实际上没有抛出错误,我认为问题是合乎逻辑的,因为我想从我的数组列表中调用一个数据(没有输入,只是在那里计算)并将其写入我的文件,所以我打算使用
pw.println(payroll.getGincome)“pw”是PrintWriter的对象。写入文件似乎可以正常工作。如果我使用
pw.println(“A”)而不是打印工资单。someMethod()
“A”存储在文件中。如果您的文件位于某个项目中,请尝试刷新项目以查看更改。或者您正在将一些数据写入文件,但这些数据不正确(比如您希望在末尾添加新数据而不删除以前的数据)?你能告诉我们你面临的问题的更多细节吗?没有任何改变,仍然无法写入我的文件。我还认为我的代码是正确的调用
close()
应该自动刷新编写器。这不是问题,耶!这是@syb0rg-san,希望你这次能帮我..先生,除了名字,其余都是Double方法append不适用于doubleUse
Double.toString()
Double
转换为
字符串。我已经更新了答案来说明这一点。先生,即使在代码中我也无法写入我的文件,那么错误就出现在你的
for
循环和你的getter方法中。此代码适用于我,仅针对您的规范进行了修改。试着摆脱
for
循环,并将其替换为只附加一个简单字符串(如“test”)。
File file = new File(filename);
if (!file.exists())
{
    try
    {
        file.createNewFile();
    } catch (Exception e)
    {
        e.printStackTrace();
    }
}
try
{
    // Read to end of file for writing
    BufferedReader read = new BufferedReader(new FileReader(file));
    String complete = "";
    String line = null;
    while ((line = read.readLine()) != null)
    {
        complete += line + "\n";
    }

    // Write your data
    BufferedWriter write = new BufferedWriter(new FileWriter(file));
    for (Person payroll : payrolls) 
    {
        write.append(payroll.getName());
        write.append(Double.toString(payroll.getGincome()));
        write.append(Double.toString(payroll.getSss()));
        write.append(Double.toString(payroll.getPagibig()));
        write.append(Double.toString(payroll.getPhil()));
        write.append(Double.toString(payroll.getDeduc()));
        write.append(Double.toString(payroll.getNincome()));
    }
    read.close();
    write.close();
} catch (Exception e)
{
    e.printStackTrace();
}