Good day. There was a problem when loading text from a file, Cyrillic is not displayed. Tell me how to fix it.

public String read(File file) { StringBuilder sb = new StringBuilder(); if (!file.exists()) { throw new RuntimeException("File not found!"); } try (BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream(file), Charset.forName("ISO-8859-1")))) { String s; while ((s = in.readLine()) != null) { sb.append(s); sb.append("\n"); } } catch (IOException e) { System.err.println("Error: " + e.getMessage()); } return sb.toString(); } 
  • `BufferedReader input = new BufferedReader (new FileReader (file))` doesn't work? - Senior Pomidor
  • not. broken characters in the test field after import, that's why I'm trying to set the encoding manually - studer
  • one
    very strange. can you attach a file? try another option: `BufferedInputStream reader = new BufferedInputStream (new FileInputStream (fileName)); StringBuilder stringBuilder = new StringBuilder (); try {if (reader.available ()> 0) {stringBuilder.append ((char) reader.read ()); }} catch (IOException e) {running = false; } sout (stringBuilder.toString ()); ` - Senior Pomidor
  • one
    Encoding in the source text. Determine what encoding and decode when reading. - Riĥard Brugekĥaim
  • thanks, oddly enough, but it helped .. - studer

1 answer 1

the answer is taken from the comment

  BufferedInputStream reader = new BufferedInputStream(new FileInputStream( fileName ) ); StringBuilder stringBuilder = new StringBuilder(); try { if( reader.available() > 0 ) { stringBuilder.append( (char)reader.read() ); } } catch (IOException e) { } sout(stringBuilder.toString());