i'm still working on the property to let program. and it sort of works... its just when the user selects to delete a line from it it will delete it fine, but it will then duplicate all the items that where previously in it...

EG: My File looks like this to start with...

1. pe158ul,1,566
2. ln11ur,4,755
3. df336ty,77,800

then the user want to delete line 2 so they type in 1(because you have to enter 1 less than the actual number) and it then ends up looking like this

1. pe158ul,1,566
2. df336ty,77,800
3. pe158ul,1,566
4. ln11ur,4,755
5. df336ty,77,800

heres my code to do that bit... can anybody see why. I sure as hell cant...

if(i == 2)
            {
               try
               
               {          
                   System.out.println("\nEnter the line number of the property you wish to remove");
                   ln = Double.valueOf(br.readLine()).intValue();
               	   BufferedReader br=new BufferedReader(new FileReader("properties.txt"));
                   String line=null;
                   while ((line=br.readLine()) != null) 
                   {
                        String[] addy=line.split(",");
                        PropertyToLet ptl=new PropertyToLet(addy[0],addy[1],addy[2]);
                        v.addElement(ptl);
                   }        
                   
                   v.removeElementAt(ln);                   
                   PrintWriter outFile = new PrintWriter(new FileWriter("properties.txt"));
                   for (int i=0; i<v.size(); i++) 
                   {
                        PropertyToLet obj = (PropertyToLet) (v.get(i));
                        outFile.println(obj.getpc() + "," + obj.gethn() + "," + obj.getmr());      
                   }
                   outFile.close();
                }               
                catch(ArrayIndexOutOfBoundsException aioobe)
                {
                    System.out.println("An Error Has occurred");   
                }
                catch(IOException ex)
                {
                    ex.printStackTrace();
                }
                
            }

Recommended Answers

All 4 Replies

you haven't stated the problem, send us the problem
is it a syntax or compilation error
tell more details

the problem is that it will add the original contents of the file to the end of the new file

You are reading from and writing to the same file. After you are finished reading everything from it, delete the file, then write to the file. Then tell me if you are still having problems. Without reading the PrintWriter documentation, it seems like it is just appending the new text to the beginning of the file. Why it would do this, I don't know, but like I said, try deleting the file first.

Instead of PrintWriter use:
BufferedWriter.
When you write to the file, it overrides what it is written:

FileWriter fileWriter = new FileWriter("fileName"); //better check the API for this
BufferedWriter writer = new BufferedWriter(fileWriter);

writer.write("line");
writer.newLine(); 

// or writer.write("line\n");

writer.close();
// again check the API because I don't remember if FileWriter needs to be closed
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.