简体   繁体   English

PrintWriter新行问题。在Eclipse中正常工作

[英]PrintWriter new line issue. Working fine in Eclipse

Basically I have the following method that is a bit overkill I know, but I'm learning..... 基本上我有以下方法,我知道有点矫枉过正,但我​​正在学习.....

public String showOrder() {
        String x = "";
        x = "Order Summary: \n ";
        for (Order order : orders) {
            x += "    Product Id: " + order.getProductCode() + "    Product Description: " + order.getProductDesc() + 
                    "    Order Quantity: " + order.getQuantity() + "    Order Cost: £" + order.getCost() + "\n "; 
        }
        x += "Total Cost: "+ getTotalCost() + "p        Number of Orders: " + numOfOrders() + "     Total Products: " + numOfProducts();
        return x;
    }

This is returning in my test program as I'd expect with each order on its own line. 这正在我的测试程序中返回,因为我期望每个订单都在自己的行上。

 Product Id: 1...etcetc
 Product Id  2...etcetc
 Product Id  3...etcetc

But, when I create a PrintWriter the showOrder() method prints out one long list in my .txt. 但是,当我创建一个PrintWriter时,showOrder()方法会在我的.txt中打印出一个长列表。 file. 文件。

PrintWriter cartReceipt = new PrintWriter("Cart.txt");

        cartReceipt.println(cart1.showOrder());
        cartReceipt.flush();
        cartReceipt.close();

Is there a way to make the same method return a nice orderly list in the txt file like it does in the IDE? 有没有办法让同一个方法在txt文件中返回一个好的有序列表,就像在IDE中一样?

The issue is \\n is not a portable line ending. 问题是\\n不是便携式行结束。 You can use System.lineSeparator() like 你可以使用System.lineSeparator()类的

x += "    Product Id: " + order.getProductCode()
        + "    Product Description: " + order.getProductDesc()
        + "    Order Quantity: " + order.getQuantity() 
        + "    Order Cost: £" + order.getCost() + System.lineSeparator();

However, it would be more efficient if you streamed the write operation to the PrintWriter (since you don't seem to use the String except for writing the orders). 但是,如果将写操作流式传输PrintWriter会更有效(因为除了编写命令之外 ,您似乎不使用String )。 You could pass the PrintWriter into the writing method. 您可以将PrintWriter传递给写入方法。 Something like, 就像是,

public void writeOrder(PrintWriter pw) {
    pw.println("Order Summary:");
    for (Order order : orders) {
        // NOTE: If possible, I would move this code into Order.toString()
        //       and then use pw.println(order);
        pw.print("    Product Id: ");
        pw.print(order.getProductCode());
        pw.print("    Product Description: ");
        pw.print(order.getProductDesc());
        pw.print("    Order Quantity: ");
        pw.print(order.getQuantity());
        pw.print("    Order Cost: £");
        pw.println(order.getCost());
    }
    pw.print("Total Cost: ");
    pw.print(getTotalCost());
    pw.print("p        Number of Orders: ");
    pw.print(numOfOrders());
    pw.print("     Total Products: ");
    pw.println(numOfProducts());
}

You are using "\\n" as a newline character. 您使用“\\ n”作为换行符。 If you are using Notepad to read the .txt file, it doesn't recognize the '\\n' character as a newline (it is normally "\\r\\n" on Windows) and therefore displays all lines on the same line. 如果使用记事本读取.txt文件,则它不会将'\\ n'字符识别为换行符(在Windows上通常为“\\ r \\ n”),因此会在同一行显示所有行。 If you use your IDE to read the .txt file, it will probably display it the way you expect. 如果您使用IDE来读取.txt文件,它可能会以您期望的方式显示它。

The correct way to write your code is to use System.lineSeparator() . 编写代码的正确方法是使用System.lineSeparator() Another change you ought to make in your code is purely for performance reasons: if you are building a string one piece at a time in a loop, you should use a StringBuilder instead of String: 您在代码中应该进行的另一个更改纯粹是出于性能原因:如果您在循环中一次构建一个字符串,则应使用StringBuilder而不是String:

public String showOrder() {
    StringBuilder x = new StringBuilder("Order Summary:");
    x.append(System.lineSeparator());
    for (Order order : orders) {
        x.append("    Product Id: ").append(order.getProductCode()
            .append("    Product Description: ").append(order.getProductDesc())
            .append("    Order Quantity: ").append(order.getQuantity())
            .append("    Order Cost: £").append(order.getCost())
            .append(System.lineSeparator());
    }
    x.append("Total Cost: ").append(getTotalCost())
        .append("p        Number of Orders: ").append(numOfOrders())
        .append("     Total Products: ").append(numOfProducts())
        .append(System.lineSeparator());
    return x.toString();
}

Lastly, be careful when you use non-ASCII characters like "£" in your source file. 最后,在源文件中使用“£”等非ASCII字符时要小心。 Make sure you are using the same character encoding (eg UTF-8 or Windows-1252) as javac expects. 确保使用与javac预期相同的字符编码(例如UTF-8或Windows-1252)。

You can replace "\\n" with "\\r\\n" and see if it helps. 您可以将"\\n"替换为"\\r\\n" ,看看是否有帮助。 I suspect if this is the interpretation of the line separator by the app. 我怀疑这是否是应用程序对行分隔符的解释。 For example, there is '\\n' (the LF character) in the code, but the app you use to read the file does not interpret it into a new line. 例如,代码中有'\\n' (LF字符),但用于读取文件的应用程序不会将其解释为新行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM