Package ca.cgjennings.io
Class EscapedLineWriter
- java.lang.Object
-
- java.io.Writer
-
- java.io.BufferedWriter
-
- ca.cgjennings.io.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 complementsEscapedLineReader
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
-
-
Constructor Summary
Constructors Constructor Description EscapedLineWriter(java.io.File f)
EscapedLineWriter(java.io.File f, java.lang.String charset)
EscapedLineWriter(java.io.OutputStream out)
EscapedLineWriter(java.io.OutputStream out, java.lang.String charset)
EscapedLineWriter(java.io.Writer out)
EscapedLineWriter(java.io.Writer out, int sz)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
isUnicodeEscaped()
Returnstrue
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.
-
-
-
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()
Returnstrue
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 istrue
; can be set tofalse
if the output file can represent all unicode characters directly (e.g., UTF-8).- Parameters:
escape
- iftrue
, 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 towriteLine(java.lang.String)
,writeComment(java.lang.String)
, andwriteProperty(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 keyvalue
- 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
-
-