상세 컨텐츠

본문 제목

org.apache.commons.io

IT 세상/자바세상

by 이현민 (지후지율아빠) 2009. 12. 28. 20:51

본문


1.The org.apache.commons.io.IOUtils
org.apache.commons.io.IOUtils은 스트림과 리더로 부터 String과 Byte Array를 생성하고 안전하게 스트림을 닫는 IO와 관련된 툴들을 포함하고 있다.


아래 예제는 일반적으로 URL로 부터 읽어들인 바이트를 프린팅하는 방법이다.

InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
try {
 InputStreamReader inR = new InputStreamReader( in );
 BufferedReader buf = new BufferedReader( inR );
 String line;
 while ( ( line = buf.readLine() ) != null ) {
     System.out.println( line );
 }
} finally {
 in.close();
}

 

IOUtils class로 아래와 같이 바꿀수 있다.

InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
try {
 System.out.println( IOUtils.toString( in ) );
} finally {
 IOUtils.closeQuietly(in);
}

이 Class로 다량의 시간을 절약할수 있으며 그리고 잘 테스트되었던 코드에 의지할수 있다.


2.The org.apache.commons.io.FileUtils

The org.apache.commons.io.FileUtils class는
methods for retrieving different components of File Path(디렉토리명, File Base Name, 파일 확장자),
파일들을 다른파일들과 디렉토리에 카피하는 method
그리고 디렉토리 querying, 삭제, cleaning 에 대한 method 메소드를 포함하고 있다.

 

FileUtils Method들.....

static String byteCountToDisplaySize(long size)
          Returns a human-readable version of the file size (original is in bytes).
static void cleanDirectory(File directory)
          Clean a directory without deleting it.
static boolean contentEquals(File file1, File file2)
          Compare the contents of two files to determine if they are equal or not.
static File[] convertFileCollectionToFileArray(Collection files)
          Converts a Collection containing java.io.File instanced into array representation.
static void copyFile(File source, File destination)
          Copy file from source to destination.
static void copyFile(File source, File destination, boolean preserveFileDate)
          Copy file from source to destination.
static void copyFileToDirectory(File source, File destinationDirectory)
          Copy file from source to destination.
static void copyURLToFile(URL source, File destination)
          Copies bytes from the URL source to a file destination.
static void deleteDirectory(File directory)
          Recursively delete a directory.
static void forceDelete(File file)
           Delete a file.
static void forceDeleteOnExit(File file)
          Schedule a file to be deleted when JVM exits.
static void forceMkdir(File directory)
          Make a directory.
static boolean isFileNewer(File file, Date date)
          Tests if the specified File is newer than the specified Date.
static boolean isFileNewer(File file, File reference)
          Tests if the specified File is newer than the reference File.
static boolean isFileNewer(File file, long timeMillis)
          Tests if the specified File is newer than the specified time reference.
static Collection listFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
          Finds files within a given directory (and optionally its subdirectories).
static Collection listFiles(File directory, String[] extensions, boolean recursive)
          Finds files within a given directory (and optionally its subdirectories) which match an array of extensions.
static byte[] readFileToByteArray(File file)
           Reads the contents of a file into a byte array.
static String readFileToString(File file, String encoding)
           Reads the contents of a file into a String.
static long sizeOfDirectory(File directory)
          Recursively count size of a directory (sum of the length of all files).
static File toFile(URL url)
          Convert from a URL to a File.
static File[] toFiles(URL[] urls)
          Converts each of an array of URL to a File.
static void touch(File file)
          Implements the same behaviour as the "touch" utility on Unix.
static URL[] toURLs(File[] files)
          Converts each of an array of File to a URL.
static boolean waitFor(File file, int seconds)
          Waits for NFS to propagate a file creation, imposing a timeout.
static void writeByteArrayToFile(File file, byte[] data)
           Writes a byte array to a file creating the file if it does not exist.
static void writeStringToFile(File file, String data, String encoding)
           Writes a String to a file creating the file if it does not exist.


3.The org.apache.commons.io.CopyUtils
다양한 copy 메소드들이 존재하나 대부분 Deprecated 됐다.(Ver 1.1)

 

 

나머지 사세한 사항은 자카르타 홈페이지에서 확인할수 있다.

참고 : http://jakarta.apache.org/commons/io/

반응형

관련글 더보기