简体   繁体   中英

Problem writing to file

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?

您需要使用FileOutputStream

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.

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();
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM