Showing posts with label nio write to file. Show all posts
Showing posts with label nio write to 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: Write to a file


Below step-by-step procedure explains how to write to the file.

Step 1: Get the FileChannel from FileOutputStream.
FileOutputStream fout = new FileOutputStream(filePath)
FileChannel fileChannel = fout.getChannel();

Step 2: Fill the buffer with the content.
byte[] contentBytes = content.getBytes();
ByteBuffer byteBuffer = ByteBuffer.allocate(contentBytes.length);
byteBuffer.put(contentBytes);
byteBuffer.flip();

Step 3: Write the content to the file.
fileChannel.write(byteBuffer);

Find the below working application.

Test.java
package com.sample.app;

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 FILE_PATH = "C:\\Users\\krishna\\Documents\\Study\\demo.txt";
 
 public static void writeContentToFile(String content, String filePath) throws FileNotFoundException, IOException {
  if (content == null) {
   return;
  }

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

  try (FileOutputStream fout = new FileOutputStream(filePath)) {

   byte[] contentBytes = content.getBytes();
   ByteBuffer byteBuffer = ByteBuffer.allocate(contentBytes.length);
   byteBuffer.put(contentBytes);

   FileChannel fileChannel = fout.getChannel();
   byteBuffer.flip();
   fileChannel.write(byteBuffer);
  }

 }

 public static void main(String... args) throws IOException {
  writeContentToFile("Hello, How are you", FILE_PATH);
 }
}



Previous                                                 Next                                                 Home