Page 1 of 4 123 ... LastLast
Results 1 to 20 of 63
Like Tree3Likes

Thread: FileInputStream question.

  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default FileInputStream question.

    Well as you can see in the API, FileInputStream has 3 constructors:

    FileInputStream(File file)
    Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.
    FileInputStream(FileDescriptor fdObj)
    Creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.
    FileInputStream(String name)
    Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.

    and this class has a method:

    public int read(byte[] b)
    throws IOException
    Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.
    Overrides:
    read in class InputStream
    Parameters:
    b - the buffer into which the data is read.
    Returns:
    the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.

    My question is:

    I have a byte array and some byte values in it. And I create a FileInputStream to read the bytes in it.
    But I can not create a FileInputStream without pointing it to a file.
    But I really do not need a file to read an array.

    So why would that be ?
    Any logical answers?

    And why would I want to read a byte array with a FileInputStream, where I can easily see the values by byte[i] ?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: FileInputStream question.

    Can you explain what are you trying to do?

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: FileInputStream question.

    I am trying to learn JAVA.

  4. #4
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: FileInputStream question.

    It was an honest answer.

    I would never want to get on nerves of someone who is sparing some time from himself and trying to help.

    I was reading the API, and I wondered why they would have put a method like this..

    I am trying to ask more experienced people.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: FileInputStream question.

    why they would have put a method like this..
    What method?

  6. #6
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: FileInputStream question.

    I am trying to ask more experienced people.
    by this I mean:
    with posting questions here, I am trying to get help from experienced people..

    Sorry, I am not a native speaker.

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: FileInputStream question.

    Yes I have a specific question:

    What use can have a method :public int read(byte[] b) in FileInputStream class?

    How / Why would you ever use it ?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: FileInputStream question.

    To read data into an array of bytes.

    There are many many many different file contents and ways to read files. This is one of them.

  9. #9
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: FileInputStream question.

    public int read(byte[] b)

    You mean to read data ( and the data is how many bytes that array is holding ) FROM an array of bytes and return it as an integer ?

    but I can easily do it with b.length(), my question is:

    Why would I create a FileInputStream object ( that is pointing to a file somewhere, since it has only such constructors ), and use it to read how long an array is ?

  10. #10
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: FileInputStream question.

    Anyway thanks, it seems like I will just leave that method alone..
    Maybe someday I will need it and that will answer my question.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: FileInputStream question.

    Yes, that would be better. When the time comes that you need to read bytes into an array, remember this method.

  12. #12
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: FileInputStream question.

    Do you mean "When you need to get the number of bytes FROM an array of bytes."
    I doubt that I will remember this method since I am familiar with b.length().

    Thanks.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: FileInputStream question.

    The FileInputStream reads from a disk file. Read the API doc!

  14. #14
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: FileInputStream question.

    Yes I know, that is why I was confused at the first place. The method has nothing to do with a file. It takes an array as an argument, and returns how many bytes it has.

    Anyway, thanks for helping.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: FileInputStream question.

    Read the API doc again.

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

    Default Re: FileInputStream question.

    The Java InputStream uses a design pattern known as the decorator pattern where you wrap more primitive streams with higher level streams so that the primitive streams do the dirty work -- read the actual bits and bites, while the higher level streams allow for them to do it more efficiently (i.e., a BufferedStream) or with syntactic sugar to make it easier for us to code (i.e., PrintWriter). Some of the methods of the more primitive streams, such as a FileInputStream may not be often used directly by us the programmers, but may often used by the higher level streams that wrap it, and all of this occurs behind the scenes without our directly knowledge or intervention.

  17. #17
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: FileInputStream question.

    Norm, thanks for trying to help:

    See where I get confused is:

    I have this file try.txt where I have 5 characters in it. (money)

    What I am doing is:

    Java Code:
           byte[] readOnly = new byte[3];
    		FileInputStream myFileInputStream3 = new FileInputStream(readFile);
    		System.out.println(myFileInputStream3.read(readOnly));
    So I am expecting to read only 3 bytes from the file, because in the method it says in the API:
    Reads up to b.length bytes of data from this input stream into an array of bytes.
    And it is returning an integer.
    And that integer is :3. The size of my 'readOnly' byte..

    Please do not tell me to read the API, I have, and I tried creating an FileInputStream linking to a file, and reading up to b.length bytes from the file by passing my array as an argument to read method. But it doesn't help.

    I appreciate your help, but telling me to read the API does not change anything.

    I try a lot before posting here.

    @Fubarable, thanks for the explanation. I will try to understand what it means.

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

    Default Re: FileInputStream question.

    Quote Originally Posted by fatabass View Post
    @Fubarable, thanks for the explanation. I will try to understand what it means.
    Picture yourself, the programmer, as someone who wants to build a house. You will discuss your desires with a general contractor, a home builder, but you will usually discuss general things of how you want the house built, not the nitty gritty details of how to sand the floors and whatnot. Then the contractor will discuss these details with the carpenter who will spend all of his time working out those details. Think of the carpenter as the low level concept -- the one most concerned with the details (in our case, the FileInputStream), and the general contractor the higher level concept (the one more concerned with the overall building of the house. You, the programmer will deal mostly with the higher level concepts, the BufferedInputStreams and while the BufferedInputStream will be using a FileInputStream under the hood, you the programmer don't have to ask how it's doing this.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: FileInputStream question.

    it is returning an integer.
    And that integer is :3. The size of my 'readOnly' byte..
    That is because it could only read as many bytes as will fit into the array. It can't read any more without losing them.

    If the read() method read 2 bytes it would return 2 and if it read one byte it would return 1.
    If you read the API doc, it says:
    Returns:
    the total number of bytes read into the buffer,

  20. #20
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: FileInputStream question.

    Norm I know, this is not where I have a problem.

    My question is:

    Why would I create an array and put it into read method as an argument which belongs to a FileInputStreamer object that is referencing to a file ?

    What is the purpose ?
    When would it be useful ?

    I am not asking what the method is doing, I am trying to understand how it would be used..

Page 1 of 4 123 ... LastLast

Similar Threads

  1. FileInputStream
    By lakpathy in forum New To Java
    Replies: 2
    Last Post: 01-18-2011, 11:32 AM
  2. FileInputStream
    By lakpathy in forum New To Java
    Replies: 4
    Last Post: 01-17-2011, 12:00 PM
  3. FileInputStream Problems.
    By bayan in forum New To Java
    Replies: 18
    Last Post: 08-11-2010, 11:11 AM
  4. FileInputStream
    By pachufir in forum New To Java
    Replies: 3
    Last Post: 12-11-2009, 05:07 PM
  5. FileOutputStream > int > FileInputStream
    By dudejonne in forum New To Java
    Replies: 11
    Last Post: 11-11-2009, 04:03 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
  •