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 6 of 6

Thread: JAVA = Replace a line in a text file

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question JAVA = Replace a line in a text file

    I want to create a program that will replace some of the text within a text file, however when I attempt to do so it deletes the rest of the file so I'm left with just the replaced line.

    For example if i want to change the fourth record from e to d it deletes items within the file.

    Text File before
    1 a
    2 b
    3 c
    4 e

    Text File after
    4 d

    Can anyone help??

    //package org.kodejava.example.io;
     
    import java.io.*;
    import java.util.*;
     
    public class LineNumberReaderExample {
        public static void main(String[] args) throws Exception {               
            File file = null;
            FileReader fr = null;
            LineNumberReader lnr = null;
     
            try {
                Scanner input=new Scanner(System.in);
                System.out.print("Enter record to edit ");
                String word=input.next();
                file = new File("file.txt");
                fr = new FileReader(file);            
                lnr = new LineNumberReader(fr);
                String line = "";
     
                while ((line = lnr.readLine()) != null) {
                    if(line.startsWith(word)){
     
                    System.out.println("Line Number " + lnr.getLineNumber() + ": " + line);
                    String[] st = line.split(" ");
                    String date = st[0];
                    String event = st[1];
     
                    System.out.print("Replace With: ");
                    String newtext=input.next();
                    FileWriter writer = new FileWriter("file.txt");
                    writer.write(line.replace(event, newtext));
                    writer.close();
                    }     
     
                }
            } 
           finally {
                if (fr != null) {
                    fr.close();
                }
                if (lnr != null) {
                    lnr.close();
                }
            }
        }
    }

    Once I get this code working I can then save dates and events to the text file.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JAVA = Replace a line in a text file

    To change the middle of a file, you need to copy everything before the changed area to an output file, then write the changed part and then copy everything after the changed area to the output file.
    There is a case when the new data EXACTLY byte for byte replaces the old data where you could use the RandomAccessFile class to write the new data over the top of the old data.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: JAVA = Replace a line in a text file

    Can you post the contents of the input file and the contents of the file that is written out?

    For testing you should use different files for input and output.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA = Replace a line in a text file

    Sorry I've changed my method of saving now. Ignore this thread

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA = Replace a line in a text file

    Could you please upload your final codes? Thanks

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JAVA = Replace a line in a text file

    Probably not, since this topic is several months old. Start a new topic if you need help to solve a similar problem.

Similar Threads

  1. Replies: 0
    Last Post: October 29th, 2012, 12:17 AM
  2. How to plot line graph using jfreechart reading from text file
    By priti in forum Java Theory & Questions
    Replies: 9
    Last Post: March 31st, 2012, 01:39 PM
  3. how to plot the line graph using jfreechart reading from text file
    By priti in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2012, 06:53 AM
  4. Writing to a specific line in a text file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 7th, 2011, 09:11 PM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM

Tags for this Thread