search and modify a string in a file

Joined
May 15, 2011
Messages
3
Reaction score
0
Hello i'm trying to make a search in a txt file and if i find what i'm looking for i want to update it! for example i have this data (d1#1#200#good) which is (Id,quantity,price,description)
What i want to do is if i give an input whith the same id i want to update the quantity
ex: di,2,200,good =>di#3#200#good!!!
I have this code but it doesn't work!! eny helps please...!!!

Code:
public void modifyItem(String filename, String id) {

        try {
            boolean check = false;
            String tempfid = null, tempsname = null;/
            int change = 1;
            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream(filename);
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                //append(save) se arxeio txt
                // Print the content on the console
                String[] temp = null;
                temp = lineDelimiter(strLine, 4, "#");  


                if (temp[0].equals(id)) {
                    check = true;
                    Items stud = new Items();
                    stud.setId(temp[0]);
                    stud.Posotita=Integer.getInteger(temp[1]);
                    stud.Price=Double.parseDouble(temp[2]);
                    stud.setPerigrafi(temp[3]);

                    change = stud.showChangeDetails();
                    if (change == 0) {
                        stud.addStoixeia();
                        strLine = stud.toString();

                        tempfid = stud.getId();

                    }
                }
                Items eggrafhstoarxeio = new Items();
                eggrafhstoarxeio.write2Txt(strLine, "temp.txt");
                //dhmiourgia tou temp.txt me allagmenh thn mia grammh

            }
            if (check == false) {
                String msg =
                        String.format("Δε βρέθηκε Item με code <" + id+">");
                JOptionPane.showMessageDialog(null, msg, "Modify item - not found!", JOptionPane.WARNING_MESSAGE);
            }
            //Close the input stream
            in.close();
            String result3[] = findFullName("temp.txt", id);
            if (result3[1] != null)//an dld uparxoun 2 akrivws idies egrafes
            {
                String msg =
                        String.format("Δυστυχώς υπάρχει εγγραφή με αυτό το id, η αλλαγή δεν πραγματοποιείται");
                JOptionPane.showMessageDialog(null, msg, "Modify item - already exists!", JOptionPane.ERROR_MESSAGE);
                File oldbackup = new File("temp.txt");
                oldbackup.delete();

            } else {
                rename("temp.txt", filename);
                if (change == 0) {
                    String msg = String.format("Η εγγραφή άλλαξε"); //an pathsw NO pio panw apli to emfanizei
                    JOptionPane.showMessageDialog(null, msg, "Modify item - successfully changed!", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        } catch (Exception e) {//Catch exception if any
            //System.err.println("Error: " + e.getMessage());
            String message1 =
                    String.format(e.getMessage());
            JOptionPane.showMessageDialog(null, message1, "file not found", JOptionPane.WARNING_MESSAGE);
        }
    }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top