aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about DataInput streams (chains) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about DataInput streams (chains)" Watch "about DataInput streams (chains)" New topic
Author

about DataInput streams (chains)

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
basically the same code that i used to copy contents
of one file into another using FileInput and Output
Streams only. That worked fine.
But here i have just expanded to include another
layer ,the DataInput and DataOutput Streams for chaining
as they say.
But only the first 4 integers are getting copied into the
destination file, repeatedly. is this the behaviour of
DataInputStream methods ? The api says so....
Returns: the next four bytes of this input stream,
interpreted as an int.
if this is ok then does this mean that they cannot be
used to read files. Or if my code is not ok then let me
know. (try and catch have not been included in the code
here, but the code works fine)


FileInputStream fi = new FileInputStream
("c:\\test\\1.txt");
DataInputStream di = new DataInputStream(fi);
int ii = di.readInt();
FileOutputStream fo = new FileOutputStream
("c:\\test\\copy1.txt",true);
DataOutputStream ddo = new DataOutputStream(fo);
while(ii != -1){ddo.writeInt(ii);di.readInt();}
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
while(ii != -1){ddo.writeInt(ii);di.readInt();}

You keep writing the int ii to the output stream, then you read an int from the input stream.....my question to you is, where are you storing the new value you read from the input stream?
Rob


Rob
SCJP 1.4
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
the int ii is reading the new value.
and this value is written. next again value is read and is written.
i used the same code ok with FileInputStream only. (no chains)
here is the code
FileOutputStream ff = new FileOutputStream
("c:\\test\\2copy.txt",true);
FileInputStream fi = new FileInputStream
("c:\\test\\2.txt");
int ii=0;ii=fi.read();
while(ii != -1)
{ff.write(ii);ii=fi.read();
this one is just fine and works great.
if i am missing something please let me know

Originally posted by Rob Ross:

You keep writing the int ii to the output stream, then you read an int from the input stream.....my question to you is, where are you storing the new value you read from the input stream?
Rob
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by mark stone:
the int ii is reading the new value.
and this value is written. next again value is read and is written.

Not in the sample code you provided in your first post. Btw, to increase clarity I would suggest you make better use of white space. Try placing each statement on its own line.
Rob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about DataInput streams (chains)
 
Similar Threads
file transfer roblem
I/O BufferedInputStream
about stream
Some questions about writeBytes(String) vs writeChars(String)
Files I/O