0

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
}
Mikko
  • 1,877
  • 1
  • 25
  • 37
QFireball
  • 39
  • 3

1 Answers1

0

Try reading the file as below:

BufferedReader in = new BufferedReader(
       new InputStreamReader(
                  new FileInputStream(file), "UTF8"));
alirabiee
  • 1,286
  • 7
  • 14
  • still the same thing cant read the special chars and when i use UTF-16 he wount read anything – QFireball Aug 28 '17 at 16:29
  • Maybe you could upload a sample file? – alirabiee Aug 28 '17 at 16:43
  • what do you mean with sample file? i manegd to get it to work to read the special chars with using: Charset inputCharset = Charset.forName("ISO-8859-1"); InputStreamReader isr = new InputStreamReader(fis, inputCharset); But with that i have the problem that he just reads 104 lines from the .txt file and not all 424 – QFireball Aug 28 '17 at 16:52