net.sweetohm.ant.util
Class IO

java.lang.Object
  extended by net.sweetohm.ant.util.IO

public class IO
extends java.lang.Object

This class is an utility class for IO.

Version:
1.0
Author:
Michel CASABIANCA

Field Summary
(package private) static java.lang.String BLANKS
          Separator characters
(package private) static java.lang.String DEFAULT_ENCODING
          The default encoding (ASCII)
(package private) static java.lang.String ENCODING_ATT
          The encoding attribute
(package private) static java.lang.String VERSION_ATT
          The version attribute
(package private) static java.lang.String VERSION_VAL
          The version value
(package private) static java.lang.String XML_HEADER_END
          The XML header end
(package private) static java.lang.String XML_HEADER_START
          The XML header start
 
Constructor Summary
IO()
           
 
Method Summary
static java.lang.String getXMLFileEncoding(java.io.File file)
          Return the encoding of an XML file.
static java.lang.String loadFile(java.io.File file)
          Read a file.
static java.lang.String loadFile(java.io.File file, java.lang.String encoding)
          Read a file using a given encoding.
static java.lang.String loadXMLFile(java.io.File file)
          Read an XML file.
static void main(java.lang.String[] args)
          Test method.
(package private) static java.lang.String parseXMLHeader(java.lang.String header)
          Parse XML header to: Check that this is an XML header.
static void writeFile(java.lang.String string, java.io.File file)
          Write a String into a file.
static void writeFile(java.lang.String string, java.io.File file, java.lang.String encoding)
          Write a String into a file using a given encoding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_ENCODING

static final java.lang.String DEFAULT_ENCODING
The default encoding (ASCII)

See Also:
Constant Field Values

XML_HEADER_START

static final java.lang.String XML_HEADER_START
The XML header start

See Also:
Constant Field Values

XML_HEADER_END

static final java.lang.String XML_HEADER_END
The XML header end

See Also:
Constant Field Values

BLANKS

static final java.lang.String BLANKS
Separator characters

See Also:
Constant Field Values

VERSION_ATT

static final java.lang.String VERSION_ATT
The version attribute

See Also:
Constant Field Values

VERSION_VAL

static final java.lang.String VERSION_VAL
The version value

See Also:
Constant Field Values

ENCODING_ATT

static final java.lang.String ENCODING_ATT
The encoding attribute

See Also:
Constant Field Values
Constructor Detail

IO

public IO()
Method Detail

loadFile

public static java.lang.String loadFile(java.io.File file)
                                 throws java.io.IOException
Read a file.

Parameters:
file - The file to read
Returns:
The content of the file as a String
Throws:
java.io.IOException

loadFile

public static java.lang.String loadFile(java.io.File file,
                                        java.lang.String encoding)
                                 throws java.io.IOException
Read a file using a given encoding.

Parameters:
file - The file to read
encoding - The encoding to read the file
Returns:
The content of the file as a String
Throws:
java.io.IOException

loadXMLFile

public static java.lang.String loadXMLFile(java.io.File file)
                                    throws java.io.IOException
Read an XML file. This method takes care of encoding of the loaded file by reading the first line to determine the file encoding.

Parameters:
file - The file to read.
Returns:
The content of the file as a String.
Throws:
java.io.IOException

getXMLFileEncoding

public static java.lang.String getXMLFileEncoding(java.io.File file)
                                           throws java.io.IOException
Return the encoding of an XML file. Return default encoding (ascii) if no encoding was set in the XML header.

Parameters:
file - The XML file to read.
Returns:
The encoding of the file as stated in the XML header.
Throws:
java.io.IOException

parseXMLHeader

static java.lang.String parseXMLHeader(java.lang.String header)
                                throws java.io.IOException
Parse XML header to:

Parameters:
header - a String value
Returns:
a String value
Throws:
java.io.IOException - if an error occurs (no XML header or bad XML version).

writeFile

public static void writeFile(java.lang.String string,
                             java.io.File file)
                      throws java.io.IOException
Write a String into a file.

Parameters:
string - the string to write
file - The file to write
Throws:
java.io.IOException

writeFile

public static void writeFile(java.lang.String string,
                             java.io.File file,
                             java.lang.String encoding)
                      throws java.io.IOException
Write a String into a file using a given encoding.

Parameters:
string - the string to write
file - The file to write
encoding - The encoding to use to write the file.
Throws:
java.io.IOException - if an error occurs

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Test method.

Parameters:
args - Command line arguments as a String[]. This should be the file to read.
Throws:
java.lang.Exception