JDK-4338871 : PrintWriter does not throw IOException for full floppy disk
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.2.2
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2000-05-17
  • Updated: 2005-10-21
  • Resolved: 2005-10-21
Description
Name: stC104175			Date: 05/17/2000


java version "1.3.0"
Java (TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot (TM) Client VM (build 1.3.0-C, mixed mode)

Write files to an almost full floppy disk. Files will be properly written until
floppy disk is full. After that _no_ exception is thrown that the disk is full,
but empty files of 0KB are written to floppy until name-table fills up. Then an
exception is thrown.


public class SimplCon {

	public SimplCon () {

	}

    static public void main(String args[]) {
        try {
            String filename = "a:\\file";
            File f;
            for (int i = 0; ; i++) {
                f = new File(filename + i + ".txt");
                FileOutputStream fos = new FileOutputStream(f);
                PrintWriter w = new PrintWriter(new OutputStreamWriter(fos));
                String text = "blah blah blah blah blah blah blah blah";

                w.print(text);
                w.flush();
                fos.flush();
                fos.close();
                w.close();
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace(new PrintStream(System.err));
        }
    }

}
(Review ID: 104951) 
======================================================================

Comments
EVALUATION This is not a bug. The problem is that all methods in PrintWriter never throw I/O exceptions, so the exception indicating that the disk is full is never propageted to the test program. If the for loop is re-written to avoid the PrintWriter, the exception is thrown as expected: for (int i = 0; ; i++) { f = new File(filename + i + ".txt"); FileOutputStream fos = new FileOutputStream(f); String text = "blah blah blah blah blah blah blah blah"; fos.write(text.getBytes()); fos.flush(); fos.close(); } This is not an ideal solution because it uses the deprecated method String.getBytes(). OUTPUT: C:\temp>java Test2 There is not enough space on the disk java.io.IOException: There is not enough space on the disk at java.io.FileOutputStream.writeBytes(Native Method) at java.io.FileOutputStream.write(FileOutputStream.java:247) at Test2.main(Test2.java:19) C:\temp>dir a: Volume in drive A is NO Volume Serial Number is DCC7-E13D Directory of A:\ 10/20/2005 10:58 PM 113,000 fill2.txt 10/20/2005 11:04 PM 39 file0.txt 10/20/2005 11:04 PM 39 file1.txt 10/20/2005 11:04 PM 0 file2.txt 10/20/2005 10:53 PM 1,343,400 fill.txt 5 File(s) 1,456,478 bytes 0 Dir(s) 0 bytes free Note that file2.txt is of size 0 because the exception was thrown by the write, after the file was created. A small work-around in the existing code would be to explicitly detect errors by calling PrintWriter.checkError(). Alternatively, a better solution would be to use an implementation of Writer which does not dispose of the exceptions, for instance FileWriter: for (int i = 0; ; i++) { FileWriter w = new FileWriter(filename + i + ".txt"); String text = "blah blah blah blah blah blah blah blah"; w.write(text); w.flush(); w.close(); } Note that ideally, the close() invocation would be within the finally portion of a try-finally block to ensure that resources are always released even if an exception has occurred at an earlier point in the code. OUTPUT: C:\temp>java Test3 There is not enough space on the disk java.io.IOException: There is not enough space on the disk at java.io.FileOutputStream.writeBytes(Native Method) at java.io.FileOutputStream.write(FileOutputStream.java:260) at sun.nio.cs.StreamEncoder$CharsetSE.writeBytes(StreamEncoder.java:336) at sun.nio.cs.StreamEncoder$CharsetSE.implFlushBuffer(StreamEncoder.java:404) at sun.nio.cs.StreamEncoder$CharsetSE.implFlush(StreamEncoder.java:408) at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:152) at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:213) at Test3.main(Test3.java:16) C:\temp>dir a: Volume in drive A is NO Volume Serial Number is DCC7-E13D Directory of A:\ 10/20/2005 10:58 PM 113,000 fill2.txt 10/21/2005 11:57 AM 39 file0.txt 10/21/2005 11:57 AM 39 file1.txt 10/21/2005 11:57 AM 0 file2.txt 10/20/2005 10:53 PM 1,343,400 fill.txt 5 File(s) 1,456,478 bytes 0 Dir(s) 0 bytes free
2005-10-21