2013-04-03 20 views
-1

好日子的傢伙,即時通訊創建一個程序,將數據寫入文本文件。將數據從一個數組列表的到來,它是從數組列表比如解決的數據:不能寫在文件

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

問題是我不能寫我的TXT file..here是我在寫數據的代碼:

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




} 

我希望你能幫助我的人。

+1

你是什麼意思*不能寫*?你需要更具體。你遇到錯誤?如果是這樣,他們是什麼? ......他們發生了什麼? –

+0

您需要提供更多詳細信息。沒有更多的信息,我猜想它與你的文件系統上的寫權限有關。 – OlivierLi

+0

你爲什麼不寫?拋出的錯誤是什麼? – jsedano

回答

0

在finally塊中嘗試pw.flush();之前pw.close();。它可以解決問題。 否則代碼看起來對我來說是正確的。

+0

沒有改變任何東西,仍然無法寫入我的文件。我也認爲我的代碼是正確的 – NewInJava

+1

調用'close()'應該自動刷新寫入器。這不是問題。 – syb0rg

+0

耶!這是@ syb0rg -san,希望你能幫助我這次... – NewInJava

1

改爲使用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方法使用得當這應該工作。

+0

生病請嘗試這個... – NewInJava

+0

先生,名稱除外,其餘全部爲雙倍 – NewInJava

+0

方法追加不適用雙倍 – NewInJava