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

Thread: how to replace a line in a text file

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

    Default how to replace a line in a text file

    hi gud day everyone;

    i want to replace a line in a text file ...and here is my code


    import java.io.*;

    public class exampletextfile
    {
    public static void main(String args[])throws IOException
    {
    BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
    File file = new File("StudGrades.itp" );
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String line="";

    System.out.print("Find: " );
    String del=input.readLine();

    while((line = reader.readLine()) != null)
    if(line.contains(del))
    {
    System.out.print(line );
    }



    System.out.print("\nReplace with:" );
    String replacement=input.readLine();


    if(line.contains(del))
    {
    line= replacement.replace(del,line);
    }

    FileWriter writer = new FileWriter("StudGrades.itp" );
    writer.write(line);
    reader.close();
    writer.close();

    }
    }

    im done with the displaying of the line my only problem is the replacement part
    here's the info i have in the text file

    1 090-2012-0032,98.0,90.0,95.0,96.0,95.0
    2 090-2012-0033,98.0,90.0,95.0,96.0,95.0

    i just want to replace a single line then i need to input the new grades from prelim to finals and compute for the subject grade.

    here's my code for that:
    (in what part of the program can i insert these codes.?)
    System.out.println("Enter Student number:" );
    String sn=input.readLine();
    System.out.println("Enter prelim grade:" );
    p=Integer.parseInt(input.readLine());
    System.out.println("Enter midterm grade:" );
    m=Integer.parseInt(input.readLine());
    System.out.println("Enter prefinal grade:" );
    pf=Integer.parseInt(input.readLine());
    System.out.println("Enter final grade:" );
    f=Integer.parseInt(input.readLine());


    sg=(p*.2)+(m*.2)+(pf*.2)+(f*.4);

    in my case all lines are being deleted whenever i input the infos.


    i hope someone can help me with my program....thanks


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: how to replace a line in a text file

    Try using the RandomAccessFile Class or BufferedWriter Class.

    RandomAccessFile Class
    BufferedWriter Class

    To preserve formatting and for readability use code tags.

    [C0DE=java]
    <code>
    [/CODE]

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,918
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: how to replace a line in a text file

    i want to replace a line in a text file .
    To replace a line in a file by writing over the file, the new line must be exactly the same size as the line being replaced.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: how to replace a line in a text file

    Have you thought about reading in the contents of the file, adding the missing line, and then writing a new file?

Similar Threads

  1. JAVA = Replace a line in a text file
    By JGW in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 16th, 2013, 05:46 AM
  2. Replies: 0
    Last Post: October 29th, 2012, 01:17 AM
  3. 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, 02:39 PM
  4. 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, 07:53 AM
  5. 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