[英]Reading from file into HashMap
我正在嘗試使用文本文件(zadania.txt)中的字符串填充HashMap。 這是一個簡單的文本文件,格式如下:
問:問題1
答:第一個問題的答案
問:問題2
答:回答問題2等...
然后我想在控制台上寫出來,這里的問題是。 它可以運行,但是不寫任何東西。 當我更改源文件時,它可以工作,但是我想知道為什么它不能與該文件一起工作(文件可以,沒有損壞,用Pages編寫並另存為文本文件)。 有人可以幫忙嗎? 這是我的代碼:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
public class testClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("zadania.txt");
try {
Scanner skaner = new Scanner(file);
HashMap<String,String> questions = new HashMap<String,String>();
while(skaner.hasNext()){
String question = skaner.nextLine();
String answer = skaner.nextLine();
questions.put(question, answer);
}
Iterator<String> keySetIterator = questions.keySet().iterator();
while(keySetIterator.hasNext()){
String key = keySetIterator.next();
System.out.println(key + "//** " +questions.get(key));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如果您使用該路徑到文件File file = new File("zadania.txt");
,您需要將zadania.txt
文件zadania.txt
在項目的根文件夾中。
或者,您可以創建文件夾resources
將文件放在此處,然后編輯File file = new File("resources/zadania.txt");
路徑File file = new File("resources/zadania.txt");
您的代碼對我有用。 因此,正如Oli Charlesworth所述,您應該在第一個循環中添加一些輸出,以檢查是否插入了某些內容。 如果不是,您似乎有一個名為zadania.txt
的空文件。
有關進一步的Java程序的其他一些提示:
關閉你的skaner
! 如果您使用的是Java 7,則可以使用try-with-resources:
try (Scanner skaner = new Scanner(file)){ //... } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }
否則,使用finally構造:
Scanner skaner = null; try { skaner = new Scanner(file) //... } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if(skaner != null) { skaner.close(); } }
否則,您可能會冒風險(在較大的程序中)用盡文件句柄。 最佳做法是關閉您打開的任何資源。
類名(按慣例)應使用大寫大寫字母,因此在您的情況下為TestClass
。
如果同時迭代這兩個,則Map的鍵和值將使用Map.entrySet()
方法來獲取兩者。 對於大型地圖,這比遍歷鍵和調用Map.get()
以獲取Value更快。
這是另一個讀取* .txt文件並將其放入HashMap
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class App {
public static void main(String[] args) {
Map<String, String> questions = new HashMap<>();
Scanner scanner = null;
try {
scanner = new Scanner(new FileInputStream(new File("zanader.txt")));
while (scanner.hasNext()) {
String question = scanner.nextLine();
String answer = scanner.nextLine();
questions.put(question, answer);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (scanner != null) {
scanner.close();
}
}
// get an iterator
Iterator<Map.Entry<String, String>> itr = questions.entrySet().iterator();
// and go through it
while (itr.hasNext()) {
// get the entryset from your map
Map.Entry<String, String> value = itr.next();
// return only the key
String question = value.getKey();
System.out.println("Get answer by key: " + questions.get(question));
System.out.println("Question: " + value.getKey() + " - Answer: " + value.getValue());
}
}
}
我評論了有趣的部分。
帕特里克
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.