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: Write ascii in a specific position of the file

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Write ascii in a specific position of the file

    Hi,
    i would like to know how could I write a text in a specific position of a file.
    I could reed the specific position of a file using the BufferedReader() and the subString() .But Now I would like to write something using a function .
    Any suggestion?
    Thanks in advanced


  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: Write ascii in a specific position of the file

    The easiest method is to read the file into memory (probably inside a StringBuilder), then you can modify this text anyway you want (insert text, remove it, etc.). Once you're done, write the contents back to the file (basically replace the original file).

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Write ascii in a specific position of the file

    You could also use a RandomAccessFile, calling seek to move to a particular position to write.

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: Write ascii in a specific position of the file

    I would like to write something using a function .
    Java doesn't have functions. They are called methods.

    db

  5. #5
    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: Write ascii in a specific position of the file

    Quote Originally Posted by Darryl.Burke View Post
    Java doesn't have functions. They are called methods.

    db
    Not really much of a difference except the name

  6. #6
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Write ascii in a specific position of the file

    Thanks for your replays but I think I have not explain properly what I'm looking for.
    Usualy I have a plain text and I reed the file, line by line, and I put into, for example, Strings a specific position of each line.

    path = fileChooser.getSelectedFile();
    FileReader fr = new FileReader(path);
    BufferedReader br = new BufferedReader(fr);
    String line;
    while((line = br.readLine()) != null){
    String month = line.substring(56,58).trim();
    String year = line.substring(58,62).trim();
    // Do what ever I need with the Strings
    .
    .
    .
    }//close while
    fr.close();

    What I'm looking for is a method to write a String that I have in a specific position of a file's line. Imagine position 56 to 58.

    Something like that :
    String month = myMonth;
    1st line of the file;
    write month in position 56,58 of 1st line;
    month = myOtherMonth;
    2nd line;
    write month in position 56,58 of 2nd line;

    Hope somebody can help me.
    Thanks!

Similar Threads

  1. Writing in a file using Java
    By JavaPF in forum File Input/Output Tutorials
    Replies: 4
    Last Post: December 17th, 2011, 03:33 PM
  2. [SOLVED] Why can't I write to file inside a doGet() method?
    By FailMouse in forum Java Servlet
    Replies: 1
    Last Post: July 7th, 2010, 01:15 AM
  3. How to write 2 dimensional array of float numbers to binary file?
    By Ghuynh in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 17th, 2010, 04:26 PM
  4. How to write to specific part of an html file using java
    By nasi in forum File I/O & Other I/O Streams
    Replies: 12
    Last Post: May 27th, 2010, 11:22 PM
  5. write text to a file help
    By wolfgar in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 24th, 2009, 07:36 AM