Apache Commons IO – Useful Resources ”; Previous Next The following resources contain additional information on Apache Commons IO. Please use them to get more in-depth knowledge on this. Useful Links on Apache Commons IO Apache Commons IO Wiki − Wikipedia reference for Apache Commons IO. Apache Commons IO − Reference for Apache Common Collections. To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;
Category: commons Io
Apache Commons IO – FileUtils ”; Previous Next Provides method to manipulates files like moving, opening, checking existence, reading of file etc. These methods use File Object. Class Declaration Following is the declaration for org.apache.commons.io.FileUtils Class − public class FileUtils extends Object Features of FileUtils The features of FileUtils are stated below − Methods to write to a file. Methods to read from a file. Methods to make a directory including parent directories. Methods to copy files and directories. Methods to delete files and directories. Methods to convert to and from a URL. Methods to list files and directories by filter and extension. Methods to compare file content. Methods to file last changed date. Methods to calculating a checksum. Example of FileUtils Class Here is the input file we need to parse − Welcome to TutorialsPoint. Simply Easy Learning. IOTester.java import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import org.apache.commons.io.FileUtils; public class IOTester { public static void main(String[] args) { try { //Using FileUtils usingFileUtils(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingFileUtils() throws IOException { //get the file object File file = FileUtils.getFile(“input.txt”); //get the temp directory File tmpDir = FileUtils.getTempDirectory(); System.out.println(tmpDir.getName()); //copy file to temp directory FileUtils.copyFileToDirectory(file, tmpDir); //create a new file File newTempFile = FileUtils.getFile(tmpDir, file.getName()); //get the content String data = FileUtils.readFileToString(newTempFile, Charset.defaultCharset()); //print the content System.out.println(data); } } Output It will print the following result. Temp Welcome to TutorialsPoint. Simply Easy Learning. Print Page Previous Next Advertisements ”;
Apache Commons IO – AndFileFilter ”; Previous Next AndFileFilter provides conditional and logic across a list of file filters. It returns true, if all filters in the list return true. Otherwise, it returns false. Class Declaration Following is the declaration for org.apache.commons.io.filefilter.AndFileFilter Class − public class AndFileFilter extends AbstractFileFilter implements ConditionalFileFilter, Serializable Example of AndFileFilter Class Here is the input file we need to parse − Welcome to TutorialsPoint. Simply Easy Learning. Let”s print all files and directories in the current directory and then, filter a file with name starting with . and ends with t. IOTester.java import java.io.File; import java.io.IOException; import org.apache.commons.io.filefilter.AndFileFilter; import org.apache.commons.io.filefilter.PrefixFileFilter; import org.apache.commons.io.filefilter.WildcardFileFilter; public class IOTester { public static void main(String[] args) { try { usingAndFileFilter(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingAndFileFilter() throws IOException { //get the current directory File currentDirectory = new File(“.”); //get names of all files and directory in current directory String[] files = currentDirectory.list(); System.out.println(“All files and Folders.n”); for( int i = 0; i < files.length; i++ ) { System.out.println(files[i]); } System.out.println(“nFile starting with . and ends with tn”); String[] filesNames = currentDirectory.list( new AndFileFilter( new PrefixFileFilter(“.”), new WildcardFileFilter(“*t”))); for( int i = 0; i < filesNames.length; i++ ) { System.out.println(filesNames[i]); } } } Output It will print the following result. All files and Folders. .classpath .project .settings bin input.txt src File starting with . or ends with t .project Print Page Previous Next Advertisements ”;
Apache Commons IO – NameFileFilter ”; Previous Next NameFileFilter in Commons IO filters the file-names for a name. Class Declaration Following is the declaration for org.apache.commons.io.filefilter.NameFileFilter Class : public class NameFileFilter extends AbstractFileFilter implements Serializable Example of NameFileFilter Class Here is the input file we need to parse − Welcome to TutorialsPoint. Simply Easy Learning. Let”s print all files and directories in the current directory and then, filter a file whose name is Input.txt. IOTester.java import java.io.File; import java.io.IOException; import org.apache.commons.io.IOCase; import org.apache.commons.io.filefilter.NameFileFilter; public class IOTester { public static void main(String[] args) { try { usingNameFileFilter(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingNameFileFilter() throws IOException { //get the current directory File currentDirectory = new File(“.”); //get names of all files and directory in current directory String[] files = currentDirectory.list(); System.out.println(“All files and Folders.n”); for( int i = 0; i < files.length; i++ ) { System.out.println(files[i]); } System.out.println(“nFile with name input.txtn”); String[] acceptedNames = {“input”, “input.txt”}; String[] filesNames = currentDirectory.list( new NameFileFilter(acceptedNames, IOCase.INSENSITIVE) ); for( int i = 0; i < filesNames.length; i++ ) { System.out.println(filesNames[i]); } } } Output It will print the following result − All files and Folders. .classpath .project .settings bin input.txt src File with name input.txt input.txt Print Page Previous Next Advertisements ”;
Apache Commons IO – PrefixFileFilter ”; Previous Next PrefixFileFilter filters the files which are based on prefix. Class Declaration Following is the declaration for org.apache.commons.io.filefilter.PrefixFileFilter Class − public class PrefixFileFilter extends AbstractFileFilter implements Serializable Example of PrefixFileFilter Class Here is the input file we need to parse − Welcome to TutorialsPoint. Simply Easy Learning. Let”s print all files and directories in the current directory and then, filter a file with name starting with input. IOTester.java import java.io.File; import java.io.IOException; import org.apache.commons.io.filefilter.PrefixFileFilter; public class IOTester { public static void main(String[] args) { try { usingPrefixFileFilter(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingPrefixFileFilter() throws IOException { //get the current directory File currentDirectory = new File(“.”); //get names of all files and directory in current directory String[] files = currentDirectory.list(); System.out.println(“All files and Folders.n”); for( int i = 0; i < files.length; i++ ) { System.out.println(files[i]); } System.out.println(“nFile starting with inputn”); String[] filesNames = currentDirectory.list( new PrefixFileFilter(“input”) ); for( int i = 0; i < filesNames.length; i++ ) { System.out.println(filesNames[i]); } } } Output It will print the following result − All files and Folders. .classpath .project .settings bin input.txt src File with extenstion txt input.txt Print Page Previous Next Advertisements ”;
Apache Commons IO – FileEntry ”; Previous Next FileEntry provides the state of a file or directory. File attributes at a point in time. Class Declaration Following is the declaration for org.apache.commons.io.monitor.FileEntry Class − public class FileEntry extends Object implements Serializable Features of FileEntry FileEntry class object provides the following file attributes at a point in time. getName() − file name. exists() − checks if file exists or not. isDirectory() − checks if file is a directory. lastModified() − gives last modified date time. listFiles() − gives content of directory. Example of FileEntry Class Here is the input file we need to parse − Welcome to TutorialsPoint. Simply Easy Learning. IOTester.java import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.commons.io.monitor.FileEntry; public class IOTester { public static void main(String[] args) { try { usingFileEntry(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingFileEntry() throws IOException { //get the file object File file = FileUtils.getFile(“input.txt”); FileEntry fileEntry = new FileEntry(file); System.out.println(“Monitored File: ” + fileEntry.getFile()); System.out.println(“File name: ” + fileEntry.getName()); System.out.println(“Is Directory: ” + fileEntry.isDirectory()); } } Output It will print the following result. Monitored File: input.txt File name: input.txt Is Directory: false Print Page Previous Next Advertisements ”;
Apache Commons IO – TeeOutputStream ”; Previous Next TeeOutputStream splits the OutputStream. It is named after the unix ”tee” command. It allows a stream to be branched to two streams. Class Declaration Following is the declaration for org.apache.commons.io.output.TeeOutputStream Class − public class TeeOutputStream extends ProxyOutputStream Example of TeeOutputStream Class In this example, TeeOutputStream accepts two output streams as parameter and passing data to TeeOutputStream set data to both output streams. IOTester.java import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import org.apache.commons.io.input.TeeInputStream; import org.apache.commons.io.output.TeeOutputStream; public class IOTester { private static final String SAMPLE = “Welcome to TutorialsPoint. Simply Easy Learning.”; public static void main(String[] args) { try { usingTeeInputStream(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingTeeInputStream() throws IOException { TeeInputStream teeInputStream = null; TeeOutputStream teeOutputStream = null; try { ByteArrayInputStream inputStream = new ByteArrayInputStream(SAMPLE.getBytes(“US-ASCII”)); ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream(); teeOutputStream = new TeeOutputStream(outputStream1, outputStream2); teeInputStream = new TeeInputStream(inputStream, teeOutputStream, true); teeInputStream.read(new byte[SAMPLE.length()]); System.out.println(“Output stream 1: ” + outputStream1.toString()); System.out.println(“Output stream 2: ” + outputStream2.toString()); } catch (IOException e) { System.out.println(e.getMessage()); } finally { //teeIn.close() closes teeIn and teeOut which in turn closes the out1 and out2. try { teeInputStream.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } } } Output It will print the following result. Output stream 1: Welcome to TutorialsPoint. Simply Easy Learning. Output stream 2: Welcome to TutorialsPoint. Simply Easy Learning. Print Page Previous Next Advertisements ”;
Apache Commons IO – NameFileComparator ”; Previous Next NameFileComparator compare the names of two files. It can be used to sort the lists or arrays of files, using their name, either in a case-sensitive, case-insensitive or system dependent case sensitive way. Class Declaration Following is the declaration for org.apache.commons.io.comparator.NameFileComparator Class − public class NameFileComparator extends Object implements Serializable Example of NameFileComparator Class Here is the input file we need to parse − Welcome to TutorialsPoint. Simply Easy Learning. IOTester.java import java.io.File; import java.io.FileFilter; import java.io.IOException; import org.apache.commons.io.IOCase; import org.apache.commons.io.comparator.NameFileComparator; import org.apache.commons.io.filefilter.FileFileFilter; public class IOTester { public static void main(String[] args) { try { usingNameFileComparator(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingNameFileComparator() throws IOException { //get the current directory File currentDirectory = new File(“.”); NameFileComparator comparator = new NameFileComparator(IOCase.INSENSITIVE); File[] sortedFiles = comparator.sort(currentDirectory.listFiles((FileFilter)FileFileFilter.FILE)); System.out.println(“Sorted By Name: “); for(File file:sortedFiles) { System.out.println(file.getName()); } } } Output It will print the following result. Sorted By Name: .classpath .project input.txt Print Page Previous Next Advertisements ”;
LastModifiedFileComparator
LastModifiedFileComparator ”; Previous Next LastModifiedFileComparator compares the last modified dates of two files/directory. It can be used to sort the lists or arrays of files/directories using their last modified dates. Class Declaration Following is the declaration for org.apache.commons.io.comparator.LastModifiedFileComparator Class − public class LastModifiedFileComparator extends Object implements Serializable Example of LastModifiedFileComparator Class Here is the input file we need to parse − Welcome to TutorialsPoint. Simply Easy Learning. IOTester.java import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.util.Date; import org.apache.commons.io.comparator.LastModifiedFileComparator; import org.apache.commons.io.filefilter.FileFileFilter; public class IOTester { public static void main(String[] args) { try { usingLastModifiedFileComparator(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingLastModifiedFileComparator() throws IOException { //get the current directory File currentDirectory = new File(“.”); LastModifiedFileComparator comparator = new LastModifiedFileComparator(); File[] sortedFiles = comparator.sort(currentDirectory.listFiles((FileFilter)FileFileFilter.FILE)); System.out.println(“Sorted By Last Modified date: “); for(File file:sortedFiles) { System.out.println(file.getName() + “, Modified on: ” + new Date(file.lastModified())); } } } Output It will print the following result. Sorted By Last Modified date: .project, Modified on: Thu Oct 12 19:06:45 IST 2017 .classpath, Modified on: Mon Nov 20 13:09:55 IST 2017 input.txt, Modified on: Mon Nov 20 19:27:55 IST 2017 Print Page Previous Next Advertisements ”;
Apache Commons IO – IOCase
Apache Commons IO – IOCase ”; Previous Next Enumeration of IO case sensitivity. Different Operating systems have different rules for case-sensitivity for file names. For example, Windows is case-insensitive for file naming while Unix is case-sensitive. IOCase captures that difference, provides an enumeration to control how filename comparisons should be performed. It also provides methods to use the enumeration to perform comparisons. Enum Declaration Following is the declaration for org.apache.commons.io.IOCase Enum − public enum IOCase extends Enum<IOCase> implements Serializable Example of IOCase Enum An example of IOCase Enum is given below − IOTester.java import java.io.IOException; import org.apache.commons.io.IOCase; public class IOTester { public static void main(String[] args) { try { usingIOCase(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingIOCase() throws IOException { String text = “Welcome to TutorialsPoint. Simply Easy Learning.”; String text1 = “WELCOME TO TUTORIALSPOINT. SIMPLY EASY LEARNING.”; System.out.println(“Ends with Learning (case sensitive): ” + IOCase.SENSITIVE.checkEndsWith(text1, “Learning.”)); System.out.println(“Ends with Learning (case insensitive): ” + IOCase.INSENSITIVE.checkEndsWith(text1, “Learning.”)); System.out.println(“Equality Check (case sensitive): ” + IOCase.SENSITIVE.checkEquals(text, text1)); System.out.println(“Equality Check (case insensitive): ” + IOCase.INSENSITIVE.checkEquals(text, text1)); } } Output It will print the following result − Ends with Learning (case sensitive): false Ends with Learning (case insensitive): true Equality Check (case sensitive): false Equality Check (case insensitive): true Print Page Previous Next Advertisements ”;