B
B
Baur Kozhaev2017-03-13 15:51:28
Java
Baur Kozhaev, 2017-03-13 15:51:28

Problem with UTF8 and ANSI encoding when reading text from a file?

discovered the following thing, when reading from a text file saved under UTF8, the task did not see the similarity of the strings according to the task "Petrov". equals("Petrov"); If you change the encoding of a text file to ANSI IDE (Intellij Idea), it does not see the Cyrillic alphabet, and displays scribbles, although the comparison is made. those. if you change "Petrov" to "Petrov" in the text file, then the comparison of the strings occurs as it should.
I searched the internet and couldn't find a solution for this topic. Could you please advise what is wrong, thanks in advance.
Task
Calculating salaries
The first parameter of the main method is the file name.
In this file, each line has the following form:
name value
where [name] is String, [value] is double. [name] and [value] are separated by a space.
For each name, calculate the sum of all its values.
Output all data to the console, after sorting in ascending order by name.
Close streams.
Input file example:
Petrov 2
Sidorov 6
Ivanov 1.35
Petrov 3.1
Output example:
Ivanov 1.35
Petrov 5.1
Sidorov 6.0
Code

public class Solution {
public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(args[0]));
    Map<String, Double> map = new TreeMap<>();
    while (reader.ready()) {
        String[] line = reader.readLine().split(" ");
        if (map.containsKey(line[0])) {
            map.put(line[0], map.get(line[0]) + Double.parseDouble(line[1]));
        } else {
            map.put(line[0], Double.parseDouble(line[1]));
        }
    }
    for (String i : map.keySet()) {
        System.out.println(i + " " + map.get(i));
    }
    reader.close();
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adrenal1ne1, 2017-03-13
@Adrenal1ne1

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class test {
  public static void main(String[] args){

  try {
    File fileDir = new File("c:\\temp\\test.txt");

    BufferedReader in = new BufferedReader(
       new InputStreamReader(
                      new FileInputStream(fileDir), "UTF8"));

    String str;

    while ((str = in.readLine()) != null) {
        System.out.println(str);
    }

                in.close();
      }
      catch (UnsupportedEncodingException e)
      {
      System.out.println(e.getMessage());
      }
      catch (IOException e)
      {
      System.out.println(e.getMessage());
      }
      catch (Exception e)
      {
      System.out.println(e.getMessage());
      }
  }
}

Example taken from here ---> www.mkyong.com/java/how-to-read-utf-8-encoded-data...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question