Thursday, January 19, 2017

Apache FileUtils Examples.

Apache FileUtils Examples.



General file manipulation utilities.

Facilities are provided in the following areas:

1      Writing to a file.
2      Reading from a file.
3   Make a directory including parent directories.
4   Copying files and directories.
5     Deleting files and directories.
6   Converting to and from a URL.
7      Listing files and directories by filter and extension.
8   Comparing file content.
9    File last changed date.
1    Calculating a checksum.

Maven Repository
                <dependency>
                                                <groupId>commons-io</groupId>
                                                <artifactId>commons-io</artifactId>
                                                <version>2.4</version>
                                </dependency>

Examples.
package swain.swain;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.io.filefilter.SuffixFileFilter;

public class ApacheFileUtilsExample {

    private static final String PATH = "C:\\appdata\\";

    public static void main(String[] args) throws IOException {

         String fileString = FileUtils.readFileToString(new File(PATH + "file1.txt"));
         System.out.println("Reading file content:" + fileString);
         String fileString2 = FileUtils.readFileToString(new File(PATH + "file2.txt"));
         System.out.println("Reading content:" + fileString2);
         File file1 = new File(PATH + "file1.txt");
         File file2 = new File(PATH + "file2.txt");
         System.out.println("Comparing file content: " + FileUtils.contentEquals(file1, file2));

         FileUtils.copyDirectory(FileUtils.getFile(PATH), FileUtils.getFile(PATH + "copiedNewPath\\"));

         FileUtils.copyDirectory(FileUtils.getFile(PATH), FileUtils.getFile(PATH + "copiedFilterPath\\"), new SuffixFileFilter(".txt"));

         for (File f : FileUtils.getFile(PATH + "copiedFilterPath\\").listFiles()) {
             System.out.println("Contents of copiedFilterPath: " + f.getName());
         }

         File copy = FileUtils.getFile(PATH + "test1.txt");
         FileUtils.copyFile(file1, copy);
         System.out.println("Comparing file content:: " + FileUtils.contentEquals(file1, copy));

         for (File file : FileUtils.getFile(PATH).listFiles()) {
             System.out.println("File Name: " + file.getName());
         }

         FileUtils.deleteDirectory(FileUtils.getFile(PATH + "copiedFilterPath\\"));
         for (File file : FileUtils.getFile(PATH).listFiles()) {
             System.out.println("Contents of PATH after deletion: " + file.getName());
         }

         // Directories
         System.out.println("Temp Dir: " + FileUtils.getTempDirectory().getAbsolutePath());
         System.out.println("User Dir: " + FileUtils.getUserDirectory().getAbsolutePath());

         // Line Iterator
         LineIterator iter = FileUtils.lineIterator(file1);
         while (iter.hasNext()) {
             System.out.println("cmpFile1 lines: " + iter.next());
         }

         // Directory size
         System.out.println("Size of dir: " + FileUtils.sizeOfDirectory(FileUtils.getFile(PATH)) + " bytes.");

         // Ways of writing lines to a file.
         File fileToWrite1 = FileUtils.getFile(PATH + "file1.txt");
         File fileToWrite2 = FileUtils.getFile(PATH + "file2.txt");

         Collection<String> lines = new ArrayList<String>();
         lines.add("Hello world");
         lines.add("First Line");

         FileUtils.write(fileToWrite1, "Written with FileUtils!");
         FileUtils.writeLines(fileToWrite2, lines);

    }
}




No comments:

Post a Comment

How ChatGPT can Benefit Coding: Your Guide to Leveraging an AI Language Model

 Introduction: Hello, coders! Welcome to this blog post on how ChatGPT, an AI language model, can benefit your coding skills and projects. A...