簡體   English   中英

寫入.txt跳過行並保留空白Java

[英]Writing to .txt skipping lines and leaving white spaces java

   String newPurchaseOrder = dateStr + "#" + customerID + "#" + productCode + "#" + qty;
        try {
            String filename = "PurchaseOrderDataFile.txt";
            FileWriter fw = new FileWriter(filename, true); //the true will append the new data
            BufferedWriter bw = new BufferedWriter(fw);
            FileReader fr = new FileReader("PurchaseOrderDataFile.txt");

            bw.write("\n" + newPurchaseOrder);
            bw.close();
        } catch (IOException ioe) {
            System.err.println("IOException: " + ioe.getMessage());
        }

嘗試防止輸入到.txt文件時跳過行

08/12/13#PMI-1256#DT/9489#8
16/12/13#ENE-5789#PV/5732#25
27/12/13#PEA-4567#PV/5732#3@
09/01/14#PEA-4567#DT/9489#1
16/01/14#EMI-6329#PV/5732#8


16/07/13#ESE-5577#ZR/7413#6

輸入跳過上面的行

你是什​​么意思? bw.write("\\n" + newPurchaseOrder); 如果這就是您的意思,將首先在空行前插入"\\n"The following code works fine:

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
class myWrite {
    public static void main(String[] args) {
        String dateStr = "test";
        String customerID = "1";
        String productCode = "100";
        String qty = "1000";
  String newPurchaseOrder = dateStr + "#" + customerID + "#" + productCode + "#" + qty;
  String newPurchaseOrder2 = dateStr + "#" + customerID + "#" + productCode + "#" + qty;
        try {
            String filename = "PurchaseOrderDataFile.txt";
            FileWriter fw = new FileWriter(filename, true); //the true will append the new data
            BufferedWriter bw = new BufferedWriter(fw);
            FileReader fr = new FileReader("PurchaseOrderDataFile.txt");

            bw.write(newPurchaseOrder + "\n");
            bw.write(newPurchaseOrder2 + "\n");
            bw.close();
        } catch (IOException ioe) {
            System.err.println("IOException: " + ioe.getMessage());
        }
    }
}

寫道:

test#1#100#1000
test#1#100#1000

編輯:使用您告訴我的輸出,

08/12/13#PMI-1256#DT/9489#8 
16/12/13#ENE-5789#PV/5732#25 
27/12/13#PEA-4567#PV/5732#3@ 
09/01/14#PEA-4567#DT/9489#1 
16/01/14#EMI-6329#PV/5732#8 

然后,我添加了您告訴我的行:

16/07/13#ESE-5577#ZR/7413#6

產生:

08/12/13#PMI-1256#DT/9489#8 
16/12/13#ENE-5789#PV/5732#25 
27/12/13#PEA-4567#PV/5732#3@ 
09/01/14#PEA-4567#DT/9489#1 
16/01/14#EMI-6329#PV/5732#8 
16/07/13#ESE-5577#ZR/7413#6

使用我的代碼,它可以正常工作,並且可以滿足您的要求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM