PrintWriter vs FileWriter in Java

后端 未结 7 1112
谎友^
谎友^ 2020-11-29 19:36

Are PrintWriter and FileWriter in Java the same and no matter which one to use? So far I have used both because their results are the same. Is there some special cases where

相关标签:
7条回答
  • 2020-11-29 19:53

    Both of these use a FileOutputStream internally:

    public PrintWriter(File file) throws FileNotFoundException {
    this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
         false);
    }
    
    
    
    public FileWriter(File file) throws IOException {
    super(new FileOutputStream(file));
    }
    

    but the main difference is that PrintWriter offers special methods:

    Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.

    Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.

    0 讨论(0)
  • 2020-11-29 19:53

    A PrintWriter has a different concept of error handling. You need to call checkError() instead of using try/catch blocks.

    0 讨论(0)
  • 2020-11-29 19:54

    The fact that java.io.FileWriter relies on the default character encoding of the platform makes it rather useless to me. You should never assume something about the environment where your app will be deployed.

    0 讨论(0)
  • 2020-11-29 20:03

    just to provide more info related to FLUSH and Close menthod related to FileOutputStream

    flush() ---just makes sure that any buffered data is written to disk flushed compltely and ready to write again to the stream (or writer) afterwards.

    close() ----flushes the data and closes any file handles, sockets or whatever.Now connection has been lost and you can't write anything to outputStream.

    0 讨论(0)
  • 2020-11-29 20:06

    PrintWriter doesn't throw IOException.You should call checkError() method for this purpose.

    0 讨论(0)
  • 2020-11-29 20:08

    According to coderanch.com, if we combine the answers we get:

    FileWriter is the character representation of IO. That means it can be used to write characters. Internally FileWriter would use the default character set of the underlying OS and convert the characters to bytes and write it to the disk.

    PrintWriter & FileWriter.

    Similarities

    1. Both extend from Writer.
    2. Both are character representation classes, that means they work with characters and convert them to bytes using default charset.

    Differences

    1. FileWriter throws IOException in case of any IO failure, this is a checked exception.
    2. None of the PrintWriter methods throw IOExceptions, instead they set a boolean flag which can be obtained using checkError().
    3. PrintWriter has an optional constructor you may use to enable auto-flushing when specific methods are called. No such option exists in FileWriter.
    4. When writing to files, FileWriter has an optional constructor which allows it to append to the existing file when the "write()" method is called.

    Difference between PrintStream and OutputStream: Similar to the explanation above, just replace character with byte.

    PrintWriter has following methods :

    close()
    flush()
    format()
    printf()
    print()
    println()
    write()
    

    and constructors are :

    File (as of Java 5)
    String (as of Java 5)
    OutputStream
    Writer
    

    while FileWriter having following methods :

    close()
    flush()
    write()
    

    and constructors are :

    File
    String 
    

    Link: http://www.coderanch.com/t/418148/java-programmer-SCJP/certification/Information-PrintWriter-FileWriter

    0 讨论(0)
提交回复
热议问题