-1

こんにちは。データをテキスト ファイルに書き込むプログラムを作成しています。データは配列リストから取得されます。配列リストから解決されるデータです。例:

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




}

あなたが私を助けてくれることを願っています。

4

2 に答える 2

1

許可されている場合は、代わりにa を使用してくださいBufferedWriter(宿題やその他の理由ではない可能性があります)。

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();
}

これは、forループとすべての getter メソッドが適切に使用されている場合に機能するはずです。

于 2013-04-03T00:51:03.077 に答える
0

finally ブロックでpw.flush();前に試してください。pw.close();問題が解決する場合があります。そうでなければ、コードは私には正しいように見えます。

于 2013-04-03T00:28:52.433 に答える