Results 1 to 3 of 3
  1. #1
    MGT Madness is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Removing a specific line from a text file that contains a certain string

    So basically, if a line in a text file contains a certain string, that specific line will be deleted.

    It should probably be similair to this method?


    Java Code:
    /**
    		 * Replace text.
    		 * @param replace
    		 * 			The text to replace.
    		 * @param replaceWith
    		 * 			The text to replace with.
    		 */
            public static void replaceSelected(String replace, String replaceWith)
            {
                    try
                    {
                            BufferedReader file = new BufferedReader(new FileReader("data/replacer.txt"));
                            String line;
                            String input = "";
                            while ((line = file.readLine()) != null) input += line + '\n';
                            input = input.replace(replace, replaceWith);
                            FileOutputStream File = new FileOutputStream("data/replacer.txt");
                            File.write(input.getBytes());
                    }
                    catch (Exception e)
                    {
                            System.out.println("Problem reading file.");
                    }
            }

    - Thank you very much for your time.



    EDIT: This is the solution

    Java Code:
    public static void deleteLine()
    	{
    		try
            {
                    BufferedReader file = new BufferedReader(new FileReader("data/line.txt"));
                    String line;
                    String input = "";
                    while ((line = file.readLine()) != null) 
                    {
                    	//System.out.println(line);
                    	if (line.contains("Username:"))
                    	{
                    		line = "";
                    		System.out.println("Line deleted.");
                    	}
                    	input += line + '\n';
                    }
                    FileOutputStream File = new FileOutputStream("data/line.txt");
                    File.write(input.getBytes());
                    file.close();
                    File.close();
            }
            catch (Exception e)
            {
                    System.out.println("Problem reading file.");
            }
    	}
    Last edited by MGT Madness; 03-08-2014 at 09:01 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Removing a specific line from a text file that contains a certain string

    What problem(s) are you having?
    What happens when you compile and execute the code?
    Give examples of the file being changed before and after the change.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    MGT Madness is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: Removing a specific line from a text file that contains a certain string

    Quote Originally Posted by Norm View Post
    What problem(s) are you having?
    What happens when you compile and execute the code?
    Give examples of the file being changed before and after the change.
    What the currently posted method does is replace a certain string, with another. On the text file. This method works 100%

    What i would like is if a line(s), has a specific string, then that line would be deleted.


    Thank you for your time :)

    EDIT: Already solved
    Last edited by MGT Madness; 03-08-2014 at 09:00 AM.

Similar Threads

  1. removing the last blank line from txt file
    By heartysnowy in forum New To Java
    Replies: 8
    Last Post: 12-06-2013, 01:09 PM
  2. Reading a specific line from a file
    By efebatistaarda in forum New To Java
    Replies: 6
    Last Post: 02-22-2011, 02:57 PM
  3. writing to specific line in text file
    By mickmos in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 01:01 PM
  4. Writing To A Specific Text File Line
    By mokonji in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 08:13 PM
  5. Replies: 1
    Last Post: 01-11-2008, 10:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •