I try to implement a text entry from JEditorPane . When reading text using the .getText() method and then writing to the txt file, the Cyrillic alphabet is converted into characters like "в" . How can this be overcome?
Full text class:
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.nio.charset.StandardCharsets; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.Size2DSyntax; import javax.print.attribute.standard.MediaPrintableArea; import javax.print.attribute.standard.MediaSize; import javax.swing.*; public class PreviewPrinting extends JDialog{ private static final long serialVersionUID = 804726784085628551L; JEditorPane previewPane; JScrollPane scrlPane; JButton printBtn; JButton saveBtn; JButton cancelBtn; JPanel mainPanel; private String text = ""; //Конструктор. Принимает аргументы для подстановки в шаблон .html. public PreviewPrinting(String[] veriables){ this.setSize(590, 490); this.setModal(true); this.setTitle("Предпросмотр и печать документа"); this.setLocationRelativeTo(null); this.setResizable(false); this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); //Передаём текст для отображения и печати используя один из шаблонов. try{ BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("report.htm"), StandardCharsets.UTF_8)); while (br.ready()){ text+=br.readLine(); } br.close(); } catch (Exception readEx) { JOptionPane.showMessageDialog(null, "Файл шаблона не найден!", "Ошибка!", JOptionPane.ERROR_MESSAGE); } /**/ /**/ /**/ /**/ /*Здесь должна быть обработка подстановки данных veriables в считанный шаблон.*/ /**/ /**/ /**/ /**/ //Создаём компоненты окна и обработки событий нажатия кнопок. printBtn = new JButton("<html><b><u>Печать</u></b></html>"); printBtn.setBounds(495, 372, 80, 20); printBtn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { try { //Устанавливаем размеры полей по умолчанию. PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(); attrs.add(new MediaPrintableArea(10, 10, MediaSize.ISO.A4.getX( Size2DSyntax.MM ) - 20, MediaSize.ISO.A4.getY( Size2DSyntax.MM ) - 20, Size2DSyntax.MM)); //Печатаем. previewPane.print(null, null, true, null, attrs, true); } catch (Exception printEx) { JOptionPane.showMessageDialog(null, printEx.getMessage(), "Ошибка!", JOptionPane.ERROR_MESSAGE); } dispose(); } }); saveBtn = new JButton("Сохранить"); saveBtn.setBounds(495, 402, 80, 20); saveBtn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { String text = previewPane.getText(); JFileChooser fc = new JFileChooser(); System.out.println(text); if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){ try{ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fc.getSelectedFile()), StandardCharsets.UTF_8)); bw.write(text); bw.close(); } catch (Exception saveEx) { JOptionPane.showMessageDialog(null, saveEx.getMessage(), "Ошибка!", JOptionPane.ERROR_MESSAGE); } } } }); cancelBtn = new JButton("Отмена"); cancelBtn.setBounds(495, 432, 80, 20); cancelBtn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { dispose(); } }); previewPane = new JEditorPane("text/html", text); previewPane.setEditable(false); scrlPane = new JScrollPane(previewPane); scrlPane.setBounds(10, 10, 480, 443); mainPanel = new JPanel(); mainPanel.setLayout(null); mainPanel.add(scrlPane); mainPanel.add(printBtn); mainPanel.add(saveBtn); mainPanel.add(cancelBtn); this.getContentPane().add(mainPanel); } } 