如何在文本文件的特定部分覆盖整数?

时间:2019-03-26 05:08:03

标签: java file overwrite

我有一个名为“ BookDetails.txt”的文本文件

它的格式如下:书名,作者,出版商,分支机构电话号码和副本数:

  

1984:乔治·奥威尔:企鹅:国际汽联:23

在我的程序中,当顾客检查特定的书时,我试图将副本的数量从23覆盖为22(实质上减少一)

public class CheckOutDialog extends javax.swing.JDialog {
private static final DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
ArrayList<CheckOut> checkOuts = new ArrayList<CheckOut>();
String bookTitle; 
int numberOfCopies;
/** 
 * Creates new form CheckOutDialog
 */
public CheckOutDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    Date currentDate = new Date();
    jTextFieldDate.setText(df.format(currentDate));
}


private void jButtonCheckOutActionPerformed(java.awt.event.ActionEvent evt) {                                                
    String firstName = jTextFieldFirstName.getText();
    String lastName = jTextFieldLastName.getText();
    bookTitle = jTextFieldBookTitle.getText();
    String checkOutDate = jTextFieldDate.getText();

    CheckOut checkOutInfo = new CheckOut(firstName, lastName, bookTitle, checkOutDate);

    checkOuts.add(checkOutInfo);

    CheckOutCopy();
}                                               

public void CheckOutCopy() //This method checks for book and num of copies
{
     try {
        File f = new File("BookDetails.txt");
        Scanner fileRead = new Scanner(f);

        boolean foundTitle = false;
        fileRead.nextLine();
        while(fileRead.hasNextLine())
        {
            String textLine = fileRead.nextLine();
            String[] bookInfo = textLine.split(" : ");
            String tempBookTitle = bookInfo[0];
            numberOfCopies = Integer.parseInt(bookInfo[4]);


            if(tempBookTitle.trim().equals(bookTitle))
            {
                foundTitle = true;
                break;
            }
        }
        if(foundTitle && numberOfCopies > 0)
        { 
           OverwriteCopies();
           WriteCheckOut();
           this.setVisible(false);
        }
            else if(numberOfCopies == 0)
            {
                if(JOptionPane.showConfirmDialog(null, "Would you like to add the patron to the queue?", "No copies available", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
                {
                    AddPatronQueue();
                }
                else
                {
                    JOptionPane.getRootFrame().dispose();
                }
            }
        else
        {
            JOptionPanes.messageBox("Book was not found in Library Catalog", "Check Out Error");
        }

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    }
}

 private void OverwriteCopies()
{
    numberOfCopies--;

    //Unsure how to proceed from here
}


private void WriteCheckOut()
{
    WriteFile wf = new WriteFile("CheckOutDetails.txt");

    for(int i = 0; i < checkOuts.size(); i++)
    {
        CheckOut c = checkOuts.get(i);

        String checkOutDetails =  c.getFirstName() + " : " + c.getLastName() + " : " + c.getBookTitle() + " : " + c.getCheckOutDate();

        wf.write(checkOutDetails);
    }
    wf.close();
}

我的程序成功找到了特定的书及其副本数(如果还剩0册,它会生成JOptionPane)。但是,我不确定如何覆盖和更新文本文件中的特定元素。我做了一些研究,似乎RandomAccessFile可能是一种可能的选择。 RAF是解决我问题的可行解决方案,还是我可以采取另一种选择?

0 个答案:

没有答案