写入.txt跳过行并留下空格java

时间:2015-03-16 20:15:58

标签: java writer reader

   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

输入会跳过上面的行

1 个答案:

答案 0 :(得分:1)

你是什么意思“跳过行”? bw.write("\n" + newPurchaseOrder);如果这是你的意思,首先会把空行放一个空行,只需将"\n"转移到结尾。{ {1}}

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

使用我的代码它可以正常工作并做你想要的......