Results 1 to 5 of 5
- 08-03-2011, 01:14 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
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).
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:ServerSocket server= new ServerSocket(4444); Socket connection = server.accept();
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.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()
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.
- 08-03-2011, 02:36 AM #2
Your explanation shows that you understand it. What specific questions do you have?
- 08-03-2011, 09:30 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
Specifically this.Java Code:File dataFile = new File("C:\test.txt"); FileOutputStream inputFile = new FileOutputStream(dataFile); InputStream inps = connection.getInputStream(); BufferedInputStream input2 = new BufferedInputStream(inps, 1024);
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.
- 08-03-2011, 09:53 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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).
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".
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.
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.
Read the API. It explains how the read works.
- 08-03-2011, 12:02 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
FileOutputStream to GIF
By dewitrydan in forum New To JavaReplies: 12Last Post: 10-11-2010, 07:12 AM -
closing FileOutputStream?
By mgrootsch in forum New To JavaReplies: 1Last Post: 05-17-2010, 05:36 PM -
FileOutputStream > int > FileInputStream
By dudejonne in forum New To JavaReplies: 11Last Post: 11-11-2009, 04:03 PM -
FileOutputStream gets NotSerializableException
By xcallmejudasx in forum New To JavaReplies: 0Last Post: 12-02-2008, 09:38 PM -
FileOutputStream question...
By SCS17 in forum New To JavaReplies: 2Last Post: 07-07-2008, 05:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks