2015-10-20 70 views
0

我不知道我的代碼出現了什麼問題,當我使用list.get(0)時應該顯示我的文本文件的第一行,但我總是獲得第二行!如何在JAVA列表中存儲第一行文本文件

String everything; 
ArrayList<String> list = new ArrayList<String>(); 

try(BufferedReader br = new BufferedReader(new FileReader("lol/new.txt"))) { 
    StringBuilder sb = new StringBuilder(); 
    String line = br.readLine(); 

    while (line != null) { 
     sb.append(line); 
     sb.append(System.lineSeparator()); 
     line = br.readLine(); 
     list.add(line); 
    } 

    everything = sb.toString(); // maybe I'll need this :D 
} 

FILE.TXT

10 10 
5 5 N 
DADADADAA 

當我做一個簡單的System.out.println(list.get(0));我得到5 5 N而不是10 10什麼,我做錯了什麼?

+2

你雙倍讀取文件'串線= br.readLine ();',後面跟着'line = br.readLine();',使用'while((line = br.readLine)!= null){...'而不是 – MadProgrammer

回答

1

試試這個

String everything; 
    ArrayList<String> list = new ArrayList<String>(); 

    try(BufferedReader br = new BufferedReader(new FileReader("lol/new.txt"))) { 
     StringBuilder sb = new StringBuilder(); 
     String line = br.readLine(); // Read first line 

     while (line != null) { 
      sb.append(line); 
      sb.append(System.lineSeparator()); 
      list.add(line); // Line order has changed! 
      line = br.readLine(); // Read next line 
     } 
     everything = sb.toString(); // maybe I'll need this :D 

    } 
+0

你是什麼意思'Line Number Changed' ... ArrayList保存廣告訂單,所以這不是問題 – Manoj

+0

@Manoj訂單已更改。現在,首先追加到列表中,然後讀取下一行 – bhdrkn

+0

您應該指出他通過從文本文件中讀取第二行覆蓋'line'變量的錯誤。好的,正如我看到的......現在你已經做到了。太棒了! – Manoj

1

請參考我的意見對代碼

String everything; 
    ArrayList<String> list = new ArrayList<String>(); 

    try(BufferedReader br = new BufferedReader(new FileReader("lol/new.txt"))) { 
     StringBuilder sb = new StringBuilder(); 
     String line = br.readLine(); 

     while (line != null) { 
      sb.append(line); 
      sb.append(System.lineSeparator()); 
      //up to this point variable line has the line number 1 
      //first line is overwritten with line number 2 
      line = br.readLine(); 
      //when you add the line to your ArrayList, line variable contains the second line 
      list.add(line); 
     } 
     everything = sb.toString(); 

    } 
1

檢查代碼中的註釋。

String everything; 
    ArrayList<String> list = new ArrayList<String>(); 

    try(BufferedReader br = new BufferedReader(new FileReader("lol/new.txt"))) { 
     StringBuilder sb = new StringBuilder(); 
     String line = br.readLine(); // You read first line of file 
            // which is discarded. 

     while (line != null) { 
      sb.append(line); 
      sb.append(System.lineSeparator()); 
      line = br.readLine(); // You read second, and the rest of lines of file. 
            // First line is discarded here. 
      list.add(line);  // Add to list. 
     } 
     everything = sb.toString(); // maybe I'll need this :D 

    } 
2

問題是while循環你已經拉起第一線和while循環的迭代裏面的那個之外,你永遠不使用第一行你拉。

嘗試拉動下一行你while循環結束,因此利用line從while循環之前,以及拉:

while (line != null) { 
    sb.append(line); 
    sb.append(System.lineSeparator()); 
    list.add(line); 
    line = br.readLine(); 
} 
相關問題