Friday, April 3, 2020

Non-blocking I/O(NIO) File handling operations

In this post I will list down some of common file operations which encounter in day today basis when working. Following are required to run the provided project shared at end of the post.

Prerequisites 
  1. You should have install java 1.8 or above.
  2. You should have Eclipse installed in your PC.
  3. Your PC should setup Maven installed and configured.


Create Files

For this one also we can use Files.createFile method in java.nio.file.Files class. For this method we need to provide Path object of new file.  Note that this method will throw java.nio.file.FileAlreadyExistsException when file already existing. As you can see sample code in createFile() method.


String parentFolderPath="C:\\JAVA8_FILE_HANDLING\\src\\main\\resources\\PARENT_FOLDER\\";

File newFile= new File(parentFolderPath+"Test.txt");

if(!newFile.exists())
    Files.createFile(newFile.toPath());

System.out.println("Newly created file exists :: "+newFile.exists());



List everything in a folder
For listing of folder we can useFiles.list method in java.nio.file.Files class. For that method we can pass the folder path which we need to list down. Note that this list method will throw IOException so we have to handle it in our code. You can see sample code in listDownAllFilesAndFolder() method.

String parentFolderPath=
"C:\\JAVA8_FILE_HANDLING\\src\\main\\resources\\PARENT_FOLDER\\";

List fileList = Files.list(Paths.get(parentFolderPath)).collect(Collectors.toList());

fileList.forEach(System.out::println);



Search file in a folder
For this one also we can use Files.list method in java.nio.file.Files class with Filter functionality. As you can see sample code in SearchFilesInFolder() method.

String parentFolderPath=
"C:\\JAVA8_FILE_HANDLING\\src\\main\\resources\\PARENT_FOLDER\\";

String fileToSearch="srilanka";


List fileList = Files.list(Paths.get(parentFolderPath))
        .filter(path -> new File(path.toString()).getName().startsWith(fileToSearch))
        .collect(Collectors.toList());

fileList.forEach(System.out::println);



Read Property File Line by Line

For this one also we can use Files.lines method in java.nio.file.Files class and with help of Stream we can put all lines in to List as show in below example. As you can see sample code in readPropertyFileLineByLine() method.


String fileName = 
"C:\\JAVA8_FILE_HANDLING\\src\\main\\resources\\PARENT_FOLDER\\SampleProperty-2020-04-04.properties";


List<String> list = new ArrayList<>();
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
    
    list = stream
            .collect(Collectors.toList());} 
catch (IOException e) {
    e.printStackTrace();
}

list.forEach(System.out::println);



Moving Files and Folders

For this one also we can use Files.move method in java.nio.file.Files class and for this method we need to provide source path and destination path. For this method we can pass optional parameter which we can say to program whether we need to override existing files in destination folder. One most important thing in this method is it will remove all the items inside source folder and source folder it self as well. As you can see sample code in moveFilesAndFolders() method. 


String parentFolderPath="C:\\JAVA8_FILE_HANDLING\\src\\main\\resources\\PARENT_FOLDER\\";

String destFolderPath="C:\\JAVA8_FILE_HANDLING\\src\\main\\resources\\OUTPUT\\";

Files.move(Paths.get(parentFolderPath),Paths.get(destFolderPath)
, StandardCopyOption.REPLACE_EXISTING);

List fileList = Files.list(Paths.get(destFolderPath)).collect(Collectors.toList());

fileList.forEach(System.out::println);




No comments:

Post a Comment