Results 1 to 5 of 5
  1. #1
    rogi85 is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default Clarification of FileOutputStream

    Hello everyone, this is my first posting on this forums. I'm relatively new to Java, and wish to learn it more. I managed to make my own program with some tutorials and example programs found all over the Internet, however I'd like to understand better certain part of the code.

    It's about file transfer, specifically writing into file from stream (server client communication over socket).

    Java Code:
    ServerSocket server= new ServerSocket(4444);
    Socket connection = server.accept();
    This part is clear, I know what does it do, but not sure how to explain it. But following part is more problematic.

    Java Code:
    File dataFile = new File("C:\test.txt");
    FileOutputStream inputFile = new FileOutputStream(dataFile);
    InputStream inps = connection.getInputStream();
    BufferedInputStream input2 = new BufferedInputStream(inps, 1024);
    
    byte[] buff = new byte[1024];
    int length = 0;
    int count = 0;
    
    while ((length = input2.read(buff, 0, 1024)) != -1) 
                                     {
                                        count = count + 1024;
                                        inputFile.write(buff, 0, length);
                                     }
                  input2.close();
               inputFile.close()
    If anyone have patience to explain it, line by line. I compiled program, and it does what it should do: from input stream he reads data until there is nothing to read, in steps of 1024 bytes, and write it test.txt at C drive. After that, it close the stream and file for writing. However, I'm lacking technical terms that would help me understand it.

    Is there any Java book I could look into, that someone could recommend for a beginner? It doesn't necessarily have to be online book, but it would make it easier to find in online library.

    Thank you in advance.

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

    Default

    Your explanation shows that you understand it. What specific questions do you have?

  3. #3
    rogi85 is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Java Code:
    File dataFile = new File("C:\test.txt");
    FileOutputStream inputFile = new FileOutputStream(dataFile);
    InputStream inps = connection.getInputStream();
    BufferedInputStream input2 = new BufferedInputStream(inps, 1024);
    Specifically this.

    File dataFile = new File("C:\test.txt");
    - we set the file location and name, that's fine.

    FileOutputStream inputFile = new FileOutputStream(dataFile);
    - we create a new object of class FileOutputStream called inputFile with constructor inside class FileOutputStream where we're passing the object dataFile? (or we're passing the location of file?)

    InputStream inps = connection.getInputStream();
    - another object named inps where we use method getInputStream from class InputStream on previously declared object connection?

    BufferedInputStream input2 = new BufferedInputStream(inps, 1024);
    - and lastly, we're opening BufferedStream, which is allowing us to transfer data in blocks of 1024 bytes?


    And I was curious what that "count = count + 1024;" does, but since last night I've checked code - it doesn't do anything connected with file transfer, it only counts how many bytes is sent. So that's fine.

    But I'm not quite sure how this while loop works.

    while ((length = input2.read(buff, 0, 1024)) != -1)

    So, it compares length, and until it's different that -1 it continues to work?
    This (length = input2.read(buff, 0, 1024)), have no slightest clue what does is set as values for variable length.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by rogi85 View Post
    Java Code:
    File dataFile = new File("C:\test.txt");
    FileOutputStream inputFile = new FileOutputStream(dataFile);
    InputStream inps = connection.getInputStream();
    BufferedInputStream input2 = new BufferedInputStream(inps, 1024);
    Specifically this.

    File dataFile = new File("C:\test.txt");
    - we set the file location and name, that's fine.

    FileOutputStream inputFile = new FileOutputStream(dataFile);
    - we create a new object of class FileOutputStream called inputFile with constructor inside class FileOutputStream where we're passing the object dataFile? (or we're passing the location of file?)
    Pretty much. That is saying "give me a stream to write to this file". It'll throw an exception if that's not possible (ie the directory chosen doesn't exist or you don't have write privileges).

    Quote Originally Posted by rogi85 View Post
    InputStream inps = connection.getInputStream();
    - another object named inps where we use method getInputStream from class InputStream on previously declared object connection?
    You are getting the input stream that already exists in that connection. That method is part of the Socket class.
    That line says "give me the input stream from this socket and stick the reference here so I can use it".

    Quote Originally Posted by rogi85 View Post
    BufferedInputStream input2 = new BufferedInputStream(inps, 1024);
    - and lastly, we're opening BufferedStream, which is allowing us to transfer data in blocks of 1024 bytes?
    This is just wrapping the retrieved input stream from the socket in a BufferedInputStream because it's easier to read stuff with a buffered stream. It gives us access to some handy methods so we don't have to write the same logic ourselves.

    Quote Originally Posted by rogi85 View Post
    And I was curious what that "count = count + 1024;" does, but since last night I've checked code - it doesn't do anything connected with file transfer, it only counts how many bytes is sent. So that's fine.
    Actually that line is wrong. It assumes that each read() call is returning 1024 bytes, which is not necessarily the case. That's why there's a length returned from the read, to say exactly how much has been read. So it should be count = count + length.

    Quote Originally Posted by rogi85 View Post
    But I'm not quite sure how this while loop works.

    while ((length = input2.read(buff, 0, 1024)) != -1)

    So, it compares length, and until it's different that -1 it continues to work?
    This (length = input2.read(buff, 0, 1024)), have no slightest clue what does is set as values for variable length.
    Read the API. It explains how the read works.

  5. #5
    rogi85 is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    "stick the reference here so I can use it" and "wrapping the ..." - these were the things (words) I was looking for, thank you!

    Also, thank you for link to .read, I was there, but somehow missed it. Yes, it's clear now.

Similar Threads

  1. FileOutputStream to GIF
    By dewitrydan in forum New To Java
    Replies: 12
    Last Post: 10-11-2010, 07:12 AM
  2. closing FileOutputStream?
    By mgrootsch in forum New To Java
    Replies: 1
    Last Post: 05-17-2010, 05:36 PM
  3. FileOutputStream > int > FileInputStream
    By dudejonne in forum New To Java
    Replies: 11
    Last Post: 11-11-2009, 04:03 PM
  4. FileOutputStream gets NotSerializableException
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 09:38 PM
  5. FileOutputStream question...
    By SCS17 in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 05:30 AM

Posting Permissions

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