Showing posts with label nio read file. Show all posts
Showing posts with label nio read file. Show all posts

Friday, 7 June 2019

Nio: FileChannel: Read and write together


Below step-by-step procedure explains how to copy a file using nio FileChannel.

Step 1: Create FileChannel from source file.
FileInputStream fin = new FileInputStream(sourceFilePath);
FileChannel fileInputChannel = fin.getChannel();

Step 2: Get the FileChannel from FileOutputStream.
FileOutputStream fout = new FileOutputStream(destinationFilePath)
FileChannel fileOutputChannel = fout.getChannel();

Step 3: Read data from fileInputChannel into the buffer and put the buffer in fileOutputChannel.
ByteBuffer buffer = ByteBuffer.allocate(1024);

int noOfBytesRead;

while ((noOfBytesRead = fileInputChannel.read(buffer)) != -1) {
         buffer.flip();
         fileOutputChannel.write(buffer);
         buffer.clear();
}

Find the below working application.

package com.sample.app;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Test {

 private static final String SOURCE_FILE_PATH = "C:\\Users\\krishna\\Documents\\Study\\HelloWorld.txt";
 private static final String DESTINATION_FILE_PATH = "C:\\Users\\krishna\\Documents\\Study\\HelloWorld(copy).txt";

 /**
  * Copy contents of the file <code>sourceFilePath</code> to
  * <code>destinationFilePath</code>
  * 
  * @param sourceFilePath
  * @param destinationFilePath
  * @throws IOException
  * @throws FileNotFoundException
  */
 public static void copyFile(String sourceFilePath, String destinationFilePath)
   throws FileNotFoundException, IOException {

  if (sourceFilePath == null || sourceFilePath.isEmpty()) {
   throw new IllegalArgumentException("sourceFilePath shoudn't be null or empty");
  }

  if (destinationFilePath == null || destinationFilePath.isEmpty()) {
   throw new IllegalArgumentException("destinationFilePath shoudn't be null or empty");
  }

  try (FileInputStream fin = new FileInputStream(sourceFilePath);
    FileOutputStream fout = new FileOutputStream(destinationFilePath)) {
   FileChannel fileInputChannel = fin.getChannel();
   FileChannel fileOutputChannel = fout.getChannel();

   ByteBuffer buffer = ByteBuffer.allocate(1024);

   int noOfBytesRead;

   while ((noOfBytesRead = fileInputChannel.read(buffer)) != -1) {
    buffer.flip();
    fileOutputChannel.write(buffer);
    buffer.clear();
   }

  }
 }

 public static void main(String... args) throws IOException {
  copyFile(SOURCE_FILE_PATH, DESTINATION_FILE_PATH);
 }
}




Previous                                                 Next                                                 Home

NIO: FileChannel: Reading data from a file

Below step-by-step procedure explains, how to read data from a file.

Step 1: Get the FileChannel instance from the file.
FileChannel channel = FileChannel.open(Paths.get(FILE_PATH), READ);

Step 2: Create a buffer.
ByteBuffer buffer = ByteBuffer.allocate(100);

Step 3: Read the contents of file from the channel and place the content in buffer.
while (channel.read(buffer) != -1) {
    buffer.flip();
    System.out.println(Charset.defaultCharset().decode(buffer));
    buffer.clear();
}

Step 4: Close the channel.
channel.close();

Test.java
package com.sample.app;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;


public class Test {

 private static final String FILE_PATH = "/Users/krishna/Documents/demo.txt";

 public static void printFileContents(String filePath) throws IOException {
  try (FileInputStream fin = new FileInputStream(filePath)) {
   FileChannel fileChannel = fin.getChannel();

   ByteBuffer buffer = ByteBuffer.allocate(100);

   int noOfBytesRead;

   while ((noOfBytesRead = fileChannel.read(buffer)) != -1) {
    String str = new String(buffer.array());
    System.out.println(str);
    buffer.flip();

   }

  }

 }

 
 public static void main(String... args) throws IOException {
  printFileContents(FILE_PATH);
 }
}



Previous                                                 Next                                                 Home