Class LockableFileWriter

java.lang.Object
java.io.Writer
org.apache.commons.io.output.LockableFileWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class LockableFileWriter extends Writer
FileWriter that will create and honor lock files to allow simple cross thread file lock handling.

This class provides a simple alternative to FileWriter that will use a lock file to prevent duplicate writes.

Note: The lock file is deleted when close() is called - or if the main file cannot be opened initially. In the (unlikely) event that the lock file cannot be deleted, an exception is thrown.

By default, the file will be overwritten, but this may be changed to append. The lock directory may be specified, but defaults to the system property java.io.tmpdir. The encoding may also be specified, and defaults to the platform default.

  • Field Details

    • LCK

      private static final String LCK
      The extension for the lock file.
      See Also:
    • out

      private final Writer out
      The writer to decorate.
    • lockFile

      private final File lockFile
      The lock file.
  • Constructor Details

    • LockableFileWriter

      public LockableFileWriter(String fileName) throws IOException
      Constructs a LockableFileWriter. If the file exists, it is overwritten.
      Parameters:
      fileName - the file to write to, not null
      Throws:
      NullPointerException - if the file is null
      IOException - in case of an I/O error
    • LockableFileWriter

      public LockableFileWriter(String fileName, boolean append) throws IOException
      Constructs a LockableFileWriter.
      Parameters:
      fileName - file to write to, not null
      append - true if content should be appended, false to overwrite
      Throws:
      NullPointerException - if the file is null
      IOException - in case of an I/O error
    • LockableFileWriter

      public LockableFileWriter(String fileName, boolean append, String lockDir) throws IOException
      Constructs a LockableFileWriter.
      Parameters:
      fileName - the file to write to, not null
      append - true if content should be appended, false to overwrite
      lockDir - the directory in which the lock file should be held
      Throws:
      NullPointerException - if the file is null
      IOException - in case of an I/O error
    • LockableFileWriter

      public LockableFileWriter(File file) throws IOException
      Constructs a LockableFileWriter. If the file exists, it is overwritten.
      Parameters:
      file - the file to write to, not null
      Throws:
      NullPointerException - if the file is null
      IOException - in case of an I/O error
    • LockableFileWriter

      public LockableFileWriter(File file, boolean append) throws IOException
      Constructs a LockableFileWriter.
      Parameters:
      file - the file to write to, not null
      append - true if content should be appended, false to overwrite
      Throws:
      NullPointerException - if the file is null
      IOException - in case of an I/O error
    • LockableFileWriter

      @Deprecated public LockableFileWriter(File file, boolean append, String lockDir) throws IOException
      Constructs a LockableFileWriter.
      Parameters:
      file - the file to write to, not null
      append - true if content should be appended, false to overwrite
      lockDir - the directory in which the lock file should be held
      Throws:
      NullPointerException - if the file is null
      IOException - in case of an I/O error
    • LockableFileWriter

      public LockableFileWriter(File file, Charset charset) throws IOException
      Constructs a LockableFileWriter with a file encoding.
      Parameters:
      file - the file to write to, not null
      charset - the charset to use, null means platform default
      Throws:
      NullPointerException - if the file is null
      IOException - in case of an I/O error
      Since:
      2.3
    • LockableFileWriter

      public LockableFileWriter(File file, String charsetName) throws IOException
      Constructs a LockableFileWriter with a file encoding.
      Parameters:
      file - the file to write to, not null
      charsetName - the name of the requested charset, null means platform default
      Throws:
      NullPointerException - if the file is null
      IOException - in case of an I/O error
      UnsupportedCharsetException - thrown instead of UnsupportedEncodingException in version 2.2 if the encoding is not supported.
    • LockableFileWriter

      public LockableFileWriter(File file, Charset charset, boolean append, String lockDir) throws IOException
      Constructs a LockableFileWriter with a file encoding.
      Parameters:
      file - the file to write to, not null
      charset - the name of the requested charset, null means platform default
      append - true if content should be appended, false to overwrite
      lockDir - the directory in which the lock file should be held
      Throws:
      NullPointerException - if the file is null
      IOException - in case of an I/O error
      Since:
      2.3
    • LockableFileWriter

      public LockableFileWriter(File file, String charsetName, boolean append, String lockDir) throws IOException
      Constructs a LockableFileWriter with a file encoding.
      Parameters:
      file - the file to write to, not null
      charsetName - the encoding to use, null means platform default
      append - true if content should be appended, false to overwrite
      lockDir - the directory in which the lock file should be held
      Throws:
      NullPointerException - if the file is null
      IOException - in case of an I/O error
      UnsupportedCharsetException - thrown instead of UnsupportedEncodingException in version 2.2 if the encoding is not supported.
  • Method Details

    • testLockDir

      private void testLockDir(File lockDir) throws IOException
      Tests that we can write to the lock directory.
      Parameters:
      lockDir - the File representing the lock directory
      Throws:
      IOException - if we cannot write to the lock directory
      IOException - if we cannot find the lock file
    • createLock

      private void createLock() throws IOException
      Creates the lock file.
      Throws:
      IOException - if we cannot create the file
    • initWriter

      private Writer initWriter(File file, Charset charset, boolean append) throws IOException
      Initializes the wrapped file writer. Ensure that a cleanup occurs if the writer creation fails.
      Parameters:
      file - the file to be accessed
      charset - the charset to use
      append - true to append
      Returns:
      The initialized writer
      Throws:
      IOException - if an error occurs
    • close

      public void close() throws IOException
      Closes the file writer and deletes the lock file.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Writer
      Throws:
      IOException - if an I/O error occurs.
    • write

      public void write(int c) throws IOException
      Writes a character.
      Overrides:
      write in class Writer
      Parameters:
      c - the character to write
      Throws:
      IOException - if an I/O error occurs.
    • write

      public void write(char[] cbuf) throws IOException
      Writes the characters from an array.
      Overrides:
      write in class Writer
      Parameters:
      cbuf - the characters to write
      Throws:
      IOException - if an I/O error occurs.
    • write

      public void write(char[] cbuf, int off, int len) throws IOException
      Writes the specified characters from an array.
      Specified by:
      write in class Writer
      Parameters:
      cbuf - the characters to write
      off - The start offset
      len - The number of characters to write
      Throws:
      IOException - if an I/O error occurs.
    • write

      public void write(String str) throws IOException
      Writes the characters from a string.
      Overrides:
      write in class Writer
      Parameters:
      str - the string to write
      Throws:
      IOException - if an I/O error occurs.
    • write

      public void write(String str, int off, int len) throws IOException
      Writes the specified characters from a string.
      Overrides:
      write in class Writer
      Parameters:
      str - the string to write
      off - The start offset
      len - The number of characters to write
      Throws:
      IOException - if an I/O error occurs.
    • flush

      public void flush() throws IOException
      Flushes the stream.
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in class Writer
      Throws:
      IOException - if an I/O error occurs.