2016-04-26 76 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");