简体   繁体   中英

How to count occurrence of Polish characters in .txt file

I have to prepare a .txt file and count how many times each character of alphabet occurs in the file. I've found a very nice piece of code, but unfortunately, it doesn't work with Polish characters like ą,ę,ć,ó,ż,ź. Even though I put them in the array, for some reason they are not found in the .txt file so the output is 0.

Does anyone know why? Maybe I should count them differently, with "Switch" or something similar. Before anyone asks - yes, the .txt file is saved with UTF-8 :)

public static void main(String[] args) throws FileNotFoundException {
        int ch;
        BufferedReader reader;
        try {
            int counter = 0;

            for (char a : "AĄĆĘÓBCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray()) {
                reader = new BufferedReader(new FileReader("C:\\Users\\User\\Desktop\\pan.txt"));
                char toSearch = a;
                counter = 0;

                try {
                    while ((ch = reader.read()) != -1) {
                        if (a == Character.toUpperCase((char) ch)) {
                            counter++;
                            }
                    }

                } catch (IOException e) {
                    System.out.println("Error");
                    e.printStackTrace();
                }
                System.out.println(toSearch + " occurs " + counter);

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

Looks like your problem related to encoding and default system charset

try to change reader variable to this

InputStreamReader reader = new InputStreamReader(new FileInputStream("C:\\Users\\User\\Desktop\\pan.txt"), "UTF-8");

try this: I suggest that you use NIO and this code I have written for you using NIO, RandomAccessFile and MappedByteBuffer that is faster:

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;

public class FileReadNio
{
public static void main(String[] args) throws IOException
{
    Map<Character, Integer> charCountMap = new HashMap<>();

    RandomAccessFile rndFile = new RandomAccessFile
            ("c:\\test123.txt", "r");
    FileChannel inChannel = rndFile.getChannel();
    MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
    buffer.load();
    for (int i = 0; i < buffer.limit(); i++)
    {

        char c = (char) buffer.get();

        if (charCountMap.get(c) != null) {
        int cnt = charCountMap.get(c);
            charCountMap.put(c, ++cnt);

        }
        else
        {
            charCountMap.put(c, 1);
        }
    }

    for (Map.Entry<Character,Integer> characterIntegerEntry : charCountMap.entrySet()) {

        System.out.printf("char: %s :: count=%d", characterIntegerEntry.getKey(), characterIntegerEntry.getValue());
        System.out.println();
    }

    buffer.clear();
    inChannel.close();
    rndFile.close();
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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