【问题标题】:Writing to text file issue写入文本文件问题
【发布时间】:2012-12-04 04:46:44
【问题描述】:
public static void makeSandwich()
{
    System.out.println("Enter First Name: ");
    String name = Scanner.next();
    double price = sandwich.getBreadPrice() + sandwich.getMeatPrice() + sandwich.getVegPrice();
    sandwich.setPrice(price);
    NumberFormat currency = NumberFormat.getCurrencyInstance();
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();

    System.out.println(dateFormat.format(date) + " " + name + " " + sandwich.getBread() + " " + sandwich.getMeat() + " " + sandwich.getVegetables() + " " + currency.format(sandwich.getPrice()));
    OrderLine.writeOrderLine(name, sandwich.getBread(), sandwich.getMeat(), sandwich.getVegetables(), sandwich.getPrice());
}

这是我的 orderline.app 代码

public class OrderLine{
    private static Sandwich sandwich = null;

    public static void writeOrderLine(String name, String bread, String meat, String veg, double price)
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        try
        {
            File productsFile = new File("orderline.txt");
            PrintWriter out = new PrintWriter(
                new BufferedWriter(
                new FileWriter(productsFile, true)));

           out.print(dateFormat.format(date) + "\t");
           out.print(name + "\t");
           out.print(sandwich.getBread() + "\t");
           out.print(sandwich.getMeat() + "\t");
           out.print(sandwich.getVegetables() + "\t");
           out.println(sandwich.getPrice() + "\t");
           out.close();
       }
       catch (IOException e)
       {
           System.out.println(e);
       }
    }
}

它根本不打印,但是当我在 orderline.java 中的 dateformat 之前添加这一行“sandwich = new Sandwich()”时它可以工作,但它最终给了我空字符串,因为我想我正在创建一个新的三明治。我怎么叫我已经做好的三明治?

【问题讨论】:

  • 嗯,三明治。您应该将 makeSandwich() 方法中的 sandwich 实例作为参数传递给 writeOrderLine(),而不是三明治的各个组件。
  • 你能澄清一下吗?我以为我已经将三明治实例作为参数发送了 OrderLine.writeOrderLine(name, sandwich.getBread(), sandwich.getMeat(), sandwich.getVegetables(), sandwich.getPrice());
  • 同样,这些是三明治的各个组成部分,而不是 sandwich 本身。唯一的参数应该是Sandwich sandwich
  • 嗯我刚试过 OrderLine.writeOrderLine(Sandwich sandwich) 它给了我找不到符号错误。
  • 只需要将变量名传递给方法调用:OrderLine.writeOrderLine(sandwich)。你的方法声明应该是public static void writeOrderLine(Sandwich sandwich)。我建议您研究/了解有关 Java 语法的更多信息。

标签: java text file-io


【解决方案1】:

在 writeOrderLine 函数中,您没有初始化三明治,而是传递了三明治的所有属性,您可以通过以下方式进行操作

 public static void writeOrderLine(String name, String bread, String meat, String veg, double price)
        {
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            try
            {
                File productsFile = new File("orderline.txt");
                PrintWriter out = new PrintWriter(
                    new BufferedWriter(
                    new FileWriter(productsFile, true)));

               out.print(dateFormat.format(date) + "\t");
               out.print(name + "\t");
               out.print(bread + "\t");
               out.print(meat + "\t");
               out.print(veg + "\t");
               out.println(price+ "\t");
               out.close();
           }
           catch (IOException e)
           {
               System.out.println(e);
           }
        }

或者你可以这样做

这样打电话

OrderLine.writeOrderLine(name, sandwich);

并将函数更改为

public static void writeOrderLine(String name, Sandwich sandwich)
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        try
        {
            File productsFile = new File("orderline.txt");
            PrintWriter out = new PrintWriter(
                new BufferedWriter(
                new FileWriter(productsFile, true)));

           out.print(dateFormat.format(date) + "\t");
           out.print(name + "\t");
           out.print(sandwich.getBread() + "\t");
           out.print(sandwich.getMeat() + "\t");
           out.print(sandwich.getVegetables() + "\t");
           out.println(sandwich.getPrice() + "\t");
           out.close();
       }
       catch (IOException e)
       {
           System.out.println(e);
       }
    }

【讨论】:

    猜你喜欢
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 2016-05-25
    • 2014-01-28
    相关资源
    最近更新 更多