Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Writing to a specific line in a text file

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Writing to a specific line in a text file

    Hi, I have a problem where I only want to replace one line in a text file, while keeping all the others the same. For example, my text file looks something like this:

    0
    3
    10
    18
    7
    4
    2

    And, in the actual program, I do something like this:
    -Read line 3, set it to string x
    -Change the value of string x

    And now I want to replace line 3 with x, but I have no idea how to.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Writing to a specific line in a text file

    There are two ways:

    1. Write to a temporary file sequentially, only writing out new data when you need to.
    2. Use the RandomAccessFile class. See: Java Tips - How to use Random Access file and RandomAccessFile (Java 2 Platform SE v1.4.2)

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    The_Mexican (January 7th, 2011)

  4. #3
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: Writing to a specific line in a text file

    Quote Originally Posted by The_Mexican View Post
    Hi, I have a problem where I only want to replace one line in a text file, while keeping all the others the same. For example, my text file looks something like this:

    0
    3
    10
    18
    7
    4
    2

    And, in the actual program, I do something like this:
    -Read line 3, set it to string x
    -Change the value of string x

    And now I want to replace line 3 with x, but I have no idea how to.
    here's an example
    public class ReadWriteFile {
        public static void main(String[] args) throws FileNotFoundException, IOException{
            Formatter output = new Formatter(args[1]);
            Scanner sc = new Scanner ( new File(args[0]) );
            ....
            String replace = "x";
            while  (sc.hasNext() ){
                String line = sc.nextLine();
                .....
                if ( couter==3 ){
                    line = replace;
                }
                output.format("%s\n", line );
            }
            output.close();
        }
    }

Similar Threads

  1. Help writing to a text file on a website!
    By straw in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: September 11th, 2011, 12:02 PM
  2. [SOLVED] Specific Text Extraction from PDF Research paper documents
    By user101 in forum Threads
    Replies: 1
    Last Post: September 8th, 2010, 12:30 PM
  3. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 04:10 PM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 08:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 07:32 AM