Hi,I need to edit in a file without deleting all the data.
Thanks

As I do not know which exactly writer you using only thing I can say is to look for append() method which will be either directly implemented in used writer class or inherited from other classes above this one

Hi,I need to edit in a file without deleting all the data.
Thanks

do you need to edit the data in a file, append data, delete parts of the file (without deleting ALL of the data, as you put it), ...

anyway, open notepad, type something, close and save it.
re-open the file, add a line, press 'ctrl+s' and your job is done.

don't need to thank me, the bill 'll come later :P

if your task is to create an application that does such, start writing code, begin with the easy parts, and come back here when you get stuck.

when you do so, post the relevant code, post what it should do, post what it is doing, post any error messages and whatever you think WE think might be relevant

first i know how to open a file and write in it and save it I'm not gonna waste my time for such a thing.second my problem is i write to the file customer data and then the customer can edit it so i want to edit this data in the file (adding the new one and deleting the old data)I opened a temp file but it's not working can i do it without temp files?how?.
Thanks

first i know how to open a file and write in it and save it I'm not gonna waste my time for such a thing.second my problem is i write to the file customer data and then the customer can edit it so i want to edit this data in the file (adding the new one and deleting the old data)I opened a temp file but it's not working can i do it without temp files?how?.
Thanks

when you want to change data in a txt file (manually) do you first create a temp file? no, so why would this be different?
just overwrite what you want to change

@nouryn - if you can provide better description of user editing process we may able to help. However right now as you can see from the responses it is not clear what you try to do. Some supporting coding would be welcome...

Are you familiar with the 'append' argument of the FileWriter ?

BufferedWriter writer= new BufferedWriter(new FileWriter(file, true));

When you try to write to the file, the new data will be appended after the existing

hi,sorry that i didn't describe my program clear enough.
i have a file contain the following
123 Nouryn current =5000
124 Jasmine investment =300

the customer want to add to his account 300 so i search for his name and then search for ("=")so he take the credit and store it so the customer can add on it.
the user want to add 300 so the new credit will be 5300 this data must be written in the file but after deleting the old one how to do it.

now i;m trying to put the new data and the old ones in a new file but only the data after the edited line is written:s.i've attach my code hope i explained well this time.
help plllllllllllllllzzzzzzzzzzzzzz

Attachments
package project1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class DeleteFile2 {

    public void DeleteFile2(String str2) {
        File f=new File("C:\\Users\\nouryn\\Desktop\\NEW FRAME\\bank new\\BankFileEdited.txt");
        FileOutputStream fop;
        
        try {
           
           
              if(f.exists()){
                         fop = new FileOutputStream(f,true);
               try {
                   
                   fop.write(str2.getBytes());
               fop.close();
               } catch (IOException e) {
                   // TODO
               }
               try {
                   fop.flush();
               } catch (IOException e) {
                   // TODO
               }
               try {
               
                   fop.close();
               } catch (IOException e) {
                   // TODO
               }
               System.out.println("The data has been written");
                     }

                     else
                       System.out.println("This file is not exist");
               } catch (FileNotFoundException e) {
               // TODO
             }
    }
    }
package project1;

import java.awt.Dimension;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class OldCustomer extends JFrame {
    private JLabel name = new JLabel();
    private JTextField nameText = new JTextField();
    private JTextField accountText = new JTextField();
    private JLabel jLabel2 = new JLabel();
    private JComboBox accType = new JComboBox();
    private JButton search = new JButton();
    private JButton exit = new JButton();
    private JButton loans = new JButton();

    public OldCustomer() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
    
        accType.addItem("Types Of Account");
        accType.addItem("Current");
        accType.addItem("Investment");
        accType.addItem("Commercial");
        
        
        this.getContentPane().setLayout( null );
        this.setSize( new Dimension(400, 400) );
        this.setTitle("Old Customer");
        this.setBounds(new Rectangle(550, 150, 400, 400));
        name.setText("Name:");
        name.setBounds(new Rectangle(15, 30, 95, 35));
        name.setToolTipText("null");
        nameText.setBounds(new Rectangle(90, 30, 240, 35));
        accountText.setBounds(new Rectangle(115, 90, 215, 35));
        jLabel2.setText("Account Number:");
        jLabel2.setBounds(new Rectangle(10, 90, 150, 35));
        accType.setBounds(new Rectangle(5, 165, 200, 45));
        search.setText("Search ");
        search.setBounds(new Rectangle(245, 165, 125, 50));
        search.setToolTipText("null");
        search.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        search_actionPerformed(e);
                    }
                });
        exit.setText("Exit");
        exit.setBounds(new Rectangle(245, 250, 125, 50));
        exit.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        exit_actionPerformed(e);
                    }
                });
        loans.setText("Loans");
        loans.setBounds(new Rectangle(35, 250, 125, 50));
        this.getContentPane().add(loans, null);
        this.getContentPane().add(exit, null);
        this.getContentPane().add(search, null);
        this.getContentPane().add(accType, null);
        this.getContentPane().add(jLabel2, null);
        this.getContentPane().add(accountText, null);
        this.getContentPane().add(nameText, null);
        this.getContentPane().add(name, null);
    }

    private void exit_actionPerformed(ActionEvent e) {
    System.exit(0);
    }

    private void search_actionPerformed(ActionEvent e) {
        String nt=nameText.getText();
        String at=accountText.getText();
        int r1=  accType.getSelectedIndex();
         switch(r1){
        
         case 1:
             String curr="Current Account";
             SearchFile sf1=new SearchFile();
              double c1=sf1.SearchAccount(nt);
            if(c1!=0){
             String ct1=at+"     "+nt+"      "+curr+"="+c1;
         DeleteFile2 af1=new DeleteFile2();
          af1.DeleteFile2(ct1);
     }
         break;
         
         case 2:
             String inves="Investment Account";
             SearchFile sf2=new SearchFile();
             double c2=sf2.SearchAccount(nt);
             String ct2=at+"     "+nt+"      "+inves+"="+c2;
             DeleteFile2 af2=new DeleteFile2();
              af2.DeleteFile2(ct2);
              
             break;
             
             case 3:
             String comm="Commercial Account";
             SearchFile sf3=new SearchFile();
             double c3=sf3.SearchAccount(nt);
             String ct3=at+"     "+nt+"      "+comm+"="+c3;
             DeleteFile2 af3=new DeleteFile2();
              af3.DeleteFile2(ct3);
              
             break;
         }
    }
}
package project1;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import javax.swing.JOptionPane;

