473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

buffered i/o

i tried this program (attached at end) but it puzzles me a little bit. The
program waits for a user to type something and immediately writes what user
writes to a file. But when i interrupt this program after having typed
something, the file is empty. What happens here: does java do buffering
automatically or is it because java interpreter didn't have chance to close
the file normally? i'd expect everything i typed before interrupting the
program, to be in the file.

import java.io.*;

public class TextFileOutput_App1 {

public static void main(String args[]) {

File file = null;
if( args.length > 0) file = new File(args[0]);
if( file == null ) {
System.out.println("Default: textOutput.txt");
file = new File("textOutput.txt");
}

// Read the bytes directly and count them.

try {
int tmp = 0;
FileWriter fileWriter = new FileWriter( file );
// Read characters from the console and write
// to the file.
while ( (tmp=System.in.read()) != -1 ) {
// Write one character at a time to the file.
fileWriter.write( (char) tmp );
}

fileWriter.close();
}
catch (IOException e) {
System.out.println( "IO error:" + e );
}
// Compare to the File class value.
System.out.println("File.length() = " + file.length() );
}
}

Jul 17 '05 #1
1 3566
You probably need to do a fileWriter.flush() before you call fileWriter.close().

I would hope Java would do some buffering, performance would be horrible otherwise.

//Nathan

slurper <sl*********@skynet.be> wrote in message news:<41**********************@news.skynet.be>...
i tried this program (attached at end) but it puzzles me a little bit. The
program waits for a user to type something and immediately writes what user
writes to a file. But when i interrupt this program after having typed
something, the file is empty. What happens here: does java do buffering
automatically or is it because java interpreter didn't have chance to close
the file normally? i'd expect everything i typed before interrupting the
program, to be in the file.

import java.io.*;

public class TextFileOutput_App1 {

public static void main(String args[]) {

File file = null;
if( args.length > 0) file = new File(args[0]);
if( file == null ) {
System.out.println("Default: textOutput.txt");
file = new File("textOutput.txt");
}

// Read the bytes directly and count them.

try {
int tmp = 0;
FileWriter fileWriter = new FileWriter( file );
// Read characters from the console and write
// to the file.
while ( (tmp=System.in.read()) != -1 ) {
// Write one character at a time to the file.
fileWriter.write( (char) tmp );
}

fileWriter.close();
}
catch (IOException e) {
System.out.println( "IO error:" + e );
}
// Compare to the File class value.
System.out.println("File.length() = " + file.length() );
}
}

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Amit | last post by:
I have created formatted-PDF in Buffer. This PDF document have many images, lines, texts with underline-bold-italic. I put this buffer PDF contents to a variable. When I try to send the variable...
0
by: Dan Stromberg | last post by:
I finally took a moment to put my buffered socket class, bufsock.py, on a web page. It's a wrapper class for python sockets. It should reduce tinygrams, as well as provide a more convenient...
7
by: Steven T. Hatton | last post by:
I haven't looked very closely, but from what I'm seeing, it looks like there are no buffered I/O streams in the Standard Library. There are stream buffers, but not buffered streams. I don't have...
9
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
4
by: pank7 | last post by:
hi everyone, I have a program here to test the file IO(actually output) with buffer turned on and off. What I want to see is that there will be obvious differece in time. Here I have an input...
1
by: Tom Bianchi | last post by:
Hi, is it possible to make a SplitContainer double buffered ? Deriving and setting the style in the constructor doesn't seem to work, maybe because it is composed by two normal Panels... ...
2
by: Karl | last post by:
Hey everyone! I've got a quick question on whether std::ifstream is buffered or not. The reason is that I have a homework assignment that requires me to benchmark copying files using different...
1
by: LiorO | last post by:
Hi, I need to implement a high performance file copying. The File.Copy method in .NET is fast enough however I need to control the speed since it should be performed in the background without...
1
by: Clay Hobbs | last post by:
On Sun, 2008-10-05 at 14:16 -0400, Mike C. Fletcher wrote: I'm using wxPython. My real problem is that everything flashes when it moves. I thought the way to fix this was to make a...
8
by: zeeshan708 | last post by:
what is the difference between the buffered and unbuffered stream ??(e.g we say that cout is buffered and cerr is unbuffered stream) does that mean that the output sent to buffered stream have to go...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.