Question:
Working with files, my code is:
package com.company;
import java.io.*;
import java.util.Scanner;
public class Censore {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("C:\\Users\\Stepan\\Список.txt");
Scanner scan = new Scanner(new InputStreamReader(
new FileInputStream("C:\\Users\\Stepan\\Список.txt"), "UTF-8");
String t = scan.nextLine();
fr.close();
System.out.println(t);
String[] mats = t.split(" ");
int longarray = mats.length;
Scanner fx = new Scanner(System.in);
String stroka = fx.nextLine();
for (int i = 0; i < longarray; i++) {
stroka = stroka.replaceAll(mats[i], "*цензура*");
}
System.out.println(stroka);
}
}
The file contains Russian characters. When they are read � icons appear. Apparently there is a problem with UTF-8 encoding. I work in Intellij IDEA. Please tell me how to fix it.
Answer:
The standard Windows encoding is CP-1251
. In Java
it is encoded as windows-1251
, so in your code you need to write it like this:
Scanner scan = new Scanner(new InputStreamReader(
new FileInputStream("C:\\Users\\Stepan\\Список.txt"), "windows-1251"));
For reference:
"ISO-8859-1" - западноевропейские языки
"windows-1251" - стандартная Windows (русская)
"UTF-8" - юникод