2016-04-26 93 views
0

我希望有新的客戶名稱和斷裂線

try 
     { 
     br = new BufferedReader(new FileReader(oldFileName)); 
     bw = new BufferedWriter(new FileWriter(tmpFileName)); 

     String line; 
     //bw.write("Customer Record: \r\n"); 

     while ((line = br.readLine()) != null) 
     { 
      if (line.contains(customerName)) 
      { 
       customerRecordPresentInFile = true; 
       line = line + "," + billAmountPayable; 
      } 
      //bw.write("\r\n"); 

我使用這個代碼行新的生產線,但我在文件輸出是一切都在同一行,我想打破行。

bw.write(line + "\n"); 
    } 

請對代碼進行一些更改。

if(!customerRecordPresentInFile) 
    { 
    bw.write(customerName + "-" + billAmountPayable + "\n"); 
    } 
    System.out.println("\nData file updated."); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
    try 
    { 
     if(br != null) { br.close(); } 
     if(bw != null) { bw.close(); } 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    } 

回答

0

按Java的開源平臺提供herenewLine()方法,它使用平臺自有的線的概念分隔符由系統屬性line.separator定義。並非所有平臺都使用換行符('\ n')來終止行。因此調用此方法來終止每個輸出行,因此優選直接寫入換行符

請嘗試此代碼。

bw.write(line); 
    bw.newLine(); 

if(!customerRecordPresentInFile) 
     { 
      bw.write(customerName + "-" + billAmountPayable); 
      bw.newLine(); 
     } 

代替bw.write(line + "\n");