Working with files, My code:
package com.company;
Import java.io. *;
Import java.util.scanner;
Public Class Censore {
Public Static Void Main (String [] Args) Thrown Exception {
FileReader FR = New FileReader ("C: \\ Users \\ Stepan \\ List.Txt");
Scanner Scan = New Scanner (New InputStreamReader (
New FileInputStream ("C: \\ Users \\ Stepan \\ List.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 & lt; longarray; i ++) {
STROKA = STROKA.REPLACEALL (Mats [i], "* censorship *");
}
System.out.PrintLN (Stroka);
}
}
In the file, Russian characters. When reading them, � icons appear. Apparently there is a problem in the UTF-8 encoding. I work in Intellij Idea. Tell me how to fix.
Answer 1, Authority 100%
Standard Windows Encoding is CP-1251
. in java
It is encoded as Windows-1251
, therefore You need to write in your code as follows:
Scanner Scan = New Scanner (New InputStreamReader (
New FileInputStream ("C: \\ Users \\ Stepan \\ List.Txt"), "Windows-1251"));
For reference:
"ISO-8859-1" - Western European languages
"Windows-1251" - Standard Windows (Russian)
"UTF-8" - Unicode
Answer 2
What is the encoding of a text file? You can learn notpad, etc.
And try to know what encoding gives the idea, you can check the resulting result here: http: //www.online- decoder.com/en
And more Check which encoding is on the idea itself (Editor – & GT; File Encodings)
Answer 3
xs how to fix, but Windows uses the CP1251 encoding, and there are a manual symbols to be encrypted by other bytes than in UTF-8 (which uses the development medium in the idea of such exactly), i.e. read bytes need to be translated into symbols using cp1251 encoding
Answer 4
Try it:
Scanner Scanner = New Scanner (
New InputStreamReader ("FileNMAE"), "UTF-8"));