简体   繁体   English

将 Hash Map 中的值存储到文本文件中

[英]Storing values from a Hash Map into a Text File

I have created a class that allows the user to create and store compounds into a Hash Map and now I want to create another class that allows me to take the values stored in that Hash Map and save them into a text file. I have created a class that allows the user to create and store compounds into a Hash Map and now I want to create another class that allows me to take the values stored in that Hash Map and save them into a text file. I'm not sure if this is needed, but here is the code for the first class that I created containing the Hash Map:我不确定这是否需要,但这是我创建的第一个 class 的代码,其中包含 Hash Map:

package abi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class ChemicalComp {
public static void main(String[] args) throws IOException{
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
       Map<String, String> data = new HashMap<String, String>();
       while(true){
           String readinput=br.readLine();
           if(readinput.equals(""))
               break;
                String input = readinput.replaceAll("\"", "");
                String array[]=input.split(", ");
                String compound=array[0];
                String formula="";
      
                for(int i=1;i<array.length;i++){
                    if(!array[i].equals("1")){
                        formula+=array[i];
                    }
                }
                data.put(compound, formula);
       }
       if(!data.isEmpty()) {
           @SuppressWarnings("rawtypes")
           Iterator it = data.entrySet().iterator();
           while(it.hasNext()) {
               @SuppressWarnings("rawtypes")
               Map.Entry obj = (Entry) it.next();
               System.out.println(obj.getKey()+":"+obj.getValue());
           }
       }

       }


}

I'm not too familiar with text files, but I have done some research and this is what I've gotten so far.我对文本文件不太熟悉,但我做了一些研究,这就是我到目前为止所得到的。 I know its pretty basic and that I will probably need some type of getter method, but I'm not sure where to incorporate it into what I have.我知道它非常基本,我可能需要某种类型的 getter 方法,但我不确定在哪里将它合并到我所拥有的中。 Here is what I have for the class containing the text file:这是包含文本文件的 class 的内容:

package abi;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class CompoundManager {
private String path;
private boolean append_to_file = false;

public CompoundManager(String file_path) {
    path = file_path;
    
}
public CompoundManager(String file_path, boolean append_value){
    path = file_path; 
    append_to_file = append_value;
}
public void WriteToFile (String textLine) throws IOException{
    
FileWriter Compounds = new FileWriter(path, append_to_file);
PrintWriter print_line = new PrintWriter (Compounds);

print_line.printf("%s" + "%n", textLine);

print_line.close();
}
}

I can't understand what your program does but you can use a buffered writer for it.我不明白你的程序做了什么,但你可以使用缓冲的编写器。 Just create a try-catch block and wrap a filewriter in a bufferedwriter like this:只需创建一个 try-catch 块并将文件写入器包装在缓冲写入器中,如下所示:

try (BufferedWriter br = new BufferedWriter(new FileWriter(new File("filename.txt"))))
{
     for (Map.Entry<Integer, String> entry : map.entrySet()) {
        int key = entry.getKey();
        String value = entry.getValue();
        br.write(key + ": " + value);
        br.newLine();
    }
} catch (Exception e) {
    printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM