Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Code for deleting a line

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 23-01-2010
Member
 
Join Date: Dec 2009
Posts: 263
Code for deleting a line
  

Hi,
I am trying to implement transactions / manipulations that take place in a text file that is adding a line, deletion, update etc it looks like a small base of data.The problem is that given a line I do not know how its suppression. Can you help me, if any code is available on this, then please do post back, so that I can study on it. I trying to write a code for deleting a line in a text file. Thank you for your attention

Reply With Quote
  #2  
Old 23-01-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: Code for deleting a line

Hello,
You can position yourself at the end of the line and make characters backspace ( '\ b') until the beginning of the line. This is just pone way of doing it. The backslash, the line is removed only to the display, it is still present in the file and may corrupt total number of characters, the number of words etc. The only effective solution is to create a temporary file, copy all lines of source file (except to remove) temporary in this file, then rename the temporary file with the name of the original file. This is a tedious work but effective.
Reply With Quote
  #3  
Old 23-01-2010
Member
 
Join Date: Dec 2009
Posts: 263
Re: Code for deleting a line

Hello,
Yes that's what I had done too, but it remains to rename the temporary file. Is there a method that does this work effective? I think this will consume a lot of time and the code will the lengthy, is there any other alternative for this, so that the code will be shorten and the time is saved, and there will be no need for the temporary file. Thank you for your reply
Reply With Quote
  #4  
Old 23-01-2010
Member
 
Join Date: Feb 2008
Posts: 1,845
Re: Code for deleting a line

Hello,
If you do not want to close the file then you can close the stream as instructed below
Code:
test.delete();
And
Code:
test.renameTo(myfile)
If I am not wrong this will return false and true if it is not.
Reply With Quote
  #5  
Old 23-01-2010
Member
 
Join Date: Dec 2009
Posts: 263
Re: Code for deleting a line

Hello,
Flows on the files are closed. So, this solution does not works. But renameTo returns false because the other file already exists and so I have to delete one before the another. Thank you for your help. Therefor the rename method works and I can use it but for the deleting the file the above method is not useful, so if you have any other method then post back with the same.
Reply With Quote
  #6  
Old 23-01-2010
Member
 
Join Date: May 2008
Posts: 2,006
Re: Code for deleting a line

Hello,
Take a look at the following code, it can help you, the methods used here are the general methods for the performing the task
Code:
Public static boolean readReplace(String fileName, String oldPattern,
            String replPattern, int lnNumber) {
        String ln;
        StringBuffer sub = new StringBuffer();
        int lnrd = 0;
        try {
            FileInputStream fin = new FileInputStream(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    fin));
            while ((ln = reader.readLine()) != null) {
                lnrd + +;
                ln = ln.toLowerCase();
 
                if (lnrd == lnNumber) {
                    ln = ln.replaceFirst(oldPattern.toLowerCase(),
                            replPattern);
                }
                sub.append(ln + "\ n");
            }
            reader.close();
            BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
            out.write(sub.toString());
            out.close();
 
        } catch (Exception e) {
            return false;
        }
        return true;
    }
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Code for deleting a line"
Thread Thread Starter Forum Replies Last Post
Error code: 536821760 at line 1773 Max-well} Windows Software 3 05-04-2011 06:52 PM
What is the Command Line Code to run program for torrent download Lohitaksha Technology & Internet 6 26-06-2010 12:11 AM
Deleting line with sed command in unix/linux Fragman Software Development 5 20-03-2010 09:47 PM
unable to open oeaccess.inf Error code 0x2 at line 0 ricardoramey Operating Systems 3 14-10-2009 01:52 PM
How can you prevent deleting folders but allow deleting files a.dorst Operating Systems 1 24-08-2009 09:47 PM


All times are GMT +5.5. The time now is 05:58 AM.