• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about DataInput streams (chains)

 
mark stone
Ranch Hand
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
mark stone
Ranch Hand
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic