Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within I/O and Streams
Search Coderanch
Advance search
Google search
Register / Login
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
Tim Cooke
paul wheaton
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
I/O and Streams
Unexpected Behaviour with Writers
jean-gobert de coster
Ranch Hand
Posts: 49
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi, I'm running he following code:
public class PlayWithFiles { public static void main(String[] args) { playWriter(); playReader(); } public static void playWriter() { FileWriter w = null; BufferedWriter bw = null; try { w = new FileWriter("test/test2.txt"); bw = new BufferedWriter(w); w.write("Hello!"); bw.newLine(); bw.write("I'm a BufferedWriter"); w.write("I'm a FileWriter"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { bw.flush(); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void playReader() { FileReader r = null; BufferedReader br = null; try { r = new FileReader("test/test2.txt"); char[] buf = new char[50]; r.read(buf); System.out.println(buf); } catch (IOException e) { e.printStackTrace(); } } }
And when I run it, the output is the following:
Hello!I'm a FileWriter
I'm a BufferedWriter
Ulf Dittmer
Rancher
Posts: 43081
77
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Why is that unexpected behavior? The buffered writer buffers its output, while the straight writer writes immediately. Seems like it should be.
As an aside, I wouldn't use several writers that access the same underlying resource.
jean-gobert de coster
Ranch Hand
Posts: 49
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Ah ok... Buffered... figures :-) By Unexpected, I meant "that I didn't expect" of course. Thanks for the answer
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Search and Replace tag (Not its Value) in an XML file.
File IO - BufferedWriter/FileWriter
how to see how control is moving in a java program during execution
Stringbuffer and carriage return
Printing from threads in a particular sequence
More...