Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to correctly use this read() write()  RSS feed

 
Maki Jav
Ranch Hand
Posts: 449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a method. The problem is that it copies last few ending lines to the end of outputfile. Any solution?

Method is:


if I use byte array of size 10, I get it correctly ie file is written correctly, but I don't know whether it will behave appropriately in all situations.
Is there any benefit over using bis.read() and bos.write() ie the methods that read/ write a single int.
Thanks in Advance,

Maki Jav
[ November 07, 2005: Message edited by: Maki Jav ]
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, bis.available() doesn't do what you think it does:

public int available() throws IOException

Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.

java.io.InputStream
It does not return the number of bytes left to read in a stream.
Have a look at the IO Chapter in Java Platform Performance to get an idea as to how to best use the various IO classes.
 
JuanP barbancho
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I attach some code that I use in a lot of program:


[ November 07, 2005: Message edited by: Joe Ess ]
 
Maki Jav
Ranch Hand
Posts: 449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually was misled by this:

if I set the size of myfile to 100, fis.avaialble() tells me that 26 bytes
are still there...

Maybe the fis.read() call was reading the whole file in one go!

I have now used this piece of code:



The code is about as fast as using BufferedStreams for reading and writing
with buffer size of 100.

Any comments?

Thanx!

Maki Jav
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16007
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This will not always work correctly! Read the API documentation of the methods carefully.

fis.read(readBytes) is not required to read exactly as many bytes as fit in the buffer. It may also read less than that. You need to take the return value into account, which is the number of bytes that it really read.

If you want a fast way to copy a file, look at this: Copying One File to Another (using nio)
 
Maki Jav
Ranch Hand
Posts: 449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for helping me out...

I have used code on the page refered to by joe as it will work with jdk1.3 and above...


Thanks once again...


Maki Jav
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!