2013-02-11 39 views
0

寫入記事本我的文字像在一個記事本該讀取和Java中

文本記事本的字符串:

date amount intrest tenure debitamount name address state city zipcode phone altphone mailid refaddress ref refphoneno emptype employment 


12/11/1987 12345 3.5 144 32456 durga f rao 456 under road 
dallas dl 2435 2222-22-2222 2222-22-2222 [email protected] 
165 nala road raj 22-22-22-2222 it 12 
15/05/1986 2345 6.4 144 45789 rama k nand 789 residensy 
newyork ny 2222-22-2222 2222-22-2222 [email protected] 
189 ring road hen 22-22-22-22-2222 it 12 
16/12/1985 1234 6.5 144 35265 rama j rao 123 dura residensy meknoor 
mn 2222-22-2222 2222-22-2222 [email protected] 891 cing road vinky 
22-22-22-2222 it 12 
12/11/1987 12345 3.5 144 32456 durga f rao 456 under road dallas dl 
2435 2222-22-2222 2222-22-2222 [email protected] 165 nala road raj 
22-22-22-2222 it 12 


     out put should be like below 
in between name and address single space and all other double space 


12/11/1987 12345 3.5 144 32456 durga f rao 456 under road 
dallas dl 2435 2222-22-2222 2222-22-2222 [email protected] 
165 nala road raj 22-22-22-2222 it 12 
15/05/1986 2345 6.4 144 45789 rama k nand 789 residensy 
newyork ny 2222-22-2222 2222-22-2222 [email protected] 
189 ring road hen 22-22-22-22-2222 it 12 
16/12/1985 1234 6.5 144 35265 rama j rao 123 dura residensy meknoor 
mn 2222-22-2222 2222-22-2222 [email protected] 891 cing road vinky 
22-22-22-2222 it 12 
12/11/1987 12345 3.5 144 32456 dura f rao 456 under road dallas dl 
2435 2222-22-2222 2222-22-2222 [email protected] 165 nala road raj 
22-22-22-2222 it 12 

我想我的輸出應該像下面 在這兩者之間單名空間和所有其他雙空間的名稱和地址?

我寫了下面的代碼,但我沒能得到期望的輸出中

可以在任何一個可以幫助我?我試圖

代碼:

public static String newLine = System.getProperty("line.separator"); 


    public static void main(String args[]){ 
     FileWriter iWrite=null; 

     try { 
      iWrite = new FileWriter("C:/Documents and Settings/pavan/My Documents/output.txt"); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     try { 
List<String> lines = FileUtils.readLines(new File("C:/Documents and Settings/pavan/My Documents/input.txt")); 

     for(String lines1:lines){ 

      iWrite.write(lines1); 

      iWrite.write(newLine); 
     } 
     iWrite.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


    } 
+0

考慮使用'PrintWriter'而不是'FileWriter'。 – 2013-02-11 05:15:30

+0

請提供您的問題的完整信息,不要在SO之外發布鏈接,而是發佈程序的預期輸出和實際輸出。另外,也許你需要在你的'iWrite.write'語句中使用['String#format'](http://goo.gl/SauwK)。 – 2013-02-11 05:16:56

+0

除非有可用的輸入格式說明,否則讀取字段時會出現空格問題。 – 2013-02-11 05:37:17

回答

0

這聞起來有家庭作業的問題,所以我沒有想到的最佳解決方案,但這裏的,如果你需要,你可以做什麼sudo的代碼。

它之前,讓我假設的數據實際上是實際上是基於你的頭一個單行:

1987年12月11日12345 3.5 144 32456杜爾加˚F饒456下路達拉斯DL 2435 2222 -22-2222 2222-22-2222 [email protected] 165那拉氏道拉吉22-22-22-2222這12

  • 創建陣列又名ArrayList<String[]>
  • 的ArrayList中對於每一個你讀線,使用split(" ")split("\s")到換行符分成基於空間
  • 串一旦所有的行被分割並存儲在ArrayList中,您可以通過列表
  • 迭代中的字符串數組列表中,您可以通過數組迭代與黑客
  • 黑客應該知道你需要添加一個額外的空間,所以在數組中,當你到達特定地點時,你可以追加和額外的空間。在其他地方,你可以追加只是一個單一的空間。

同樣,這並不是最佳的解決方案。

至於你當前的代碼,你所做的只是讀一行並追加一個newLine字符,然後再寫回文件。因此,所有你的代碼做的是使

A A A

B B B

C C C

A A A

B B B

C C C

在本質上做的並不多。