简体   繁体   中英

How to modify a txt file with a temporary file in java?

just wondering why it is not working the code we are using... i create a program that manages products of a store, in a specific of the program the user can add or delete products that are stored in the warehouse, the application shows a grid, that represents part of the store, referring with a ProductDatabase that is uploaded when the user add the product or delete it. for the adding function no problems... when i try to delete a product i read the ProductDatabase, i get the string of which product to delete, i create a temporary file copying in it all the data without the string line referring to the prodcuct i have to delete, then i rewrite the ProductDatabase. at this point i should have uploaded the Database by deleting the product and the specific file line. i don't know why it works and sometimes it doesn't. well i know it sounds strange but that is what happens! this is the code:

 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);
    }
} 

Best bet is to change the file path to absolute path in inputfile .

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

Change it to -

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

You can find more about this issue at this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM