2015-03-13 82 views
-2

我有兩列像這樣由一個txt文件:的Java:閱讀文本文件到一個數組

Name1  _  Opt1 
Name2  _  Opt2 
Name3  _  Opt3 

在每一行中有一個名字,一個標籤分隔符,一_,然後另一名;有很多行(約150000),我甚至不知道哪一個是最好的構造函數,我正在考慮一個二維數組,但如果它是一個更好的選擇,它也可能是其他的東西。對我而言,能夠訪問具有像這樣的元素[x] [y]的元素是非常重要的。 我已經這樣做了,但我只是知道如何計算行數或如何將每行放在數組的不同位置。 下面的代碼:

int countLine = 0; 
    BufferedReader reader = new BufferedReader(new FileReader(filename)); 
    while (true) { 
     String line = reader.readLine(); 
     if (line == null) { 
      reader.close(); 
      break; 
     } else { 
      countLine++; 
     } 
    } 
+0

「對我來說,我可以接觸到的元素像這樣的東西一[X] [Y]是非常重要的我已經這樣做了......「。你是否已經創建了你的二維數組或者你正在尋求什麼幫助? 「我這樣做了」混淆了事情。您將不得不使用分隔符分隔「namex」和「optx」。 – DigitalNinja 2015-03-13 16:15:33

+0

目前還不清楚你真正想問什麼。 – 2015-03-13 16:16:45

+0

我不知道如何把行的「名稱」放在數組的位置a [x + 1] [y]上,並將「opt」放在位置a [x] [y + 1]。 – 2015-03-13 16:26:16

回答

2

既然你不知道時間提前的行數,我會用一個ArrayList而不是數組。將行分割爲字符串值可以使用正則表達式輕鬆完成。

Pattern pattern = Pattern.compile("(.*)\t_\t(.*)"); 
List<String[]> list = new ArrayList<>(); 
int countLine = 0; 

BufferedReader reader = new BufferedReader(new FileReader(filename)); 
while (true) { 
    String line = reader.readLine(); 
    if (line == null) { 
     reader.close(); 
     break; 
    } else { 
     Matcher matcher = pattern.matcher(line); 
     if (matcher.matches()) { 
      list.add(new String[] { matcher.group(1), matcher.group(2) }); 
     } 
     countLine++; 
    } 
+0

'countLine'不需要,因爲'list.size()'中已經有相同的信息,但是我喜歡你使用正則表達式的建議。 – 5gon12eder 2015-03-13 16:24:08

+0

@ 5gon12eder'countLine'部分剛從原始問題複製而來。它的值和'list.size()'至少在我的代碼中的區別在於後者只給你*匹配*行的數量。如果所有行匹配,則該值將與'countLine'相同。 – Thomas 2015-03-13 16:25:37

+0

是的,這是正確的。是否需要該信息。 – 5gon12eder 2015-03-13 16:27:08

0

你應該做的第一件事是編寫一個代表文件中某個條目的類。它可能相當複雜,但一個非常簡單的設計也可能會做到。

class Record { 

    final String name; 
    final String option; 

    Record(final String name, final String option) { 
     this.name = name; 
     this.option = option; 
    } 
} 

使用這個類比處理字符串數組要好得多。

第二件事是使用比數組結構更抽象的數據結構來放入記錄。這將使您免於提前知道元素數量的負擔。我建議你爲此使用ArrayList。然後,您可以一次讀入一條記錄,並將其添加到您的收藏中。

List<Record> records = new ArrayList<Record>(); 
records.add(new Record("NameX", "OptionX")); 
System.out.printf("There are %d records in the list.%n", records.size()); 

當然,上面例子中的第二行應該在讀取文件行的​​循環中一遍又一遍地完成。

0

可以節省

的HashMap>

第一個字符串的數據(關鍵)是你的名字 第二個字符串(關鍵)是你的選擇,他的價值(reault)爲對象的結果。

你可以使用它作爲:

結果= youHashMap.get(名)獲得(OPT);

0

使用ArrayList而不是數組,因爲大小未知。使用掃描儀讀取文件,並檢查下一行的存在,在文件中使用hasNextLine()方法,

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.Scanner; 

class Test { 
    static Scanner userInput = new Scanner(System.in); 

    public static void main(String args[]) throws FileNotFoundException { 

     int countline = 0; 
     Scanner inp=new Scanner(new File("/home/nasir/Desktop/abc.txt")); 
     ArrayList<String> list=new ArrayList<String>(); 

     while(inp.hasNextLine()){ 
      list.add(inp.nextLine());// adding a row in ArrayList 
      countline++;// counting every line/row 
     } 

     System.out.println(countline+" "+list.get(2)); 
    }// Main 
}// Class