Tony
Tony

Reputation: 3805

How to read not ASCII file?

I've got a txt file with cyrillic symbols. This is how I read:

        String csvFile = "C:\\Users\\dolgopolov.a\\Desktop\\Список рассылки 14 07 2014.txt";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = "\t";

        try {

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {

                // use comma as separator
                String[] country = line.split(cvsSplitBy);

                System.out.println("Номер: " + country[0]
                        + " , Сообщение: " + country[1] + "");

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

But the output is wierd:

Номер: 9047120386 , Сообщение: ��������� �������! �� ����� ����� ������� ������������� � ������� 2,98   ���., ������� ���������� �������� � ������� 3 ����

So, how can I avoid that? Do I have to change encoding type or something?

Upvotes: 1

Views: 1774

Answers (2)

Thakur
Thakur

Reputation: 557

StreamReader sr = new StreamReader(stream, Encoding.Unicode);

or

string converted = Encoding.BigEndianUnicode.GetString(dataArray); where is dataArray your array of bytes

Upvotes: 1

gtgaxiola
gtgaxiola

Reputation: 9331

FileReader uses the default encoding so you must use InputStreamReader

new InputStreamReader(new FileInputStream(filePath), encoding)

Upvotes: 3

Related Questions