Results 1 to 18 of 18
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Question about RandomAccessFile and MappedByteBuffer

    About RandomAccessFile's pointer.
    Lets say I have 5 int values in a text file. I want to the 2nd int in my java program.
    Then I jump 4 bytes forward (x.seek (4))(the pointer start at 0), because a int is equal to 4 byte.
    If its a long value, I jump 8 byte forward, and so on for the rest of the primitive types.
    But what about String, how do I do here? I dont get it....

    MappedByteBuffer(mixed up with FileChannel and RandomAccessFile), this one is fast as hell when it comes to writing and reading to file. But I cant find any method that allow it to read/write strings. Am I out of options or is there an other way to do it?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    But what about String
    Strings could be stored as single byte/letter for some character sets. You will need to know the length of the String to be able to skip over it.
    If you are writing the file, you could write a field, say a short, with the length of the String followed by the String characters. Read the short to get the length to skip.

  3. #3
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    could you please reexplain that? didnt quite get it.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    Are all your Strings the same length? And you know what that length is?
    Then your code will be able to move the RandomAccessFile's pointer over them.

    If you don't know the length of a String. How will you know how far to jump to move past the String?

  5. #5
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    They do not have the same length. Even if they had, I have no clue how long to jump.
    Example, if I have
    Java Code:
    String s = "hello";
    My pointer is in front of s, and I want to move the pointer after/behind s, should I just move it 5 positions forward? Is it that simple?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Huh?

    The pointer won't be at s. It will at some position in your file. How do you know where the pointer is in the file? How do you know if the word(s) at the current pointer position are the words you want to skip?

  7. #7
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Lets start from start.
    I write a string and an int to a fileX with RandomAccessFile.
    I create a new program that read fileX with RandomAccessFile.
    The position start at 0, thats where the string is, I dont want to read it, I want to read the int. That means I must move the pointer behind the string, then I can read the int. The question is, how long should I move the pointer?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    Move the pointer the number of bytes in the String. If its an ASCII String then the number of bytes will be the same as the length of the String. What format is the int in? Is it a 4 byte binary number or is it character digits?

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    As the subtle replies so far are trying to point out, you have no idea unless you know what the string is that you want to skip. How about you drop the RandomAccessFile path and try using a normal reader such as FileReader or Scanner. Then read the entire line into your program where you can then manipulate it. Such as using a regular expression. Or maybe if the data in the original file was in a known organisational structure such as a CSV. This would make it easier to know where the actual data you want is located on each line.

  10. #10
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    Move the pointer the number of bytes in the String. If its an ASCII String then the number of bytes will be the same as the length of the String. What format is the int in? Is it a 4 byte binary number or is it character digits?
    int is in a 4 byte binary. RandomAccessFile always print in binary form.
    Also how do I know the number of bytes in a string? and how do I know if its an ASCII? btw ASCII is standard right? or is ANSI standard?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    As Junky pointed out, are the Strings always the same length. The first one is 5 bytes, then the next one is 8 etc
    Or can they be of unknown lengths. The contents of the file must be in fixed positions always or the program will not read the correct bytes.

    RandomAccessFile always print in binary form
    What do you mean by "print"? I don't see a print() method for the RandomAccessFile class.

  12. #12
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    nv, I figured it out. Just count the number of characters in a string and then add 2(if you wonder where I got 2 from, its the " characters at the end and at the beginning on a string).

    Java Code:
    import java.io.*;
    class Pojahns
    {
    	public static void main (String[] args) throws Exception
    	{
    		String f = "hello.txt";
    
    		RandomAccessFile fout = new RandomAccessFile (f, "rw");
    		fout.writeUTF ("din mamma");//There are [B]11[/B] characters in the brackets(that include the space character)
    		fout.writeByte (40);
    		fout.seek (0);
    		fout.seek ([B]11[/B]);//Move it 11 positions forward and read the byte
    		out.println (fout.readByte ());
    
    		fout.close ();
    	}
    }

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    What is the purpose of doing this? Are you just experimenting with the class to see what it does?

  14. #14
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Yes, I am experimenting with the class. If I use this class in future when I create programs, I will know how to use it properly.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    Not too many times when it is useful. Most files have variable length records and fields.

  16. #16
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    are you suggesting that RandomAccessFile should be avoided?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    I was suggesting that I didn't know of many applications for using it.

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    Agreed. When you have a file with fixed length records, you're usually better off using a database.

    db

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. read from RandomAccessFile
    By peliukasss in forum New To Java
    Replies: 4
    Last Post: 08-05-2010, 09:34 AM
  3. RandomAccessFile
    By swati.jyoti in forum New To Java
    Replies: 1
    Last Post: 04-22-2009, 04:05 PM
  4. Memory-Mapped File-(MappedByteBuffer )
    By lenah in forum Advanced Java
    Replies: 3
    Last Post: 12-22-2008, 07:00 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
  •