tags:

views:

204

answers:

4

I'm having a problem writing to a file:

FileInputStream fin;              
try
{
    fin = new FileInputStream ("c:/text.txt");     
    PrintStream p = new PrintStream(fin);
    p.println ("test");
    fin.close();               
}
catch (IOException ioe)
{
    System.err.println (ioe.getMessage);
}

Is there a problem with this code?

+6  A: 

You need to use a FileOutputStream.

ski
He also needs to close the `PrintStream`.
McDowell
Or at least to flush it.
java.is.for.desktop
+6  A: 

Get used to the following structure. You'll use it a lot in Java.

PrintStream out = null;
try {
  out = new PrintStream(new FileOutputStream("c:/text.txt"));
  out.println ("test");
} catch (IOException e) {
  System.err.println (e.getMessage);
} finally {
  if (out != null) {
    try { out.close(): } catch (Exception e) { }
  }
  out = null; // safe but not strictly necessary unless you reuse fin in the same scope
}

At least until ARM blocks hopefully eventuate in Java 7.

As noted, you should close the PrintStream and not the FileOutputStream so the above is a better form to use.

cletus
You may want to obey the contract and close the PrintStream
McDowell
Okay, that link to ARM Blocks has made my day. Upvote!
Dean J
A: 

The class: FileInputStream is used to read input from a file. If you want to write to the file, you can use: FileOutputStream. If you want to make your life really easy, you can use a BufferedOutputStream as well.

As pointed out, you should close your streams in the finally block. The reason why you want to do that is say your program isn't really small, and it's a larger application. If you forget to close file streams, for example, the application will hold on to it and if you try to do something to it on the file system (read: at least in Windows) you won't be able to it. We've all seen the 'File cannot be deleted because it's still in use' error.

Here's an example of using the FileOutputStream + BufferedOutputStream: http://www.javadb.com/write-to-file-using-bufferedoutputstream.

Malaxeur
+2  A: 

Problems with that code that immediately strike me:

  • Non-standard formatting.
  • Awkward variable names.
  • The exception handling is not good.
  • Failure to close the file in the case of exceptions. (Use acquire(); try { use(); } finally { release(); }.
  • Hidden use of default character encoding.
  • PrintStream swallows exceptions. BufferedOutputStream is better.
  • Failure to flush the decorator. It may still have data buffered. Although actually in this case you have left the PrintStream in auto-flush mode, which can be a performance issue.
  • Use / for a Windows path separator. You might be able to get away with it, but it's not good.

So:

FileOutputStream fileOut = new FileOutputStream(
    "c:\\text.txt"
);
try {
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
        fileOut,
        "UTF-8" // Or, say, Charset.defaultCharset()
    ));
    out.write("test");
    out.newLine()
    out.flush();
} finally {
    fileOut.close();
}
Tom Hawtin - tackline