无法获取if语句的产品来写入新的.txt文件

时间:2017-06-09 00:59:29

标签: java filewriter

我正在为我的Java类开发一个销售工具,除了将结果写入新的.txt文件之外,我能够完成所有工作。为了给出一些背景故事,我有一个GUI,它将数据写入名为out1.txt的文本文件中。我正在从这个文件中读取数据并在JtextArea中显示computesales> = 8,000的任何内容,如下面的代码所示。

 private void evaluateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    saleOutput.setText("");        
    SaleReader reader = new SaleReader();

    try
    {
        List<Sales> sale = reader.readSale("out1.txt");   
        for (Sales s : sale)
        {
            if(s.computeSales()>= 8000)
            {
            saleOutput.append(s.toString());
            saleOutput.append("\n");

            }
        }
    }catch (IOException ex) {
        JOptionPane.showMessageDialog(this, "Unable to open file");
        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
    }


}                                              

我尝试过将下面的内容写入我的out1.txt文件,但它似乎根本不起作用。它甚至不会创建文件。我的同事似乎认为这可能是文件撰写人没有被冲洗的问题?

 public void writeSales(Sales s) throws IOException
{
    File testWriter = new File ("out1.txt");
    BufferedWriter out = new BufferedWriter(new FileWriter(testWriter, true));

    out.write("" + s.getSalesID());
    out.write(", ");
    out.write(s.getFirstName());
    out.write(", ");
    out.write(s.getLastName());
    out.write(", ");
    out.write("" + s.getOfficeSupplies());
    out.write(", ");
    out.write("" + s.getBooks());
    out.write(", ");
    out.write("" + s.getPaper());
    out.write(", ");
    out.write("" + s.getDistrictValue());        
    out.write(", ");
    out.write("" + s.getPreferredContact());   
    out.write(", ");
    out.write("" + s.computeSales());
    out.newLine();       
    out.close();
}

这是我在一个单独的类中编写的,以写入新的stars.txt文件,但是不起作用。有什么建议吗?

public void writeStars(Sales f) throws IOException
{
    File testStars = new File ("stars.txt");
    BufferedWriter outStars = new BufferedWriter(new FileWriter(testStars, true));
    SaleReader reader = new SaleReader();

     try
    {
        List<Sales> sale = reader.readSale("out1.txt");   
        for (Sales s : sale)
        {
            if(s.computeSales()>= 8000)
            {
            outStars.write("" + s.getDistrictValue());
            outStars.newLine();
            outStars.close();

            }
        }
    }catch (IOException ex) {
        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
    }
}

我从这里打电话。

public class Sales 
{
 private int salesID;
 private String firstName;
 private String lastName;
 private double officeSupplies;
 private double books;
 private double paper;
 private String districtValue;
 private String preferredContact;

public int getSalesID() {
    return salesID;
}

public void setSalesID(int salesID) {
    this.salesID = salesID;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public double getOfficeSupplies() {
    return officeSupplies;
}

public void setOfficeSupplies(double officeSupplies) {
    this.officeSupplies = officeSupplies;
}

public double getBooks() {
    return books;
}

public void setBooks(double books) {
    this.books = books;
}

public double getPaper() {
    return paper;
}

public void setPaper(double paper) {
    this.paper = paper;
}

public String getPreferredContact() {
    return preferredContact;
}

public void setPreferredContact(String preferredContact) {
    this.preferredContact = preferredContact;
}    

public String getDistrictValue() {
    return districtValue;
}

public void setDistrictValue(String districtValue) {
    this.districtValue = districtValue;
}

@Override
public String toString() {
    return "Sales{" + "salesID=" + salesID + ", firstName=" + firstName + ", lastName=" + lastName + ", officeSupplies=" + officeSupplies + ", books=" + books + ", paper=" + paper + ", districtValue=" + districtValue + ", preferredContact=" + preferredContact + '}';
}

public double computeSales()
{
    return officeSupplies + books + paper;
}   

}

1 个答案:

答案 0 :(得分:0)

如果您的班级被称为SalesWriter,并且您有此代码

public void writeStars() throws IOException
{
    File testStars = new File ("stars.txt");
    BufferedWriter outStars = new BufferedWriter(new FileWriter(testStars, true));
    SaleReader reader = new SaleReader();

     try
    {
        List<Sales> sale = reader.readSale("out1.txt");   
        for (Sales s : sale)
        {
            if(s.computeSales()>= 8000)
            {
            outStars.write(s);
            outStars.newLine();

            }
        }
        outStars.close();

    }catch (IOException ex) {
        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
    }
}

然后从您的主要代码中,您可以将其称为

new SalesWriter ().writeStars ();

修改

了解我如何将参数移除到此方法,因为您正在使用此方法读取文件。