Results 1 to 18 of 18
  1. #1
    jessJ2EE is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default mark in InputStream

    Hi everyone! I am accesing a file, using InputStream, wrapping it in DataInputStream and BufferedReader. I need to make a mark in the file using "mark(int readAhead)" method, and I know I only can call "reset()" before reaching "readAhead" bytes.

    But if I do not know the bytes I am going to read before calling "reset()".... What can I do?

    Thank you very much!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default Re: mark in InputStream

    Then all you can do is use a huge value for the readAhead parameter; but even better is to use a RandomAccessFile (read its API documentation).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jessJ2EE is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default Re: mark in InputStream

    thanks Josah! RandomAccessFile or just File is what I need. Now my problem is that I am on Android platform and I do not know how to get the path of the file. Suppose the name of my file is “textfile.txt”

    Java Code:
    File file=File(getFilesDir(),”textfile.txt”); //this throws a FileNotFoundException
    I put the file in /res/raw/textfile.txt folder.

    Where am I supposed to place the file??

    Thank you very much!
    Last edited by jessJ2EE; 06-02-2012 at 06:12 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default Re: mark in InputStream

    Quote Originally Posted by jessJ2EE View Post
    thanks Josah! RandomAccessFile or just File is what I need. Now my problem is that I am on Android platform and I do not know how to get the path of the file. Suppose the name of my file is “textfile.txt”

    Java Code:
    File file=File(getFiles(),”textfile.txt”); //this throws a FileNotFoundException
    I put the file in /res/raw/textfile.txt folder.

    Where am I supposed to place the file??

    Thank you very much!
    Supply the 'absolute' path name:

    Java Code:
    File file=File(”/res/raw/textfile.txt”);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jessJ2EE is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default Re: mark in InputStream

    Thank you very much! but it is still not working. It throws again a FileNotFound exception.

    "getFilesDir()" returns /data/data/mypackage/files, but I do not know where to place the file.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default Re: mark in InputStream

    Quote Originally Posted by jessJ2EE View Post
    Thank you very much! but it is still not working. It throws again a FileNotFound exception.

    "getFilesDir()" returns /data/data/mypackage/files, but I do not know where to place the file.
    I don't know what getFilesDir() is but if you store your file in directory /x/y/z/textfile.txt, an absolute path always references it correctly:

    Java Code:
    File f= new File("/x/y/z/textfile.txt");
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jessJ2EE is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default Re: mark in InputStream

    Hi thank you very much again!

    I can not find the path getFilesDir() returns (“/data/data...”). Probably this path is inside the apk file.....

    So maybe solution would be to insert the txt file inside the apk......

    Is this the solution??

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default Re: mark in InputStream

    I don't know what getFilesDir() is nor do I know what an apk file is; if it is some sort of .jar file you should be able to get an InputStream from it; read the API documentation for the Class.getResourceAsStream( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    jessJ2EE is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default Re: mark in InputStream

    Hi thanks again! I am able to get an InputStream using the getAsset(filename), but I do need a File or RandomAccesFile object.

    Is there a way to convert an InputStream to a File object?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default Re: mark in InputStream

    Quote Originally Posted by jessJ2EE View Post
    Hi thanks again! I am able to get an InputStream using the getAsset(filename), but I do need a File or RandomAccesFile object.

    Is there a way to convert an InputStream to a File object?
    Not really, except you could read from that InputStream and write it to a FileOutputStream; close everything when done and open a RandomAccessFile on the file that was created before; but it's my feeling that you're on the wrong track then; please elaborate on what it is exacltly that you want to accomplish.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    jessJ2EE is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default Re: mark in InputStream

    Hi again! I am very very sorry for expressing myself so bad, I am not English (you'd probably notice).

    I need to mark the file, but do not know how many bytes will be read. A solution would be to call
    mark() with the file length as a parameter “mark(file.length())”.

    But the InputStream does not have any method to know the length of a file, so I need a File object, with the length() method.

    But how to know the path of a file on android is getting me crazy.

    Is there anyway to get a File object from an InputStream??

    Thank you very very much!!

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

    Default Re: mark in InputStream

    You're telling us how you want to solve your problem in code, not what behavior you're trying to achieve with this code. Please tell us the behavior that you're trying to achieve as there may well be a better way to solve this.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: mark in InputStream

    Quote Originally Posted by jessJ2EE View Post
    ... to know the path of a file on android is getting me crazy.
    Moved from Advanced Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    jessJ2EE is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default Re: mark in InputStream

    I have to retrieve a random word from a dictionary with a specific size. The words in this dictionary are ordered by size.

    For example, if I need to retrieve a word of size 6, I have to know the number of words of this size, so I can return a random word.

    This is the algorithm:
    1.Read words until I get a word with size 6.
    2.I mark the first word with size=6
    3.Count the number of words of this size.
    4.Call “reset” to position the file to the mark set in step one.
    5.Generate a random number between 0 and the number of words and get the random word.

    My idea is to call “mark” with the size of the file as a parameter, so I am sure I will never go further of the mark.

    So I need a object of type File (which has a method length()), instead of InputStream object.

    Now my problem is that I do not know how to get the path of the file on android.

    Thank you very very much!

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default Re: mark in InputStream

    Do you have room enough to store the entire file contents in memory?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    jessJ2EE is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default Re: mark in InputStream

    yes, the file only takes 1.4M. But right now I am using Eclipse and the emulator in my computer, so there is no problem of space

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default Re: mark in InputStream

    Quote Originally Posted by jessJ2EE View Post
    yes, the file only takes 1.4M. But right now I am using Eclipse and the emulator in my computer, so there is no problem of space
    Ok, assuming that you can't read the entire thing in memory, would it be feasable to read the entire thing once and record the start of the first word of length n? All you need to store then is an array of length, say, 30 with int elements representing the first word of length i, where a[ i ] is the particular offset.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    jessJ2EE is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default Re: mark in InputStream

    assuming that you can't read the entire thing in memory
    this is the only thing I do not understand. But I do understand the rest and I agree with you. This is a good choice. Read words until I get the first word with size n and record the offset. Then count all the words with size n and close the file.
    Then reopen the file, but jumping offset to get the first word and get the random word.

    thank you very very much!!!

Similar Threads

  1. Removing question mark
    By Optimus in forum Eclipse
    Replies: 4
    Last Post: 08-06-2010, 09:55 AM
  2. Replies: 1
    Last Post: 02-12-2010, 01:50 PM
  3. help to understand the ? mark in this code
    By carolain79@hotmail.com in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 06:57 AM
  4. BufferedReader .mark(int readAheadLimit)
    By ShoeNinja in forum New To Java
    Replies: 1
    Last Post: 11-16-2007, 10:58 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
  •