wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes error while writing to a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "error while writing to a file" Watch "error while writing to a file" New topic
Author

error while writing to a file

abhinav sinha
Greenhorn

Joined: Nov 29, 2007
Posts: 22
Hi,

I am getting an error while writing to a file.Here is the sample code in which I am getting error.The STDERR is getting printed to console but the same is not getting written to a file.

------------------------------------------------------------------
<code>

package Sample;

import java.util.*;
import java.io.*;
public class MediocreExecJavac
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("perl ic_start");
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
FileWriter fw=new FileWriter("result.txt");
String line = null;
System.out.println("<ERROR>");
while ( (line = br.readLine()) != null)
System.out.println(line);
fw.write(line);
System.out.println("</ERROR>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
fw.close();
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

<\code>
----------------------------------------------------------------------

Below is the output -

<ERROR>
Can't open perl script "ic_start": No such file or directory
java.lang.NullPointerException
at java.io.Writer.write(Unknown Source)
at Sample.MediocreExecJavac.main(MediocreExecJavac.java:21)


-----------------------------------------------------------------------

Please tell where the program is going wrong.
abhinav sinha
Greenhorn

Joined: Nov 29, 2007
Posts: 22
aahh..I am sorry guys.I found my mistake.Now my program is running fine.
:-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error while writing to a file