簡體   English   中英

將txt文件讀取到哈希圖中,並用“ \\ t”分隔

[英]read txt file to hashmap, split by “\t”

我有這種形式的txt文件:

 1     01/01/2018 01:00 1915    8,4
 1     01/01/2018 02:00 2111    8,8

讀取文件后,我想將其存儲到具有以下結構的Map中:

     <"Key1",1> <"Key2",01/01/2018 01:00>  <"Key3",1915>  <"Key4",8,4>

這是導入代碼

        BufferedReader buf = new BufferedReader(new 
 FileReader("test.txt"));
        ArrayList<String> words = new ArrayList<>();
        String lineJustFetched = null;
        String[] wordsArray;
        Map<String,String> map = new HashMap<>();

        while(true){
            lineJustFetched = buf.readLine();
            if(lineJustFetched == null) {
                break;
            } else {
                wordsArray = lineJustFetched.split("\t");
                for(String each : wordsArray){
                        words.add(each);
                  //  System.out.println(words.toString());
                    map.put("Key1",each);
                    System.out.println(map.toString());

                }
            }
        }
        buf.close();

我不知道要放入地圖中具有這種結構的問題

   <"Key1",1> <"Key2",01/01/2018 01:00>...

for索引

for(int i = 0 ; i < wordsArray.length ; i++) {
    map.put("Key"+(i+1), wordsArray[i]);
}

編輯

在評論之后,您可以設置一個具有字段名稱的數組並使用它

String[] fieldNames = {"id", "date", "whatever"};
for(int i = 0 ; i < wordsArray.length ; i++) {
    map.put(fieldNames[i], wordsArray[i]);
}

像這樣將split的結果放置到地圖中,並分配鍵:

wordsArray = lineJustFetched.split("\t");
map.put("Key1", wordsArray[0]);
map.put("Key2", wordsArray[1]);
map.put("Key3", wordsArray[2]);
map.put("Key4", wordsArray[3]);

好吧,首先,您的錯誤是您已將Key1硬編碼為Hashmap的密鑰,您應該更改它。

但是,為什么不使用OOP方法呢? 在要讀取的行中創建數據對象,然后將其添加到列表或其他內容中,這樣,您還可以為“鍵”使用有意義的名稱,而不僅僅是“ Key1”,“ Key2”,...

編輯:解釋OOP方式

首先,我對Java類型名/內置函數不是很熟悉,因此您可能需要更改此代碼的某些部分,但是概念保持不變。 如果您有任何問題,請問我:)

// the object that you can store your data in
class YourObject
{
    int _someKey;
    DateTime _dateTime; // I'm not sure which is the correct Java typename here, but I'm sure you'll understand what I mean
    int _someValue;
    double _otherValue;

    // getters and setters as you need..
}

// container for all your data to be fetched from the file
ArrayList<YourObject> data = new ArrayList<YourObject>()

// in your fetching loop:
wordsArray = lineJustFetched.split("\t");
YourObject yourObject = new YourObject();
yourObject.setSomeKey(int.Parse(wordsArray[0]));
yourObject.setDateTime(DateTime.Parse(wordsArray[1]));
yourObject.setSomeValue(int.Parse(wordsArray[2]));
yourObject.setOtherValue(double.Parse(wordsArray[3]));
data.add(yourObject);

希望這能達到您的目的。 我已根據您的要求更新了您的代碼。 輸入將與您在問題中提到的相同。

BufferedReader buf = new BufferedReader(new FileReader("src/com/test/test.txt"));
ArrayList<Map> allWordsList = new ArrayList<>();
String lineJustFetched = null;
String[] wordsArray;
Map<String, String> map;
String[] fieldNames = { "Id", "Date", "Code", "No" };

while ((lineJustFetched = buf.readLine()) != null) {
    wordsArray = lineJustFetched.split("\\s+");
    map = new HashMap<>();
    for (int i = 0, j = i; i < wordsArray.length; i++) {
        if (i == 1) map.put(fieldNames[j++], wordsArray[i] + " " + wordsArray[i + 1]);
        if (i != 1 && i != 2) map.put(fieldNames[j++], wordsArray[i]);
    }
    allWordsList.add(map);
}
buf.close();

System.out.println(allWordsList);

您將獲得輸出為

[{No=8,4, Id=1, Code=1915, Date=01/01/2018 01:00}, 
{No=8,8, Id=1, Code=2111, Date=01/01/2018 02:00}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM