Results 1 to 5 of 5
  1. #1
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Removing text in a .txt file

    Hello, I have a .txt file and I need to remove a certain string from the file.

    for example:

    {previous lines}
    LINE 17: # Arguments to VM
    LINE 18: java.args=-server -Xmx512m -Dsun.io.useCanonCaches=false -XX:MaxPermSize=192m -Xbatch -Dcoldfusion.rootDir={application.home}/ -Djava.security.policy={application.home}/servers/cfusion/cfusion-ear/cfusion-war/WEB-INF/cfusion/lib/coldfusion.policy -Djava.security.auth.policy={application.home}/servers/cfusion/cfusion-ear/cfusion-war/WEB-INF/cfusion/lib/neo_jaas.policy -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,ad dress=8000 -morearguments that can't be deleted
    LINE 19: ...etc

    I would need to remove "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,ad dress=8000".

    The code I have so far, reads in the file and creates a temp file. Copies everything up to the line containing "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,ad dress=8000" to the new file. Copies everything after the line containing "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,ad dress=8000" to the new file. But DOES NOT print any of the line containing "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,ad dress=8000" (line 18 in the above example).

    So the new temp file only contains lines 1-17 and 19-end of file, and is then replaced over the old file.

    How would I go about deleting that specific string and copying over line 18 as well?

    Thank you!

    Java Code:
    public static boolean removeConfigFile( String file )
        {
            try
            {
                File configFile = new File( file );
                File tempConfig = new File( "temp.config" );
    
    
                if( argumentsExist( file ) == true )
                {
                    PrintWriter out = new PrintWriter( new FileWriter( configFile, true ) );
                    PrintWriter tempOut = new PrintWriter( new FileWriter( tempConfig ) );
                    Scanner scan;
                    String str = null;
                    String delete = "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000";
    
                    scan = new Scanner( configFile );
                    str = scan.nextLine();
                    tempOut.println( "#" );
    
                    while( scan.hasNext() )
                    {
                        String currLine = scan.nextLine();
                        if( currLine.contains( "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=" ) )
                        {
                            currLine.replace(delete, "");
                            //tempOut.println( currLine);
                        }
                        else
                        {
                            tempOut.println( currLine );
                        }
                    }
                    
                    out.close();
                    tempOut.close();
                    scan.close();
    
                    move( file );
    
                    return true;
                    
                }
            }
            catch (Exception e)
            {
                
            }
            
            return false;
        }
    As you can see, I tried

    currLine.replace(delete, "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,ad dress=8000");

    but that doesn't seem to work, as when I print the currentLine, "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,ad dress=8000" is still included in it.

    Thanks again

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Removing text in a .txt file

    Quote Originally Posted by ralph View Post
    Java Code:
                            currLine.replace(delete, "");
    Strings are immutable (can't be changed), so the replace( ... ) method has to return the changed String; do this:

    Java Code:
    currLine= currLine.replace(delete, "");
    and print the new (changed) String to your file.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Removing text in a .txt file

    Quote Originally Posted by JosAH View Post
    Strings are immutable (can't be changed), so the replace( ... ) method has to return the changed String; do this:

    Java Code:
    currLine= currLine.replace(delete, "");
    and print the new (changed) String to your file.

    kind regards,

    Jos
    Ahh, that got it! Thanks a bunch! +rep

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Removing text in a .txt file

    Note that in your current code, when the line contains another portnumber than 8000 things don't work as you expect them to work.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Removing text in a .txt file

    Quote Originally Posted by gimbal2 View Post
    Note that in your current code, when the line contains another portnumber than 8000 things don't work as you expect them to work.
    I know, it's different in my code (I've created a separate class and use JVMArgumentsComposite.getPort() to get the port number from a drop down box). I just put a placeholder of 8000 for simplicity as that was irrelevant to the question :)

    Thanks again though ^_^

Similar Threads

  1. Removing characters from web log file
    By ultaabhi in forum New To Java
    Replies: 0
    Last Post: 02-02-2013, 07:44 PM
  2. Searching Text Files Then Removing (My First Post!)
    By technoreject in forum New To Java
    Replies: 12
    Last Post: 06-29-2011, 06:51 AM
  3. Removing a word from a txt file
    By MiitchKay in forum New To Java
    Replies: 3
    Last Post: 06-02-2011, 03:45 PM
  4. Help removing pronouns from text
    By jessie in forum New To Java
    Replies: 11
    Last Post: 11-19-2010, 05:45 AM
  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
  •