Results 1 to 6 of 6
  1. #1
    loggen is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Question How to delete a string middle of a text file?

    hi, i am new to java. i have a problm about saving and deleting line from a text file. Suppose i have a text file named "input.txt", it has multiple lines like,

    20007-James 007-James-Golden
    20006-James 006-James-Golden
    20005-James 005-James-Golden
    20004-James 004-James-Golden
    20003-James 003-James-Golden

    if i want to delete the 2nd line, the view would be

    20007-James 007-James-Golden
    20005-James 005-James-Golden
    20004-James 004-James-Golden
    20003-James 003-James-Golden

    And i want to save the file after i delet the 2nd line. Next when i open the file it should give me 4 line. can anybody help me with this?
    Last edited by loggen; 12-14-2008 at 10:37 AM.

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Create a temp file without the second line. After you are done delete original and rename the temp file to original.
    dont worry newbie, we got you covered.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  4. #4
    angelicsign is offline Member
    Join Date
    Dec 2008
    Posts
    25
    Rep Power
    0

    Default

    just try any ways,any things,any code to get the output you want 1st.
    after try it, if you find it difficult or some bugs cannot be solved, all senior programmers are willing to help you.
    There is the way how you learn things.

  5. #5
    loggen is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    i tried it. and it works fine. thank you guys.

  6. #6
    bishnu is offline Member
    Join Date
    Dec 2008
    Location
    Mumbai
    Posts
    5
    Rep Power
    0

    Smile string middle of a text file

    Hii all

    its better you should check this code,i think it will help you.

    import java.io.*;
    import java.util.Scanner;

    public final class ReadWithScanner {

    public static void main(String... aArgs) throws FileNotFoundException {
    ReadWithScanner parser = new ReadWithScanner("C:\\Temp\\test.txt");
    parser.processLineByLine();
    log("Done.");
    }

    /**
    * @param aFileName full name of an existing, readable file.
    */
    public ReadWithScanner(String aFileName){
    fFile = new File(aFileName);
    }

    /** Template method that calls {@link #processLine(String)}. */
    public final void processLineByLine() throws FileNotFoundException {
    Scanner scanner = new Scanner(fFile);
    try {
    //first use a Scanner to get each line
    while ( scanner.hasNextLine() ){
    processLine( scanner.nextLine() );
    }
    }
    finally {
    //ensure the underlying stream is always closed
    scanner.close();
    }
    }

    /**
    * Overridable method for processing lines in different ways.
    *
    * <P>This simple default implementation expects simple name-value pairs, separated by an
    * '=' sign. Examples of valid input :
    * <tt>height = 167cm</tt>
    * <tt>mass = 65kg</tt>
    * <tt>disposition = "grumpy"</tt>
    * <tt>this is the name = this is the value</tt>
    */
    protected void processLine(String aLine){
    //use a second Scanner to parse the content of each line
    Scanner scanner = new Scanner(aLine);
    scanner.useDelimiter("=");
    if ( scanner.hasNext() ){
    String name = scanner.next();
    String value = scanner.next();
    log("Name is : " + quote(name.trim()) + ", and Value is : " + quote(value.trim()) );
    }
    else {
    log("Empty or invalid line. Unable to process.");
    }
    //(no need for finally here, since String is source)
    scanner.close();
    }

    // PRIVATE //
    private final File fFile;

    private static void log(Object aObject){
    System.out.println(String.valueOf(aObject));
    }

    private String quote(String aText){
    String QUOTE = "'";
    return QUOTE + aText + QUOTE;
    }
    }


    For a file containing :
    height = 167cm
    mass = 65kg
    disposition = "grumpy"
    this is the name = this is the value
    the output of the above class is :
    Name is : 'height', and Value is : '167cm'
    Name is : 'mass', and Value is : '65kg'
    Name is : 'disposition', and Value is : '"grumpy"'
    Name is : 'this is the name', and Value is : 'this is the value'
    Done.

Similar Threads

  1. Replies: 10
    Last Post: 05-24-2010, 08:25 PM
  2. Replies: 3
    Last Post: 12-12-2008, 12:12 PM
  3. Write a List into a Text file
    By tech2000 in forum New To Java
    Replies: 1
    Last Post: 11-13-2008, 05:09 AM
  4. Searching a String from Text file using Swings .
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 4
    Last Post: 09-09-2008, 05:29 AM
  5. how do i write to a text file from an arraylist?
    By otoro_java in forum New To Java
    Replies: 3
    Last Post: 01-30-2008, 06:53 AM

Posting Permissions

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