Provides method to manipulates files like moving, opening, checking existence, reading of file etc. These methods use File Object.
Following is the declaration for org.apache.commons.io.FileUtils Class −
public class FileUtils extends Object
Here is the input file we need to parse −
Welcome to Howcodex. 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); } }
It will print the following result.
Temp Welcome to Howcodex. Simply Easy Learning.