In this Java IO Tutorial, we will write a text file (.txt) using FileOutputStream by taking file content in the Java Swing ‘s JTextArea component. Here, you will learn write and close method of the FileOutputStream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
private void btnWriteActionPerformed(java.awt.event.ActionEvent evt) { //Sample 005.1: Take File Name from UI FileOutputStream fos = null; String fileName = txtFileName.getText(); String dataToStore = taFileContent.getText(); //Sample 005.2: Open File for Writing try { fos = new FileOutputStream(fileName); } catch (FileNotFoundException e) { e.printStackTrace(); } //Sample 005.3: Write Text Content to File byte[] data = dataToStore.getBytes(); try { fos.write(data); } catch (IOException e) { e.printStackTrace(); } } |
Categories: Java-Tube