Please help me correctly write the functions for reading data from a file into a TextArea, deleting data from it, as well as saving TextArea data to a file! Program Code:

package ch.makery.address.view; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; import javax.swing.JFileChooser; import ch.makery.address.MainApp; import ch.makery.address.PrepodMainApp; import ch.makery.address.model.GhurnalOcenok; import ch.makery.address.model.Lections; import ch.makery.address.model.Test; import javafx.beans.property.SimpleStringProperty; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.TextArea; public class LectionsController { @FXML public TextArea taLection; @FXML private Button butOpen;//save @FXML private Button butSave;//look @FXML private Button butUdalit;//udalit /** * The constructor. * The constructor is called before the initialize() method. */ public LectionsController() { } public void FileOpen(){ JFileChooser fileopen = new JFileChooser(); fileopen.setCurrentDirectory(new File(".")); int ret = fileopen.showDialog(null, "Открыть файл"); if (ret == JFileChooser.APPROVE_OPTION) { File file = fileopen.getSelectedFile(); try { FileRead(file.getAbsolutePath()); } catch (Exception e1) { } } } public void FileSave(){ JFileChooser fileopen = new JFileChooser(); fileopen.setCurrentDirectory(new File(".")); int ret = fileopen.showDialog(null, "Сохранить в файл"); if (ret == JFileChooser.APPROVE_OPTION) { File file = fileopen.getSelectedFile(); try { FileWrite(file.getAbsolutePath()); } catch (IOException e1) { } } } public void Delete(){ if(PrepodMainApp.getLectionList().size()>0){ taLection.clear(); } } public void FileWrite(String Filename)throws IOException{ FileWriter f = new FileWriter(Filename); for(Lections tst : PrepodMainApp.getLectionList()){ f.write(tst.Lecii.getValue() + "\r\n"); } f.close(); } public void FileRead(String Filename) throws FileNotFoundException, IOException{ PrepodMainApp.getLectionList().clear(); Scanner fin = new Scanner(new File(Filename)); String test; while(fin.hasNext()) { if(PrepodMainApp.getLectionList().size()>0){ fin.nextLine(); } test = fin.nextLine(); PrepodMainApp.getLectionList().add(new Lections( new SimpleStringProperty(test) )); } fin.close(); } } 

232

  • Where is the error? P.S. use javaFX - do not use controls from swing (for example, JFileChooser, there is an analogue in javafx) - Andrew Bystrov
  • Logical error: when you click the corresponding buttons (see figure) - reading, deleting and writing does not occur. I do not know how to properly describe these functions for the text area. - Alexandr
  • And you handlers hung on the buttons? - Andrew Bystrov

1 answer 1

Code itself

  import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.stage.FileChooser; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextArea; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; public class Main extends Application { TextArea text; @Override public void start(Stage primaryStage) { try { BorderPane root = new BorderPane(); Scene scene = new Scene(root); text = new TextArea(); root.setRight(text); Button btnRead = new Button("Read"); Button btnClear = new Button("Clear"); Button btnWrite = new Button("Write"); GridPane noRoot = new GridPane(); noRoot.add(btnWrite, 0, 0); noRoot.add(btnClear, 0, 1); noRoot.add(btnRead, 0, 2); root.setCenter(noRoot); btnRead.setOnAction(new ReadButton()); btnClear.setOnAction(new ClearButton()); btnWrite.setOnAction(new WriteButton()); scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); primaryStage.setScene(scene); primaryStage.show(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } public List<String> read(){ List<String> list = new ArrayList<String>(); FileChooser choose = new FileChooser(); File txt = choose.showOpenDialog(null); try (Scanner in = new Scanner (txt)){ while(in.hasNext()){ list.add(in.nextLine()); } }catch(IOException ex){ System.out.println(ex.getMessage()); } return list; } private class ReadButton implements EventHandler<ActionEvent>{ @Override public void handle(ActionEvent arg0) { text.setText(read().toString()); } } private class ClearButton implements EventHandler<ActionEvent>{ @Override public void handle(ActionEvent arg0) { text.clear(); } } private class WriteButton implements EventHandler<ActionEvent>{ @Override public void handle(ActionEvent event) { File file = new File("test"); try (FileOutputStream filtrom = new FileOutputStream(file); ObjectOutputStream objektstrom = new ObjectOutputStream(filtrom)) { String res = text.getText(); objektstrom.writeObject(res); } catch (FileNotFoundException e) { System.err.println("не удалось создать файл " + file.getName()); e.printStackTrace(); } catch (IOException e) { System.err.println("Проблема с записью в файл " + file.getName()); e.printStackTrace(); } } } 

}

fully working code. Finalize to your wishes a little)
used scanner while reading. Not always digestible thing can be used here and possible exceptions threw.

  public static String ReadButton(){ try (FileInputStream filstrom = new FileInputStream(file); ObjectInputStream objektstrom = new ObjectInputStream(filstrom)) { innObjekt = objektstrom.readObject(); if (innObjekt instanceof [...]) { resultat = ([...])innlestObjekt; Object objekt = resultat; if (!(objekt instanceof [...])) { System.err.println("Ошибка типа объекта"); return null; } } } catch (FileNotFoundException e) { System.err.println("Файл не готов " + file.getName()); e.printStackTrace(); } catch (IOException e) { System.err.println("Проблема с чтением файла " + file.getName()); e.printStackTrace(); } catch (ClassNotFoundException e) { System.err.println("Fila " + file.getName() + " содержит не известный объект класса"); e.printStackTrace(); } return resultat; } 

}