Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By eRaaaa

Thread: Need help using RandomAccessFile writeByte Method

  1. #1
    ii88xix88ii is offline Member
    Join Date
    Jan 2013
    Location
    Philippines
    Posts
    14
    Rep Power
    0

    Default Need help using RandomAccessFile writeByte Method

    ~hi everyone, i am a student taking Computer Science. I need help with my program that uses RandomAccessFile methods..
    ~i would like to know what code do i use for the program to write something as if you're pressing the "enter" key.



    RandomAccessFile raf = new RandomAccessFile(file, "rw");

    System.out.print("\n Name: ");
    sName = br.readLine();
    System.out.print("\n Address: ");
    sAddress = br.readLine();
    System.out.print("\n Contact #: ");
    sContact = br.readLine();

    raf.seek(raf.length());

    raf.writeBytes("\n Name: " + sName);
    raf.writeBytes("\n Address: " + sAddress);
    raf.writeBytes("\n Contact #: " + sContact);
    raf.close();

    ~using the code about will only write information like the student's name, address, and contact number..
    ~is there a code that we can use in java that works just like "\n" code?
    ~Thanks!

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Need help using RandomAccessFile writeByte Method

    Unix: Line Feed
    Windows: Carriage Return and Line Feed
    Macintosh: Carriage Return

    -> \n works fine on Unix.
    Try to use System.getProperty("line.separator")
    Java Code:
    		String newLine = System.getProperty("line.separator");
    
    		raf.writeBytes(newLine +" Name: " + sName);
    		raf.writeBytes(newLine +" Address: " + sAddress);
    		raf.writeBytes(newLine +" Contact #: " + sContact);
    ii88xix88ii likes this.

  3. #3
    ii88xix88ii is offline Member
    Join Date
    Jan 2013
    Location
    Philippines
    Posts
    14
    Rep Power
    0

    Default Re: Need help using RandomAccessFile writeByte Method

    ~thank you so much for your reply eRaaaa

    ~i just have a quick question, does this code "String newLine = System.getProperty("line.separator");" writes a next line on the file I'm accessing?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help using RandomAccessFile writeByte Method

    Quote Originally Posted by ii88xix88ii View Post
    ~thank you so much for your reply eRaaaa

    ~i just have a quick question, does this code "String newLine = System.getProperty("line.separator");" writes a next line on the file I'm accessing?
    No, as the method implies, it simply gets the String that you'd write into the file if you wanted a new line. There's nothing in that line that writes out anything to a file.

  5. #5
    ii88xix88ii is offline Member
    Join Date
    Jan 2013
    Location
    Philippines
    Posts
    14
    Rep Power
    0

    Default Re: Need help using RandomAccessFile writeByte Method

    ~oh! i see.. so there's no such thing that we can use RandomAccessFile to write a next line command to the document, is that what it is?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help using RandomAccessFile writeByte Method

    Quote Originally Posted by ii88xix88ii View Post
    ~oh! i see.. so there's no such thing that we can use RandomAccessFile to write a next line command to the document, is that what it is?
    Wait, you don't use RandomAccessFile to write any text document. These files are for data, not for text.

  7. #7
    ii88xix88ii is offline Member
    Join Date
    Jan 2013
    Location
    Philippines
    Posts
    14
    Rep Power
    0

    Default Re: Need help using RandomAccessFile writeByte Method

    RandomAccessFile raf = new RandomAccessFile(file, "rw");

    System.out.print("\n Name: ");
    sName = br.readLine();
    System.out.print("\n Address: ");
    sAddress = br.readLine();
    System.out.print("\n Contact #: ");
    sContact = br.readLine();

    raf.seek(raf.length());

    raf.writeBytes("\n Name: " + sName);
    raf.writeBytes("\n Address: " + sAddress);
    raf.writeBytes("\n Contact #: " + sContact);
    raf.close();

    ~this is my code..
    ~i saved a Test.txt document in my drive C:
    ~what this does, it writes whatever you input in the name, address, etc, to the file Test.txt.. but it writes as plain text.. right!?

    ~what I want to know is, is there a code that we can use to have an output of:

    Name: John Smith
    Address: <whatever address>
    Contact #: <whatever was inputted>

    ~then if I re-run the program, is it possible to have:

    Name: John Smith
    Address: <whatever address>
    Contact #: <whatever was inputted>

    Name: John Smith
    Address: <whatever address>
    Contact #: <whatever was inputted>

    ~with a space between them? or a next line between them?
    ~the output of what I have right now is:

    Name: John Smith
    Address: <whatever address>
    Contact #: <whatever was inputted>
    Name: John Smith
    Address: <whatever address>
    Contact #: <whatever was inputted>

  8. #8
    ii88xix88ii is offline Member
    Join Date
    Jan 2013
    Location
    Philippines
    Posts
    14
    Rep Power
    0

    Default Re: Need help using RandomAccessFile writeByte Method

    ~oh i think i get it now..
    ~is it possible to use BufferedWriter with RandomAccessFile?

  9. #9
    ii88xix88ii is offline Member
    Join Date
    Jan 2013
    Location
    Philippines
    Posts
    14
    Rep Power
    0

    Default Re: Need help using RandomAccessFile writeByte Method

    ~oh gosh.. BufferedWriter is not what I'm looking for..

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help using RandomAccessFile writeByte Method

    No and again, RandomAccessFile is not what you're looking for.

  11. #11
    ii88xix88ii is offline Member
    Join Date
    Jan 2013
    Location
    Philippines
    Posts
    14
    Rep Power
    0

    Default Re: Need help using RandomAccessFile writeByte Method

    ~okay.. by the way, is there a method associated with RAF that i can have an output of :

    Name: John Smith
    Address: <whatever address>
    Contact #: <whatever was inputted>

    Name: John Smith
    Address: <whatever address>
    Contact #: <whatever was inputted>

    ~with a space between them? or a next line between them?
    ~i understand that RAF may not be the best option but we are required to use RAF methods as of now..

Similar Threads

  1. Writing the data into RandomAccessFile
    By vasavi.singh in forum New To Java
    Replies: 5
    Last Post: 01-31-2013, 07:12 AM
  2. Question about RandomAccessFile and MappedByteBuffer
    By Pojahn_M in forum New To Java
    Replies: 17
    Last Post: 06-23-2011, 01:13 PM
  3. read from RandomAccessFile
    By peliukasss in forum New To Java
    Replies: 4
    Last Post: 08-05-2010, 09:34 AM
  4. RandomAccessFile
    By swati.jyoti in forum New To Java
    Replies: 1
    Last Post: 04-22-2009, 04:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •