I have some trouble with reading and saving a .txt
file
The .txt
file is a Wordlist with words where every word also have a attached number (integer). So its looks like:
hello,0
bye,3
maby,0
...
I have already a function created to read and load the word with their numbers into the problem is this is not working for special characters which I need because I am from Germany.
Every special character is transferred into a ?
or an empty block. So I need some help to modify my read and save function to be able so save and read ä
, ö
, ü
and ß
chars.
public void loadWordList(Map<String,Integer> map){
try{
File toRead=new File("Wortliste.txt");
FileInputStream fis=new FileInputStream(toRead); // Ist bereits in einer Try and Catch
Scanner sc=new Scanner(fis);
Map<String,String> mapInFile=new HashMap<String,String>();
//read data from file line by line:
String currentLine;
while(sc.hasNextLine()){
currentLine=sc.nextLine();
//now tokenize the currentLine:
StringTokenizer st=new StringTokenizer(currentLine,",",false);
//put tokens ot currentLine in map
map.put(st.nextToken(),Integer.parseInt(st.nextToken()));
}
fis.close();
}catch(Exception e){System.out.println("Wortliste konnte nicht gefunden werden");} // Ist bereits in einer Try and Catch
}
//******************************************************************************
//Speichern der Wordliste für Wortvorschläge
//******************************************************************************
public void saveWordList(Map<String,Integer> map){
try{
File fileTwo=new File("Wortliste.txt");
FileOutputStream fos=new FileOutputStream(fileTwo); // Ist bereits in einer Try and Catch
PrintWriter pw=new PrintWriter(fos);
for(Map.Entry<String,Integer> m :map.entrySet()){ // Andere schreibweise
pw.println(m.getKey()+","+m.getValue());
}
System.out.println("Word gespeichert");
pw.flush();
pw.close();
fos.close();
}catch(Exception e){System.out.println("Wortliste konnte nicht gespeichert werden");} // Ist bereits in einer Try and Catch
}