public abstract class TextIO
extends java.lang.Object
A tool for reading and writing text files.
Different instances of this class use different encoding
("UTF-8", "ASCII", "UnicodeBig", etc.) To write or read
the text file in the given encoding, you may use one
of the constants declared in this class (such as ASCII
) or create a new instance of the TextIO.ForCharset
inheritor by TextIO.ForCharset.getInstance(String)
call.
Methods of this class throw NullPointerException
if one of the arguments is null.
Modifier and Type | Class and Description |
---|---|
static class |
TextIO.ForCharset
An implementation of
TextIO class that writes and reads text files
in encodings specified by their names (as the second argument of
InputStreamReader and OutputStreamWriter constructors). |
static class |
TextIO.SystemDefault
An implementation of
TextIO class that writes and reads text files
in the current system-dependent encoding. |
Modifier and Type | Field and Description |
---|---|
static TextIO.ForCharset |
ASCII
This
TextIO instance writes and reads text files
in the standard 7-bit "ASCII" encoding. |
static TextIO.ForCharset |
CP1252
This
TextIO instance writes and reads text files
in the standard 8-bit "Cp1252" encoding. |
static TextIO.ForCharset |
ISO8859_1
This
TextIO instance writes and reads text files
in the standard 8-bit "ISO8859_1" encoding. |
static TextIO.SystemDefault |
SYSTEM_DEFAULT
This
TextIO instance writes and reads text files
in the current system-dependent encoding. |
static TextIO.ForCharset |
UNICODE_BIG
This
TextIO instance writes and reads text files
in the standard 16-bit "UnicodeBig" encoding. |
static TextIO.ForCharset |
UNICODE_BIG_UNMARKED
This
TextIO instance writes and reads text files
in the standard 16-bit "UnicodeBigUnmarked" encoding. |
static TextIO.ForCharset |
UNICODE_LITTLE
This
TextIO instance writes and reads text files
in the standard 16-bit "UnicodeLittle" encoding. |
static TextIO.ForCharset |
UNICODE_LITTLE_UNMARKED
This
TextIO instance writes and reads text files
in the standard 16-bit "UnicodeLittleUnmarked" encoding. |
static TextIO.ForCharset |
UTF16
This
TextIO instance writes and reads text files
in the standard 16-bit "UTF-16" encoding. |
static TextIO.ForCharset |
UTF8
This
TextIO instance writes and reads text files
in the standard "UTF-8" encoding. |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
read(java.io.File file)
Reads the full content of the given text file and returns it as a
String . |
java.lang.String |
read(java.io.InputStream inputStream,
boolean closeStream)
Reads the text file from the current position of the input stream
until the end of this stream and returns it as a
String . |
char[] |
readChars(java.io.File file)
Reads the full content of the given text file and returns it as a
char[] . |
char[] |
readChars(java.io.InputStream inputStream,
boolean closeStream)
Reads the text file from the current position of the input stream
until the end of this stream and returns it as a
char[] . |
void |
write(java.io.File file,
java.lang.String data)
Saves the text passed in the
data argument in the given text file. |
void |
writeChars(java.io.File file,
char[] data)
Saves the text passed in the
data argument in the given text file. |
void |
writeCharsOrDelete(java.io.File file,
char[] data)
|
void |
writeOrDelete(java.io.File file,
java.lang.String data)
|
public static final TextIO.SystemDefault SYSTEM_DEFAULT
TextIO
instance writes and reads text files
in the current system-dependent encoding.public static final TextIO.ForCharset ASCII
TextIO
instance writes and reads text files
in the standard 7-bit "ASCII" encoding.public static final TextIO.ForCharset CP1252
TextIO
instance writes and reads text files
in the standard 8-bit "Cp1252" encoding.public static final TextIO.ForCharset ISO8859_1
TextIO
instance writes and reads text files
in the standard 8-bit "ISO8859_1" encoding.public static final TextIO.ForCharset UNICODE_BIG
TextIO
instance writes and reads text files
in the standard 16-bit "UnicodeBig" encoding. In this encoding:public static final TextIO.ForCharset UNICODE_BIG_UNMARKED
TextIO
instance writes and reads text files
in the standard 16-bit "UnicodeBigUnmarked" encoding. In this encoding:UNICODE_BIG
encoding.
public static final TextIO.ForCharset UNICODE_LITTLE
TextIO
instance writes and reads text files
in the standard 16-bit "UnicodeLittle" encoding. In this encoding:UNICODE_BIG
).
public static final TextIO.ForCharset UNICODE_LITTLE_UNMARKED
TextIO
instance writes and reads text files
in the standard 16-bit "UnicodeLittleUnmarked" encoding. In this encoding:UNICODE_LITTLE
encoding.
public static final TextIO.ForCharset UTF16
TextIO
instance writes and reads text files
in the standard 16-bit "UTF-16" encoding. In this encoding,
unlike UNICODE_BIG
and UNICODE_LITTLE
,
an order of writing bytes in every character can depend on
the used operating system. The start 16-bit prefix 0xFEFF
is always written in the text file. The read methods use
this prefix to determine the order of bytes: if the first byte
is 0xFE, it is "big-endian" order (as in UNICODE_BIG
),
if the first byte is 0xFF, it is "little-endian" order
(as in UNICODE_LITTLE
).public static final TextIO.ForCharset UTF8
TextIO
instance writes and reads text files
in the standard "UTF-8" encoding. It is the best variant for
most needs.public final java.lang.String read(java.io.InputStream inputStream, boolean closeStream) throws java.io.IOException
String
.
Useful for reading text data stored in resources when the
Class.getResourceAsStream
method is used to
access such resources.inputStream
- the input stream (will be read until its end)closeStream
- if true
, the input stream will be closed for sure
before ending this methodjava.io.IOException
- if an I/O error occursreadChars(InputStream, boolean)
public final char[] readChars(java.io.InputStream inputStream, boolean closeStream) throws java.io.IOException
char[]
.
Useful for reading text data stored in resources when the
Class.getResourceAsStream
method is used to
access such resources.inputStream
- the input stream (will be read until its end)closeStream
- if true
, the input stream will be closed for sure
before ending this methodjava.io.IOException
- if an I/O error occursread(InputStream, boolean)
public final java.lang.String read(java.io.File file) throws java.io.IOException
String
.file
- the file to be readjava.io.IOException
- if an I/O error occursreadChars(File)
public final char[] readChars(java.io.File file) throws java.io.IOException
char[]
.file
- the file to be readjava.io.IOException
- if an I/O error occursread(File)
public final void write(java.io.File file, java.lang.String data) throws java.io.IOException
data
argument in the given text file.
The file is fully rewritten and will contain this text only.
In a case of I/O errors, the file will be attempted to be deleted.file
- the file to be saveddata
- the text saved in the file (String
object)java.io.IOException
- if an I/O error occurswriteChars(File, char[])
public final void writeChars(java.io.File file, char[] data) throws java.io.IOException
data
argument in the given text file.
The file is fully rewritten and will contain this text only.
In a case of I/O errors, the file will be attempted to be deleted.file
- the file to be saveddata
- the text saved in the file (array of characters)java.io.IOException
- if an I/O error occurswrite(File, String)
public final void writeOrDelete(java.io.File file, java.lang.String data) throws java.io.IOException
file
- the file to be saveddata
- the bytes saved in the filejava.io.IOException
- if an I/O error occurspublic final void writeCharsOrDelete(java.io.File file, char[] data) throws java.io.IOException
file
- the file to be saveddata
- the bytes saved in the filejava.io.IOException
- if an I/O error occurs