Good day! I would like to ask how you can implement reading and outputting the contents of the txt file in the JTextPane .

I try it like this, but I don’t have enough knowledge to complete it. I would be happy to help!

  private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) { FileDialog FD; // поиск файлов FD = new FileDialog(this, "Open file",FileDialog.LOAD); FD.show(); // полный путь polnijput = FD.getDirectory()+FD.getFile(); setTitle("Bloknot test" + " - " +polnijput); //чтениt данных из файлов FileInputStream FIS = null;//для чтения данных из файлов try { FIS = new FileInputStream(polnijput); buf = new byte[FIS.available()]; // массив байтов FIS.read(buf);//прочитанные данные } catch (IOException ex) { System.out.println(ex.toString()); } // массив buf теперь хранит прочитанныеданные // Выбираем всё содержимое тектсовой области jTex.selectAll(); jTex.cut(); // тут я вырезаю, так как метод удаления не знаю пока // Преобразуем массивв строку String szStr = new String(buf); StringTokenizer st; st = new StringTokenizer(szStr, " "); while(st.hasMoreElements()) { szStr = new String((String)st.nextElement());// получаем очередной элемент и преобразуем его в строку // а вот тут его нужно записать. каким методом бы? } try { FIS.close(); } catch (IOException ex) { System.err.println(ex.toString()); } } ` 

    1 answer 1

    if anyone is interested in the decision, then it is necessary

     class FileUtils { public String read(File file) { StringBuilder sb = new StringBuilder(); if (!file.exists()) { throw new RuntimeException("File not found!"); } try (BufferedReader in = new BufferedReader(new FileReader(file.getAbsoluteFile()))) { String s; while ((s = in.readLine()) != null) { sb.append(s); sb.append("\n"); //sb.append(new String(s, "windows-1251")); } } catch (IOException e) { System.err.println("Error: " + e.getMessage()); } return sb.toString(); } }