Please tell me how to implement save txt. File .. I try this:

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) { FileDialog fdlg; fdlg = new FileDialog( this, "Сохранение файла:..", FileDialog.SAVE); fdlg.show(); File myfile = new File("test.txt"); String text1 = jTex.getText(); try { // запись всей строки BufferedWriter writer = new BufferedWriter(new FileWriter(myfile)); polnijput = fdlg.getDirectory(); writer.write(text1); // запись по символам // writer.append('E'); writer.flush(); writer.close(); } catch(IOException ex){ System.out.println(ex.getMessage()); } } 
  • what's the problem? - zRrr
  • does not save the contents of the Jtex field to a txt file. A dialog box opens. but the file is not saved (although it passes without an error) - studer
  • so myfile you always points to test.txt . In your recent question, it was taking a file from FileDialog . - zRrr

1 answer 1

 /** * Write a small string to a File - Use a FileWriter */ public static void useFileWriter(String content, String filePath) { Writer writer = null; try { writer = new FileWriter(filePath); writer.write(content); } catch (IOException e) { Messages.getInstance().error("Error writing the file : " + filePath, e); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { Messages.getInstance().error("Error closing the file : " + filePath, e); } } } } public static void writeToFile(String fullFilePath, String text) { BufferedWriter output = null; try { File file = new File(fullFilePath); output = new BufferedWriter(new FileWriter(file)); output.append(text); } catch ( IOException e ) { Messages.getInstance().error(e.getMessage(), e); } finally { if ( output != null ) try { output.close(); } catch (IOException e) { Messages.getInstance().error(e.getMessage(), e); } } } 
  • thanks for the help. - studer