Добавление элементов в текстовый файл - PullRequest
0 голосов
/ 11 января 2019

Я создаю систему бронирования, в которой, когда я заполняю требования бронирования и нажимаю сохранить, они будут добавлены в текстовый файл под названием «бронирование». Есть предложения о том, как я могу это сделать?

Это код FileIo (где вы читаете и пишете текстовый файл бронирований):

public class FileIO {
    // This method will be reading the items in the text file and returing them as a
    // string
    public static String readTextFile(String filepath) {
        String line = null;
        Path path = Paths.get("bookings.txt");
        try {
            BufferedReader reader = null; // Readinf the text file
            reader = Files.newBufferedReader(path);

            while ((line = reader.readLine()) != null)// Processes each line hat it is not empty
            {
                System.out.println(line);
            }
            reader.close();
        } catch (IOException io) {
            System.out.println("Error while reading file");
        }
        return line;
    }

    public static void appendToTextFile(String filePath, String toWrite)// This will write a string to the file
    {
        toWrite = toWrite.replace("\n", "").replace("\r\n", "");// To return in a new line
        Path path = Paths.get(filePath);

        try {
            BufferedWriter writer = null;

            if (!Files.exists(path))// If the files doesnt exist
                writer = Files.newBufferedWriter(path, StandardOpenOption.CREATE);
            else {
                writer = Files.newBufferedWriter(path, StandardOpenOption.APPEND);
                writer.write(toWrite);
                writer.newLine();
            }
            writer.close();// Closing the file 'writer'
        } catch (IOException io) {
            System.out.print("Error while writing to file");
        }
    }

    public static void main(String[] args) {
        appendToTextFile("bookings.txt", "");

    }
}

А это код JFrame, где заполняются детали бронирования:

private void savebActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:

    boolean passed = true;  

    String Name = namefield.getText(); //Getting the text from the text field
    String Surname = surnamefield.getText(); //Getting the text from the text field
    String Contact = contactfield.getText(); //Getting the text from the text field

    //If Name is empty or invalid items it will pop up a message saying that it is Invalid.
    if(Name.isEmpty()||!Name.matches("[a-zA-Z]*"))//If Name is empty or does not match 
    {
        JOptionPane.showMessageDialog(null,"Invalid Name"); //This will pop up the JOption Pane 
        passed = false; //If passed then no validation will pop up
    }
     //If Surame is empty or invalid items it will pop up a message saying that it is Invalid.
    if(Surname.isEmpty()||!Surname.matches("[a-zA-Z]*"))//If Surame is empty or does not match 
    {
        JOptionPane.showMessageDialog(null,"Invalid Surname"); //This will pop up the JOption Pane 
        passed = false; //If passed then no validation will pop up
    }
     //If Contact is empty or invalid items it will pop up a message saying that it is Invalid.
    if(Contact.isEmpty())//If Contact is empty
    {
        JOptionPane.showMessageDialog(null,"Invalid Contact"); //This will pop up the JOption Pane 
        passed = false; //If passed then no validation will pop up
    }




    this.setVisible(false);//This hides the Makeabooing JFrame
    new ViewaBooking().setVisible(true);//This will open a new ViewaBooking JFrame

}                             
...