it.andynaz.utils
Class UtilsIO

java.lang.Object
  extended by it.andynaz.utils.UtilsIO

public class UtilsIO
extends Object

Useful methods for an elementary file management.

Version:
2013/01/14
Author:
andynaz

Constructor Summary
private UtilsIO()
           
 
Method Summary
static void cp(File inFile, File outFile)
          Copy a file.
static void cp(InputStream in, OutputStream out)
          Copy the content of an InputStream in an OutputStream.
static boolean mv(File from, File to)
          Moves a file.
static StringBuilder readFromFile(File file)
          Reads the content of a text file.
static boolean writeToFile(File file, String text)
          Writes a string in a text file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UtilsIO

private UtilsIO()
Method Detail

readFromFile

public static StringBuilder readFromFile(File file)
                                  throws FileNotFoundException,
                                         IOException
Reads the content of a text file. The file content is returned as a StringBuilder for efficency.

Parameters:
file - file to be read
Returns:
a StringBuilder with the content of the file
Throws:
FileNotFoundException - if the file does not exist
IOException - if I/O errors occour

writeToFile

public static boolean writeToFile(File file,
                                  String text)
Writes a string in a text file. The method does not throw any exception. A boolean is returned with the result of the operation.

In case of error, the writing is interrupted and a log is produced on the standard output.

Parameters:
file - file to be written
text - text to write
Returns:
true if the writing has been done without errors, false otherwise

cp

public static void cp(InputStream in,
                      OutputStream out)
               throws IOException
Copy the content of an InputStream in an OutputStream. At the end of the copy, the streams are not closed.

Parameters:
in - stream to read
out - stream to write
Throws:
IOException - if I/O errors occour

cp

public static void cp(File inFile,
                      File outFile)
               throws IOException
Copy a file. The method does not any check if the destination file exist.

Parameters:
inFile - file to be copied
outFile - file in which to copy
Throws:
FileNotFoundException - if inFile does not exist
IOException - if I/O errors occour

mv

public static boolean mv(File from,
                         File to)
                  throws IOException
Moves a file. At first the file is moved using the File.renameTo(java.io.File); if this fail, a copy is made and the original file is deleted.

Note: if the original file can not be deleted, the method actually makes a copy.

Parameters:
from - file to move
to - new file position
Returns:
true if the move is completed correctly, false otherwise
Throws:
FilenotFoundException - if file from does not exist
IOException - if I/O errors occour


(c) 2013 andynaz - http://andynaz.altervista.org