public class SearchFile {
    
    public void SearchFile(){
   
    byte[] infile = new byte[10000000];

    File f=new File("C:\\Users\\nouryn\\Desktop\\NEW FRAME\\bank new\\BankFile.txt");
    
    FileInputStream fin;
   try
   {
    fin= new FileInputStream(f);
         
                                     BufferedInputStream bin = new BufferedInputStream(fin);
                                     DataInputStream din = new DataInputStream(bin);
                                 
                                     
                                             
            try {
                int filelength = din.read(infile);
             
            String filestring = new String(infile, 0, filelength);
                                             
                       //  String filestring= din.readLine();
                                          String s=JOptionPane.showInputDialog("Enter The Search Word");
             int index1=filestring.indexOf(s);
          int indexNewLine = filestring.indexOf("\n",index1);
       
       
       if(index1!=-1) {
           JOptionPane.showMessageDialog(null,"THE NAME FOUND");
          
       System.out.println("UR SEarRCh="+filestring.substring(index1,indexNewLine));
       }
           else {
           JOptionPane.showMessageDialog(null,"NOT FOUND");
           
           }
       } catch (IOException e) {
                  // TODO
              }   
                                          
   } catch (FileNotFoundException e) {
            // TODO
        }
  
    }
    public double SearchAccount(String s){
    
        byte[] infile = new byte[10000000];

        File f=new File("C:\\Users\\nouryn\\Desktop\\NEW FRAME\\bank new\\BankFile.txt");
        
        FileInputStream fin;
        try
        {
        fin= new FileInputStream(f);
             
                                         BufferedInputStream bin = new BufferedInputStream(fin);
                                         DataInputStream din = new DataInputStream(bin);
                                     
                                         
                                                 
                try {
                    int filelength = din.read(infile);
                 
                String filestring = new String(infile, 0, filelength);
                                                 
                          
                                            //  String s=JOptionPane.showInputDialog("Enter The Search Word");
                 int index1=filestring.indexOf(s);
              int indexNewLine = filestring.indexOf("\n",index1);
               int indexNew = filestring.indexOf("=",index1);
              
           
           if(index1!=-1) {
               JOptionPane.showMessageDialog(null,"THE NAME FOUND");
              
           System.out.println("UR SEarRCh="+filestring.substring(indexNew+1,indexNewLine));
         
            String s1=filestring.substring(indexNew+1,indexNewLine);

             
                    //  DeleteFile df=new DeleteFile();
     //       df.removeLineFromFile(s2);
           double oldCredit=Double.parseDouble(s1);
                  
            Double credit= Double.parseDouble(JOptionPane.showInputDialog(" Enter the money"));
               double newCredit= oldCredit+credit;
               JOptionPane.showMessageDialog(null,"ur new money"+newCredit);
          String s2;
          s2=filestring.substring(indexNew+5,filelength);
           // System.out.println(s2);
    
               DeleteFile2 e=new DeleteFile2();
               e.DeleteFile2(s2);
               return newCredit;
          
           }
               else {
               JOptionPane.showMessageDialog(null,"NOT FOUND");
               return 0;
               
               }
           } catch (IOException e) {
                      return 0;
                  }   
                                              
        } catch (FileNotFoundException e) {
                return 0;
            }
        
    }
}

A simple way to do it is read the entire file and store its data somewhere (for example a Vector). Then change the credit in the Vector, after you find the right element, and save the Vector with the new data back to the file

sorry but i don't know how to use vectors.can i do it with a different way??
Thanks

sorry but i don't know how to use vectors.can i do it with a different way??
Thanks

yes, you can use banana-splits. if you can't figure out how to use a Vector, don't bother with how to write/edit files

You can use patterns. Open the file with the data and store lines one by one to a string:

String str;
while (( str = br.readLine()) != null) {
// your code here
}

Inside the while loop, you can use patterns/matchers to see if the current line (str) has the name you are looking for. If it does, you can change the value after equal sign to whatever you want to. Then, add str to your new file.

If you want, after while loop you can delete the old file.

Here, a way without using vectors. Hope it helps...

So you guys are suggesting reading the entire file into Objects and then editing and writing it back out? Seems inefficient to me - there's no better way? I'm sure there's a way to just have a carat position and then write things there. Not saying your way isn't good, just curious.

Yes there are better ways of editing data at a random position; look into RandomAccessFile, though things can get complicated pretty soon.

This article has been dead for over six months. Start a new discussion instead.