1
   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

输入跳过如上的行

4

1 回答 1

1

你是什​​么意思“跳线”? 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

使用我的代码,它工作正常,做你想做的事..

于 2015-03-16T20:25:40.807 回答