【问题标题】:Writing to .txt skipping lines and leaving white spaces java写入.txt跳过行并留下空格java
【发布时间】:2015-05-19 02:49:12
【问题描述】:
   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

输入跳过如上的行

【问题讨论】:

  • 你陈述问题的方式毫无意义。告诉我们你的输入和你想要的输出

标签: java writer reader


【解决方案1】:

你是什么意思“跳线”?bw.write("\n" + newPurchaseOrder); 会先放一个空行,如果你的意思是这样,只需将"\n" 转移到最后..The following code works fine: p>

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

使用我的代码,它工作得很好,可以做你想做的事..

【讨论】:

  • 给我一个你的意思的例子..一个你的数据例子
  • 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 1/1/1#PEA-1111#AA/1111#11111n
  • 我添加的最后一个跳过了一行
  • 跳过一行是什么意思?您的数据最初如何以及您希望它如何?
  • 它将输入保存在文本文件中的前一个输入下方 4 行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 2019-12-27
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
相关资源
最近更新 更多