【问题标题】:How to modify a txt file with a temporary file in java?如何用java中的临时文件修改txt文件?
【发布时间】:2012-11-27 16:03:49
【问题描述】:

只是想知道为什么它不能在我们使用的代码中运行... 我创建了一个管理商店产品的程序,在特定的程序中,用户可以添加或删除存储在仓库中的产品, 该应用程序显示了一个网格,它代表了商店的一部分,引用了一个 ProductDatabase,当用户添加或删除产品时上传该产品数据库。 对于添加功能没问题...当我尝试删除产品时,我读取了 ProductDatabase,我得到了要删除的产品的字符串,我创建了一个临时文件,在其中复制了所有数据,而没有引用产品的字符串行我必须删除,然后我重写 ProductDatabase。 此时我应该通过删除产品和特定文件行来上传数据库。我不知道为什么它会起作用,有时它不会。好吧,我知道这听起来很奇怪,但这就是发生的事情! 这是代码:

 import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.Toolkit;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.io.*;
 import javax.swing.JOptionPane;

/**
 *
 * @author Kastriot
 */
public class Preleva_Prodotto extends javax.swing.JFrame {

/** Creates new form Ricerca_Prodotto */
public Preleva_Prodotto() {
    initComponents();

    String fileLine;

    try {
        FileReader fileReader = new FileReader("DatabaseProdotti.txt");
        BufferedReader bufReader = new BufferedReader(fileReader);

        int i = 0;
        String[] Prodotto = new String[21];
        String[] DetProd = new String[6];

        while ((fileLine = bufReader.readLine()) != null) {

            Prodotto[i] = fileLine;
            DetProd = Prodotto[i].split(",");
            NomeProdotto = DetProd[0];
            Descrizione = DetProd[1];
            Peso = Integer.parseInt(DetProd[2]);
            CodiceID = DetProd[3];
            Posizione = DetProd[4];
            DataReg = DetProd[5];



            if (Peso <= 500) {
                if (Posizione.equals("C0")) {
                    jLabel1.setText("<html>" + NomeProdotto);
                    jPanel3.setBackground(Color.red);
                } else if (Posizione.equals("C1")) {
                    jLabel13.setText("<html>" + NomeProdotto);
                    jPanel4.setBackground(Color.red);
                } else if (Posizione.equals("C2")) {
                    jLabel14.setText("<html>" + NomeProdotto);
                    jPanel5.setBackground(Color.red);
                } else if (Posizione.equals("C3")) {
                    jLabel15.setText("<html>" + NomeProdotto);
                    jPanel2.setBackground(Color.red);
                } else if (Posizione.equals("C4")) {
                    jLabel16.setText("<html>" + NomeProdotto);
                    jPanel6.setBackground(Color.red);
                } else if (Posizione.equals("C5")) {
                    jLabel17.setText("<html>" + NomeProdotto);
                    jPanel7.setBackground(Color.red);
                } else if (Posizione.equals("C6")) {
                    jLabel18.setText("<html>" + NomeProdotto);
                    jPanel8.setBackground(Color.red);
                }


            } else if (Peso <= 1000) {
                if (Posizione.equals("B0")) {
                    jLabel19.setText("<html>" + NomeProdotto);
                    jPanel16.setBackground(Color.red);
                } else if (Posizione.equals("B1")) {
                    jLabel20.setText("<html>" + NomeProdotto);
                    jPanel17.setBackground(Color.red);
                } else if (Posizione.equals("B2")) {
                    jLabel21.setText("<html>" + NomeProdotto);
                    jPanel18.setBackground(Color.red);
                } else if (Posizione.equals("B3")) {
                    jLabel22.setText("<html>" + NomeProdotto);
                    jPanel19.setBackground(Color.red);
                } else if (Posizione.equals("B4")) {
                    jLabel23.setText("<html>" + NomeProdotto);
                    jPanel20.setBackground(Color.red);
                } else if (Posizione.equals("B5")) {
                    jLabel24.setText("<html>" + NomeProdotto);
                    jPanel21.setBackground(Color.red);
                } else if (Posizione.equals("B6")) {
                    jLabel25.setText("<html>" + NomeProdotto);
                    jPanel22.setBackground(Color.red);
                }


            } else if (Peso > 1000) {
                if (Posizione.equals("A0")) {
                    jLabel26.setText("<html>" + NomeProdotto);
                    jPanel9.setBackground(Color.red);
                } else if (Posizione.equals("A1")) {
                    jLabel27.setText("<html>" + NomeProdotto);
                    jPanel10.setBackground(Color.red);
                } else if (Posizione.equals("A2")) {
                    jLabel28.setText("<html>" + NomeProdotto);
                    jPanel11.setBackground(Color.red);
                } else if (Posizione.equals("A3")) {
                    jLabel29.setText("<html>" + NomeProdotto);
                    jPanel12.setBackground(Color.red);
                } else if (Posizione.equals("A4")) {
                    jLabel30.setText("<html>" + NomeProdotto);
                    jPanel13.setBackground(Color.red);
                } else if (Posizione.equals("A5")) {
                    jLabel31.setText("<html>" + NomeProdotto);
                    jPanel14.setBackground(Color.red);
                } else if (Posizione.equals("A6")) {
                    jLabel32.setText("<html>" + NomeProdotto);
                    jPanel15.setBackground(Color.red);
                }


            }

        }

    } catch (IOException ex) {
        Logger.getLogger(PianoB.class.getName()).log(Level.SEVERE, null, ex);
    }

}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    String lineToRemove = jTextField1.getText();

