Results 1 to 6 of 6

Thread: Code for deleting a line

  1. #1
    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

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    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.

  3. #3
    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

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    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.

  5. #5
    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.

  6. #6
    Join Date
    May 2008
    Posts
    2,012

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

Similar Threads

  1. Error code: 536821760 at line 1773
    By Max-well} in forum Windows Software
    Replies: 3
    Last Post: 05-04-2011, 06:52 PM
  2. What is the Command Line Code to run program for torrent download
    By Lohitaksha in forum Technology & Internet
    Replies: 6
    Last Post: 26-06-2010, 12:11 AM
  3. Deleting line with sed command in unix/linux
    By Fragman in forum Software Development
    Replies: 5
    Last Post: 20-03-2010, 09:47 PM
  4. unable to open oeaccess.inf Error code 0x2 at line 0
    By ricardoramey in forum Operating Systems
    Replies: 3
    Last Post: 14-10-2009, 01:52 PM
  5. Replies: 1
    Last Post: 24-08-2009, 09:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,713,289.24457 seconds with 17 queries