BufferedWriter does not write ( Beginners question)

J

Jean-Benoit MORLA

Hi,

I have a program from a textbook that reads an ASCII file and prints
it in a JTextArea.
Now I would like to modify it so that it copies the input file to
an output file.
Here is the code:

File name = new File( actionEvent.getActionCommand() );
File nomfichier = new File( "FileCopied.txt" );
..
..
try {
BufferedReader input = new BufferedReader(
new FileReader( name ) );
PrintWriter output =
new PrintWriter( new BufferedWriter ( new FileWriter( nomfichier ) )
);
StringBuffer buffer = new StringBuffer();
String text;
outputArea.append( "\n\n" );

while ( ( text = input.readLine() ) != null )
{
buffer.append( text + "\n" );
output.write( text );
System.out.println( text );
}
outputArea.append( buffer.toString() );
} // end try

The program displays the input file in the JTextArea and the
System.out.println( text ) works.
But the output file exists but contains 0 bytes.
I have searched the Sun Java api but found nothing on the subject.
Can someone help me?
Many thanks
(e-mail address removed)
 
S

Stefan Waldmann

Jean-Benoit MORLA said:
Hi,

I have a program from a textbook that reads an ASCII file and prints
it in a JTextArea.
Now I would like to modify it so that it copies the input file to
an output file.
Here is the code:

File name = new File( actionEvent.getActionCommand() );
File nomfichier = new File( "FileCopied.txt" );
.
.
try {
BufferedReader input = new BufferedReader(
new FileReader( name ) );
PrintWriter output =
new PrintWriter( new BufferedWriter ( new FileWriter( nomfichier ) )
);
StringBuffer buffer = new StringBuffer();
String text;
outputArea.append( "\n\n" );

while ( ( text = input.readLine() ) != null )
{
buffer.append( text + "\n" );
output.write( text );
System.out.println( text );
}
outputArea.append( buffer.toString() );
} // end try

The program displays the input file in the JTextArea and the
System.out.println( text ) works.
But the output file exists but contains 0 bytes.
I have searched the Sun Java api but found nothing on the subject.
Can someone help me?
Many thanks
(e-mail address removed)

Did you flush your output stream? (Your code doesn't show it, so I
assume you don't). Since it's a buffered stream, it collects data
without writing it out directly, but only when when a certain amount of
data is cumulated, or flush() is called.

After writing into output, try:

output.flush();

Additionally, like always when working with Input- or Output Streams,
close the streams when you're done with it, using:

input.close();
output.close();

HTH
Stefan
 
A

Andrew Thompson

Sub: BufferedWriter does not write ( Beginners question)

Beginners are best helped at..
I have a program from a textbook

What textbook?
..that reads an ASCII file and prints
it in a JTextArea.
Now I would like to modify it so that it copies the input file to
an output file.

Does, by any chance, this book also have a
demonstration of *saving* a file? Go on,
flip a few pages, I dare you..

[ So you effectively want an editor? ]
Here is the code:

No, that is a *snippet* of your broken code,
which is a very different, less complete and
useful thing than your code *example* of
broken code. See here for details.
<http://www.physci.org/codes/sscce.jsp>

OTOH, you do not seem to mention 'flush'
and 'close' in your snippet.

And as far as the Sun link goes,
you might start here..
<http://java.sun.com/docs/books/tutorial/essential/io/>

HTH
 
J

Jean-Benoit MORLA

Andrew Thompson said:
Sub: BufferedWriter does not write ( Beginners question)

Beginners are best helped at..
I have a program from a textbook

What textbook?
..that reads an ASCII file and prints
it in a JTextArea.
Now I would like to modify it so that it copies the input file to
an output file.

Does, by any chance, this book also have a
demonstration of *saving* a file? Go on,
flip a few pages, I dare you..

[ So you effectively want an editor? ]
Here is the code:

No, that is a *snippet* of your broken code,
which is a very different, less complete and
useful thing than your code *example* of
broken code. See here for details.
<http://www.physci.org/codes/sscce.jsp>

OTOH, you do not seem to mention 'flush'
and 'close' in your snippet.

And as far as the Sun link goes,
you might start here..
<http://java.sun.com/docs/books/tutorial/essential/io/>

HTH
The textbook is: Deitel&Deitel Java How to Program 5th ( Int'l).
It comes with JSDK 1.4.1.
The original example does not close any file, but I'll do it now.
Also I'll try "flush()".
Unfortunately there is no example of writing to a file with BufferedWriter.
What I like about BufferedReader is that it deals with ASCII files.
Many thanks for your replies.
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top