• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

help with code (inputstream)..... ?

 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
InputStream inp = url.openStream();
int ii;
while( (ii = inp.read()) != -1 )
{System.out.print( (char)ii );} }
the above code reads and prints fine from an InputStream. but instead of System.out.print(....) i want it to be written into a text file.

i tried by following but only > gets written into the file.
boolean tt=true;
FileWriter fw = new FileWriter("c:\\one.txt",tt);
PrintWriter pw = new PrintWriter(fw,tt);
while( (ii = inp.read()) != -1 )
{pw.write((char)ii);pw.close();}
again the inputstream is fine as when i use the above earlier code snippet it prints out fine via the System.out.print
now instead of getting the output written onto the standard output i want it to be written to a text file.
can someone help me on this.
thanks
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you closing your printwriter in the loop? You write one characer, then you close it. Try moving your close() to outside the loop
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops...
it works now.
also i added a BufferedWriter. i can't see any improvement in performance though. anyway buffering is for improving performance. right ?
so that data can be buffered into temporary buffers and then operations can be done on them.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can be if you use it correctly. How are you using it? How much memory did you allocate for the buffer?
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
boolean tt = true;
FileWriter fw = new FileWriter("c:\\one.txt",tt);
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw,tt);
while( (ii = inp.read()) != -1 )
{pw.write((char)ii);}
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it seems ok!
i just did a test on my machine to copy 90mb file using the same code as u did.
without buffer: 12 minutes.
with buffer: 32 seconds.
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(i was using a small text file to notice any real difference in speed)......
question about what you said
"How much memory did you allocate for the buffer?"
what do you mean by allocate memory ? is there a particular statement that we need to provide ? I am just using the constructor for BufferedWriter. where and how is memory specifically alloted ?
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can use the constructor to allocated memory size: thats from the API
BufferedWriter
public BufferedWriter(Writer out,
int sz)Create a new buffered character-output stream that uses an output buffer of the given size.
Parameters:
out - A Writer
sz - Output-buffer size, a positive integer
the second parameter you pass is the memory size.
the default is 512 if im not mistaken so it is normally ok to use the default.
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
below is from the latest api (1.4). what is the unit for "sz" ? this buffer size is in bytes or kilobytes ?
i know you did mention it is bytes and default is 512 bytes, but this information got to be in the api ? where else is this information ? wanted to know for other details etc for other classes as well as well as for future reference.

BufferedReader
public BufferedReader(Reader in,
int sz)
Create a buffering character-input stream that uses an input buffer of the specified size.
Parameters:
in - A Reader
sz - Input-buffer size
Throws:
IllegalArgumentException - If sz is <= 0
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is from the API of BufferedInputStream:
When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time.
that tells us that the buffer size is in bytes.
and this is from the BufferedReader API:
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
it doesnt tell the default size, but i remember reading somewehre its 512. (i may be mistaken)
 
You get good luck from rubbing the belly of a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic