Class EscapedLineWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable

    public class EscapedLineWriter
    extends java.io.BufferedWriter
    A writer that complements EscapedLineReader by automatically escaping characters in the lines that it writes. Long lines are wrapped and marked with the line wrap escape (last character in line is backslash).
    Since:
    3.0
    Author:
    Chris Jennings
    See Also:
    EscapedTextCodec
    • Field Summary

      • Fields inherited from class java.io.Writer

        lock
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean isUnicodeEscaped()
      Returns true if Unicode escapes will be used.
      void setUnicodeEscaped​(boolean escape)
      Sets whether uxxxx escapes should be used when writing characters outside of the ISO-8859 encoding range.
      void writeComment​(java.lang.String str)
      Writes one or more comment lines.
      void writeLine​(java.lang.String str)
      Writes a line to the output stream, escaping appropriate characters and wrapping long lines by inserting line breaks prefixed by backslashes.
      void writeProperties​(java.util.Map<java.lang.String,​java.lang.String> props)
      Writes a collection of properties as key, value pairs with escaping and line breaking.
      void writeProperty​(java.lang.String key, java.lang.String value)
      Writes a key, value string pair with escaping and line breaking.
      • Methods inherited from class java.io.BufferedWriter

        close, flush, newLine, write, write, write
      • Methods inherited from class java.io.Writer

        append, append, append, nullWriter, write, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • EscapedLineWriter

        public EscapedLineWriter​(java.io.File f)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • EscapedLineWriter

        public EscapedLineWriter​(java.io.File f,
                                 java.lang.String charset)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • EscapedLineWriter

        public EscapedLineWriter​(java.io.OutputStream out)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • EscapedLineWriter

        public EscapedLineWriter​(java.io.OutputStream out,
                                 java.lang.String charset)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • EscapedLineWriter

        public EscapedLineWriter​(java.io.Writer out,
                                 int sz)
      • EscapedLineWriter

        public EscapedLineWriter​(java.io.Writer out)
    • Method Detail

      • isUnicodeEscaped

        public boolean isUnicodeEscaped()
        Returns true if Unicode escapes will be used.
        Returns:
        true if characters outside of ISO-8859 are escaped
        See Also:
        setUnicodeEscaped(boolean)
      • setUnicodeEscaped

        public void setUnicodeEscaped​(boolean escape)
        Sets whether uxxxx escapes should be used when writing characters outside of the ISO-8859 encoding range. Default is true; can be set to false if the output file can represent all unicode characters directly (e.g., UTF-8).
        Parameters:
        escape - if true, Unicode escapes will be used
        See Also:
        isUnicodeEscaped()
      • writeLine

        public void writeLine​(java.lang.String str)
                       throws java.io.IOException
        Writes a line to the output stream, escaping appropriate characters and wrapping long lines by inserting line breaks prefixed by backslashes. The line should not contain a trailing newline; calls to writeLine(java.lang.String), writeComment(java.lang.String), and writeProperty(java.lang.String, java.lang.String) will automatically insert line breaks.
        Parameters:
        str - the string to write
        Throws:
        java.io.IOException - if an I/O error occurs while writing the comment
      • writeComment

        public void writeComment​(java.lang.String str)
                          throws java.io.IOException
        Writes one or more comment lines. The string will be divided into lines at newline (\n) characters and each line will be prefixed with # and written to the file.
        Parameters:
        str - the string to write as comment text
        Throws:
        java.io.IOException - if an I/O error occurs while writing the comment
      • writeProperty

        public void writeProperty​(java.lang.String key,
                                  java.lang.String value)
                           throws java.io.IOException
        Writes a key, value string pair with escaping and line breaking.
        Parameters:
        key - the name of the key
        value - the value of the key
        Throws:
        java.io.IOException - if an I/O error occurs
      • writeProperties

        public void writeProperties​(java.util.Map<java.lang.String,​java.lang.String> props)
                             throws java.io.IOException
        Writes a collection of properties as key, value pairs with escaping and line breaking.
        Parameters:
        props - the map of properties to write
        Throws:
        java.io.IOException - if an I/O error occurs