Exception : java.io.Writer.write(Unknown source)

D

Daku

Could some Java guru please help. I am using:
java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode, sharing)

I have a simple command line driven program that outputs large amounts
of text into a simple text file. In the constructor, I have :
if(filename != null)
{
try
{
outFile = new File(filename);
if(!outFile.exists())
{
outFile.createNewFile();
}
if(outFile != null)
{
fileWriter = new FileWriter(outFile);
if(fileWriter != null)
{
bufferedWriter = new
BufferedWriter(fileWriter);
}
}
}
catch(IOException ioe)
{
System.out.println(" constructor IO exception");
ioe.printStackTrace();
System.exit(0);
}
}

In the main code body, I have functions that write output to text file
as:
bufferedWriter.write(<some_text>);

And then I frequently get the IO exception:
java.io.Writer.write(Unknown source)

Is this is a known Java bug, or is there something wrong in my code ?
Any hints, suggestions would be of great help - thanks in advance for
your help.
 
A

Arved Sandstrom

Daku said:
Could some Java guru please help. I am using:
java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode, sharing)

I have a simple command line driven program that outputs large amounts
of text into a simple text file. In the constructor, I have :
if(filename != null)
{
try
{
outFile = new File(filename);
if(!outFile.exists())
{
outFile.createNewFile();
}
if(outFile != null)
{
fileWriter = new FileWriter(outFile);
if(fileWriter != null)
{
bufferedWriter = new
BufferedWriter(fileWriter);
}
}
}
catch(IOException ioe)
{
System.out.println(" constructor IO exception");
ioe.printStackTrace();
System.exit(0);
}
}

In the main code body, I have functions that write output to text file
as:
bufferedWriter.write(<some_text>);

And then I frequently get the IO exception:
java.io.Writer.write(Unknown source)

Is this is a known Java bug, or is there something wrong in my code ?
Any hints, suggestions would be of great help - thanks in advance for
your help.
What exactly is an entire typical stack trace for that code? Showing
only the last line is not helpful.

AHS
 
R

Roedy Green

I have a simple command line driven program that outputs large amounts
of text into a simple text file.

For a large amount of text, you probably want a larger buffer than the
default.
 
R

Roedy Green

if(filename != null)
{
try
{
outFile = new File(filename);
if(!outFile.exists())
{
outFile.createNewFile();
}
if(outFile != null)
{
fileWriter = new FileWriter(outFile);
if(fileWriter != null)
{
bufferedWriter = new
BufferedWriter(fileWriter);
}
}
}
This is all unnecessary. new FileWriter will automatically create the
file or overwrite it.
 
R

Roedy Green

outFile.createNewFile();

My guess is there are lags in the Windows OS when you create and
delete files and directories. If you are too quick with the next step
the previous one is not complete. Then you can find yourself trying to
write, for example, to a file the OS still has in use.


See http://mindprod.com/jgloss/file.html for some background.
 
M

Mike Schilling

Daku said:
Could some Java guru please help. I am using:
java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode, sharing)

I have a simple command line driven program that outputs large
amounts
of text into a simple text file. In the constructor, I have :
if(filename != null)
{
try
{
outFile = new File(filename);
if(!outFile.exists())
{
outFile.createNewFile();
}
if(outFile != null)
{
fileWriter = new FileWriter(outFile);
if(fileWriter != null)
{
bufferedWriter = new
BufferedWriter(fileWriter);
}
}
}
catch(IOException ioe)
{
System.out.println(" constructor IO exception");
ioe.printStackTrace();
System.exit(0);
}
}

There's no need for all the "if (x !-= null)" checks. "new" never
returns null. Nor is there any reason to create a file so that you
can overwrite it. This can be simplified to

if (filename !=-null)
{
try
{
outFile = new File(filename);
fileWriter = new FileWriter(outFile);
bufferedWriter = new BufferedWriter(fileWriter);
}
catch (IOException ex)
{
...
}
}


In the main code body, I have functions that write output to text
file
as:
bufferedWriter.write(<some_text>);

And then I frequently get the IO exception:
java.io.Writer.write(Unknown source)

"unknown source" means simply that the JVM doesn't have a line number
to report. It says nothing about the actual problem being reported.
What's needed to invesitage this is the complete stacktrace, including
the type of exception being throws and the exception message. The
line of code that triggers the exception would be useful too.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top