有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将项目添加到文本文件

我正在创建一个预订系统,当我填写预订要求并按save键时,它们将被添加到名为“bookings”的文本文件中。有什么建议吗

这是FileIo代码(您正在读取和写入bookings文本文件):

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

}                             

共 (0) 个答案