将项目添加到文本文件

时间:2019-01-11 18:03:30

标签: java netbeans jframe

我正在创建一个预订系统,当我填写预订要求并按保存时,它们将被添加到名为“ bookings”的文本文件中。有关如何执行此操作的任何建议?

这是FileIo代码(您在其中读写预订文本文件的地方):

import java.io.*;
import java.nio.file.*;

public class FileIO {
    public static String readTextFile(String filepath){//This method will 
be reading the items in the text file and returing them as a string
    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 个答案:

没有答案