これが私の問題です。Interatorをファイル「random.txt」に書き込む必要があります。すべてのデータを表示できます。データはStoreであると想定されており、System.out.printで確認できますが、ファイルが空白になっています。ファイルを作成することはできますが、書き込むことはできません。
コンプからファイルを読んでいます。ツリーマップに保存し、テキストファイルに書き込もうとします。(私は問題なく配列でそれを行うことができます)しかし、このイテレータ付きマップは私の頭をバウンドさせています。
誰かが私にごみを手伝ってくれるなら、私は感謝します。
TreeMapとIteratorを使用する必要があります。
public static void main(String[] args) throws FileNotFoundException, IOException {
Map<String, String> Store = new TreeMap<>();
Scanner text=new Scanner(System.in);
System.out.println("enter file: ");
//C:\Users\Alex\Desktop\Fruits\fruits.txt
String rap=text.next();
File into = new File(rap);
try (Scanner in = new Scanner(into)) {
while (in.hasNext()){
String name=in.next();
String fruta = in.next();
Integer num= Integer.parseInt(in.next());
System.out.println(fruta+"\t"+name+"\t"+num);
if (Store.containsKey(fruta)){
Store.put(name,Store.get(name)+fruta );
}
else{
Store.put(name,fruta);
}
}
in.close();
}
System.out.println();
// insert data to store MAP
Set top=Store.entrySet();
Iterator it = top.iterator();
// debugging
System.out.println(top);
// Creating file???????
FileWriter fstream = new FileWriter("random.txt");
//identify File to be write??????
BufferedWriter out = new BufferedWriter(fstream);
//iterator Loop
while(it.hasNext()) {
Map.Entry m = (Map.Entry)it.next();
String key = (String)m.getKey();
String value = (String)m.getValue();
//writing to file?????
out.write("\t "+key+"\t "+value);
// debugging
System.out.println(key +"\t"+ value);
}
System.out.println("File created successfully.");
}