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: RandomAccessFile

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default RandomAccessFile

    Hi,

    Is it possible using this class to find away to the method "seek" to somehow start a newline of a code? I understand how to get it to find the end of the file with the length method, but it still writes it on the same line. Can I somehow tell it to find the eof and then start a newline. I tried all the simple ways such as "\n" and nothing. Any suggestions.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: RandomAccessFile

    I would think the api of the class would answer this question. What did it say?

    If all you want to do is append to a file why not just append a new line? No need to seek the end of a file just to append to it. I would think there is a way to append to a file. Search engines are your friend.

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: RandomAccessFile

    here is the test file i been working with before i use it in my main program
      String line1 = "First  line \n";
           String line2 = "Second  line2 \n";
           File f = new File("C:\\Test\\Hello3");
           RandomAccessFile raf = new RandomAccessFile(f,"rw");
           raf.writeBytes(line1);
           raf.writeBytes(line2);
           raf.seek(line1.length());
           byte[] buffer = new byte[line2.length()];
           Scanner sr = null;
           sr = new Scanner(f);
           while(sr.hasNext()){
               String a = sr.next();
               System.out.print(a);
           }

    It writes it on the same line. I don't know why it just blatantly ignores this"\n".

  4. #4
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: RandomAccessFile

    Can anyone help me on this?

Similar Threads

  1. RandomAccessFile.readByte() find EOF
    By chopficaro in forum Java Theory & Questions
    Replies: 3
    Last Post: May 6th, 2010, 03:14 AM