0

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:

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. Here is what I have for the class containing the text file:

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();
}
}
Dan
  • 23
  • 4
  • it's the reverse work. Just try to do the reverse operations you did before. From put() to get() and the keys can be retrieved by keySet() –  Jul 04 '20 at 08:22
  • Where are you struggling? Iterate through the HashMap, append each value to the file you want. Is there something specific you're having trouble with? Also, for iterating through the HashMap, you can check [this answer](https://stackoverflow.com/a/1066607/12279039) – Ayush Jul 04 '20 at 08:52
  • consider using a lib, since its a problem solved many times. for example "xstream" for xml marshalling – Henning Luther Jul 04 '20 at 08:54

1 Answers1

0

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 (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();
}
Nishith Savla
  • 310
  • 2
  • 10