2014-11-25 54 views
1

我正在嘗試從java中的文件中練習閱讀文本。我很困擾我如何讀取N行數量,比如文件中的前10行,然後在ArrayList中添加行數。如何從文件中讀取N行數量?

說例如,該文件包含1-100個數字,如此;

- 1 
- 2 
- 3 
- 4 
- 5 
- 6 
- 7 
- 8 
- 9 
- 10 
- .... 

我想讀取前5個數字,所以1,2,3,4,5並將其添加到數組列表中。到目前爲止,這是我所能做到的,但我被卡住了,不知道現在該做什麼。

ArrayList<Double> array = new ArrayList<Double>(); 
InputStream list = new BufferedInputStream(new FileInputStream("numbers.txt")); 

for (double i = 0; i <= 5; ++i) { 
    // I know I need to add something here so the for loop read through 
    // the file but I have no idea how I can do this 
    array.add(i); // This is saying read 1 line and add it to arraylist, 
    // then read read second and so on 

} 
+0

http://docs.oracle.com/javase/tutorial/essential/io/charstreams.html。 Google在搜索「如何從Java文件讀取文件」時返回的10個第一個結果回答了您的問題。 – 2014-11-25 15:06:06

+0

谷歌可能會非常有用,不時;) – Maksym 2014-11-25 15:06:13

回答

3

你可以嘗試使用掃描儀和計數器:

 ArrayList<Double> array = new ArrayList<Double>(); 
    Scanner input = new Scanner(new File("numbers.txt")); 
    int counter = 0; 
    while(input.hasNextLine() && counter < 10) 
    { 
     array.add(Double.parseDouble(input.nextLine())); 
     counter++; 
    } 

應通過10條線路,只要有文件中更多的投入增加每到ArrayList循環。

2
ArrayList<String> array = new ArrayList<String>(); 
//ArrayList of String (because you will read strings) 
BufferedReader reader = null; 
try { 
    reader = new BufferedReader(new FileReader("numbers.txt")); //to read the file 
} catch (FileNotFoundException ex) { //file numbers.txt does not exists 
    System.err.println(ex.toString()); 
    //here you should stop your program, or find another way to open some file 
} 
String line; //to store a read line 
int N = 5; //max number of lines to read 
int counter = 0; //current number of lines already read 
try { 
    //read line by line with the readLine() method 
    while ((line = reader.readLine()) != null && counter < N) { 
    //check also the counter if it is smaller then desired amount of lines to read 
     array.add(line); //add the line to the ArrayList of strings 
     counter++; //update the counter of the read lines (increment by one) 
    } 
    //the while loop will exit if: 
    // there is no more line to read (i.e. line==null, i.e. N>#lines in the file) 
    // OR the desired amount of line was correctly read 
    reader.close(); //close the reader and related streams 
} catch (IOException ex) { //if there is some input/output problem 
    System.err.println(ex.toString()); 
} 
+0

爲什麼要使用ArrayList '? – 2014-11-25 15:12:32

+0

@LuiggiMendoza因爲他的問題是讀取文本文件的N行,獨立於文件本身的內容。我認爲他說明數字的事實只是給我們一個例子 – WoDoSc 2014-11-25 15:15:53

+0

我不這麼認爲。 OP的意圖非常明確。 OP不能實現的唯一部分就是讀取文件的內容。其餘的代碼只是等待那個缺失的部分來解決這個難題。 – 2014-11-25 15:17:56

1
List<Integer> array = new ArrayList<>(); 
try (BufferedReader in = new BufferedReader(
     new InputStreamReader(new FileInputStream("numbers.txt")))) { 
    for (int i = 0; i < 5; ++i) { // Loops 5 times 
     String line = in.readLine(); 
     if (line == null) [ // End of file? 
      break; 
     } 
     // line does not contain line-ending. 
     int num = Integer.parseInt(line); 
     array.add(i); 
    } 
} // Closes in. 
System.out.println(array); 
0
ArrayList<Double> myList = new ArrayList<Double>(); 
int numberOfLinesToRead = 5; 
File f = new File("number.txt"); 
Scanner fileScanner = new Scanner(f); 
for(int i=0; i<numberOfLinesToRead; i++){ 
    myList.add(fileScanner.nextDouble()); 
} 

確保你在你的文件 「numberOfLinesToRead」 線。