Class AbstractFileItemWriter<T>

All Implemented Interfaces:
ResourceAwareItemWriterItemStream<T>, ItemStream, ItemStreamWriter<T>, ItemWriter<T>, org.springframework.beans.factory.InitializingBean
Direct Known Subclasses:
FlatFileItemWriter, JsonFileItemWriter

public abstract class AbstractFileItemWriter<T> extends AbstractItemStreamItemWriter<T> implements ResourceAwareItemWriterItemStream<T>, org.springframework.beans.factory.InitializingBean
Base class for item writers that write data to a file or stream. This class provides common features like restart, force sync, append etc. The location of the output file is defined by a WritableResource which must represent a writable file.
Uses buffered writer to improve performance.
The implementation is not thread-safe.
Waseem Malik, Tomas Slanina, Robert Kasanicky, Dave Syer, Michael Minella, Mahmoud Ben Hassine, Glenn Renfro, Remi Kaeffer
  • Field Details


      public static final boolean DEFAULT_TRANSACTIONAL
      See Also:
    • logger

      protected static final org.apache.commons.logging.Log logger

      public static final String DEFAULT_LINE_SEPARATOR

      public static final String DEFAULT_CHARSET
    • state

    • shouldDeleteIfExists

      protected boolean shouldDeleteIfExists
    • lineSeparator

      protected String lineSeparator
    • append

      protected boolean append
  • Constructor Details

    • AbstractFileItemWriter

      public AbstractFileItemWriter()
  • Method Details

    • setForceSync

      public void setForceSync(boolean forceSync)
      Flag to indicate that changes should be force-synced to disk on flush. Defaults to false, which means that even with a local disk changes could be lost if the OS crashes in between a write and a cache flush. Setting to true may result in slower performance for usage patterns involving many frequent writes.
      forceSync - the flag value to set
    • setLineSeparator

      public void setLineSeparator(String lineSeparator)
      Public setter for the line separator. Defaults to the System property line.separator.
      lineSeparator - the line separator to set
    • setResource

      public void setResource( resource)
      Setter for a writable resource. Represents a file that can be written.
      Specified by:
      setResource in interface ResourceAwareItemWriterItemStream<T>
      resource - the resource to be written to
    • setEncoding

      public void setEncoding(String newEncoding)
      Sets encoding for output template.
      newEncoding - String containing the encoding to be used for the writer.
    • setShouldDeleteIfExists

      public void setShouldDeleteIfExists(boolean shouldDeleteIfExists)
      Flag to indicate that the target file should be deleted if it already exists, otherwise it will be created. Defaults to true, so no appending except on restart. If set to false and appendAllowed is also false then there will be an exception when the stream is opened to prevent existing data being potentially corrupted.
      shouldDeleteIfExists - the flag value to set
    • setAppendAllowed

      public void setAppendAllowed(boolean append)
      Flag to indicate that the target file should be appended if it already exists. If this flag is set then the flag shouldDeleteIfExists is automatically set to false, so that flag should not be set explicitly. Defaults value is false.
      append - the flag value to set
    • setShouldDeleteIfEmpty

      public void setShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty)
      Flag to indicate that the target file should be deleted if no lines have been written (other than header and footer) on close. Defaults to false.
      shouldDeleteIfEmpty - the flag value to set
    • setSaveState

      public void setSaveState(boolean saveState)
      Set the flag indicating whether or not state should be saved in the provided ExecutionContext during the ItemStream call to update. Setting this to false means that it will always start at the beginning on a restart.
      saveState - if true, state will be persisted
    • setHeaderCallback

      public void setHeaderCallback(FlatFileHeaderCallback headerCallback)
      headerCallback will be called before writing the first item to file. Newline will be automatically appended after the header is written.
      headerCallback - FlatFileHeaderCallback to generate the header
    • setFooterCallback

      public void setFooterCallback(FlatFileFooterCallback footerCallback)
      footerCallback will be called after writing the last item to file, but before the file is closed.
      footerCallback - FlatFileFooterCallback to generate the footer
    • setTransactional

      public void setTransactional(boolean transactional)
      Flag to indicate that writing to the buffer should be delayed if a transaction is active. Defaults to true.
      transactional - true if writing to buffer should be delayed.
    • write

      public void write(Chunk<? extends T> items) throws Exception
      Writes out a string followed by a "new line", where the format of the new line separator is determined by the underlying operating system.
      Specified by:
      write in interface ItemWriter<T>
      items - list of items to be written to output stream
      Exception - if an error occurs while writing items to the output stream
    • doWrite

      protected abstract String doWrite(Chunk<? extends T> items)
      Write out a string of items followed by a "new line", where the format of the new line separator is determined by the underlying operating system.
      items - to be written
      written lines
    • close

      public void close()
      Description copied from class: ItemStreamSupport
      Specified by:
      close in interface ItemStream
      close in class ItemStreamSupport
      See Also:
    • open

      public void open(ExecutionContext executionContext) throws ItemStreamException
      Initialize the reader. This method may be called multiple times before close is called.
      Specified by:
      open in interface ItemStream
      open in class ItemStreamSupport
      executionContext - current step's ExecutionContext. Will be the executionContext from the last run of the step on a restart.
      See Also:
    • update

      public void update(ExecutionContext executionContext)
      Description copied from class: ItemStreamSupport
      Return empty ExecutionContext.
      Specified by:
      update in interface ItemStream
      update in class ItemStreamSupport
      executionContext - to be updated
      See Also:
    • getOutputState

      protected AbstractFileItemWriter<T>.OutputState getOutputState()