    if ((!lineToRemove.equals("A0")) && (!lineToRemove.equals("A1")) && (!lineToRemove.equals("A2")) && (!lineToRemove.equals("A3")) && (!lineToRemove.equals("A4")) && (!lineToRemove.equals("A5")) && (!lineToRemove.equals("A6"))
            && (!lineToRemove.equals("B0")) && (!lineToRemove.equals("B1")) && (!lineToRemove.equals("B2")) && (!lineToRemove.equals("B3")) && (!lineToRemove.equals("B4")) && (!lineToRemove.equals("B5")) && (!lineToRemove.equals("B6"))
            && (!lineToRemove.equals("C0")) && (!lineToRemove.equals("C1")) && (!lineToRemove.equals("C2")) && (!lineToRemove.equals("C3")) && (!lineToRemove.equals("C4")) && (!lineToRemove.equals("C5")) && (!lineToRemove.equals("C6"))) {
        JOptionPane.showMessageDialog(this, "La posizione inserita non è esistente, inserire una posizione libera tra quelle disponibili!", "Operazione non riuscita!", JOptionPane.ERROR_MESSAGE);
        PosizioneVuota = false;
    }

    try {

        File inputFile = new File("DatabaseProdotti.txt");
        File tempFile = new File("C:\\Users\\Kastriot\\Documents\\Università\\tmpFileProdotti.txt");
        BufferedReader reader = new BufferedReader(new FileReader(inputFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

        String[] DetProd = new String[100];
        String fileLine;
        while ((fileLine = reader.readLine()) != null) {
            // trim newline when comparing with lineToRemove
            DetProd = fileLine.split(",");
            PosDaPrelevare = DetProd[4];

            if (!PosDaPrelevare.equals(lineToRemove)) {
                writer.write(fileLine);
                writer.newLine();
            } else {
                JOptionPane.showMessageDialog(this, "Il prodotto presente nella posizione " + (lineToRemove) + " è stato prelevato con successo!", "Operazione riuscita", JOptionPane.INFORMATION_MESSAGE);
                PosizioneVuota = false;
                NuovaSchermata = true;


            }
        }
        reader.close();
        writer.flush();
        writer.close();
        inputFile.delete();

        tempFile.renameTo(new File("C:\\Users\\Kastriot\\Documents\\NetBeansProjects\\EasyStorage1.0\\DatabaseProdotti.txt"));


        if (PosizioneVuota) {
            JOptionPane.showMessageDialog(this, "La posizione " + (lineToRemove) + " non contiene alcun prodotto", "Operazione non riuscita", JOptionPane.INFORMATION_MESSAGE);
        }
        if (NuovaSchermata) {
            Situazione_Generale_ SitGenFrame = new Situazione_Generale_();              //Apre la schermata Situazione Generale
            //Centrare la finestra e mostrala sullo schermo
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            SitGenFrame.setSize(950, 480);
            Dimension frameSize = SitGenFrame.getSize();
            SitGenFrame.setLocation((screenSize.width - frameSize.width) / 2,
                    ((screenSize.height - frameSize.height) / 2));

            SitGenFrame.setVisible(true);
        }

    } catch (IOException ex) {
        Logger.getLogger(Preleva_Prodotto.class.getName()).log(Level.SEVERE, null, ex);
    }
} 

【问题讨论】:

  • 你能告诉错误是什么吗?
  • 没有! =(有时产品被删除,有时什么也没有发生!
  • 好的,感谢您对代码的评论...这是我们制作的第一个程序。希望你第一次变得更好......
  • 评论inputFile.delete(),看看tmpFileProdotti.txt是否存在?
  • 我想知道为什么有时您使用相对路径而有时使用绝对路径。提示:即使在 Windows 上,您也可以使用正斜杠,以避免这些丑陋的双反斜杠...您应该检查 delete() 的结果,它可能会失败。在这种情况下,重命名也可能会失败。

标签: java database grid store temporary-files


【解决方案1】:

最好的办法是将inputfile中的文件路径更改为绝对路径。

File inputFile = new File("DatabaseProdotti.txt");

改成-

File inputFile = new File("C:\\Users\\Kastriot\\Documents\\NetBeansProjects\\EasyStorage1.0\\DatabaseProdotti.txt");

您可以在question 找到有关此问题的更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2022-06-28
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    相关资源
    最近更新 更多