Showing posts with label Java IO. Show all posts
Showing posts with label Java IO. Show all posts

Sunday, September 29, 2024

How to Read File From The Last Line in Java

There are applications where you have to read huge files line by line may be using some tool like Pentaho, Camel. Let's say these files are in the format header, records and footer and you need to check something in the footer (last line of the file) and if that condition is not met you need to reject the file. Now, reading line by line in such scenario, will be wasteful as you'll anyway reject the file in the end.

So for such scenarios it is better to read the last line (or may be last N lines) of the file, to have better throughput. In this post you will see how to read a file from the last line in Java.


In Java reading file from the end can be done using RandomAccessFile which has a seek method to set the file-pointer offset, measured from the beginning of the file.

Apache Commons IO also has a ReversedLinesFileReader class which reads lines in a file reversely.

File used

Let's say you have a file aa.txt which has the following content-

This is the first line.
This is the second line.
This is the third line.
This is the fourth line.

And you want to read last line of this file using Java.

Saturday, April 20, 2024

Writing a File Asynchronously Java Program

In this post we’ll see how to write a file asynchronously in Java using java.nio.channels.AsynchronousFileChannel class added in Java 7. Using AsynchronousFileChannel class you can create an asynchronous channel for reading, writing, and manipulating a file.

To see how to read a file asynchronously in Java, refer this post- Read File Asynchronously Java Program

Opening an Asynchronous channel

Whether you are reading or writing a file asynchronously first thing you need to do is to create an asynchronous channel. For that you need to use static open() method of the AsynchronousFileChannel class which opens or creates a file for reading or writing, returning an asynchronous file channel to access the file.

Following code snippet shows how you can create an asynchronous file channel for writing to a file.

Path path = Paths.get("F:\\netjs\\WriteFile.txt");
AsynchronousFileChannel asyncFileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE)

Writing file asynchronously using AsynchronousFileChannel class

For writing a file asynchronously there are two versions of write() method in the AsynchronousFileChannel class.

  1. write() method that writes the passed buffer to the file and returns a Future representing the result of the write operation.
  2. write() method where you pass CompletionHandler instance as an argument.

We’ll see examples of of both of these ways to write a file asynchronously to have a better idea.

Writing to a file asynchronously in Java

1. In the first Java program we’ll use the write method that returns a Future.

Future<Integer> write(ByteBuffer src, long position)- This method writes a sequence of bytes to this channel from the passed buffer, starting at the given file position.

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class AsyncFileWrite {
  public static void main(String[] args) {
    Path path = Paths.get("F:\\netjs\\test.txt");
    // increase the buffer size if required
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    // Write Data to buffer
    buffer.put("This will be written to a file.".getBytes());
    buffer.flip();
    try(AsynchronousFileChannel asyncChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE)){
      // Write to async channel from buffer
      // starting from position 0
      Future<Integer> future =  asyncChannel.write(buffer, 0);
      while(!future.isDone()) {
        System.out.println("Waiting for async write operation... ");
        // You can do other processing
      }            
      buffer.clear();            
      System.out.println("Total bytes written- " + future.get());
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ExecutionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } 
  }
}

2. In this Java program for writing file asynchronously we’ll use the write() method where CompletionHandler instance is passed as an argument.

CompletionHandler interface defines two callback methods which you need to implement.

  • completed(V result, A attachment)- Invoked when an operation has completed.
  • failed(Throwable exc, A attachment)- Invoked when an operation fails.
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.channels.CompletionHandler;

public class AsyncFileWrite {
  public static void main(String[] args) throws IOException {
    Path path = Paths.get("F:\\netjs\\test.txt");
    if(!Files.exists(path)){
      Files.createFile(path);
    }
    // increase the buffer size if required
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    // Write Data to buffer
    buffer.put("This will be written to a file.".getBytes());
    buffer.flip();
    try(AsynchronousFileChannel asyncChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE)){
      // Write to channel from buffer, start from position 0
      asyncChannel.write(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
        @Override
        public void completed(Integer result, ByteBuffer attachment) {
          System.out.println("Total bytes written- " + result);
        }

        @Override
        public void failed(Throwable ex, ByteBuffer attachment) {
          System.out.println("Write operation failed- " + ex.getMessage());                    
        }
      });            
    }
  }
}

That's all for this topic Writing a File Asynchronously Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Write to a File in Java
  2. How to Append to a File in Java
  3. How to Write Excel File in Java Using Apache POI
  4. Creating Temporary File in Java
  5. Reading File in Java Using BufferedReader

You may also like-

  1. How to Create Password Protected Zip File in Java
  2. Creating PDF in Java Using Apache PDFBox
  3. Arrange Non-Negative Integers to Form Largest Number - Java Program
  4. Invoking Getters And Setters Using Reflection in Java
  5. HashMap in Java With Examples
  6. Interface in Java
  7. Passing Arguments to getBean() Method in Spring
  8. YARN in Hadoop

Monday, April 15, 2024

Read File Asynchronously Java Program

In this post we’ll see how to read file asynchronously in Java using java.nio.channels.AsynchronousFileChannel class added in Java 7. Using AsynchronousFileChannel class you can create an asynchronous channel for reading, writing, and manipulating a file.

Opening an Asynchronous channel

Whether you are reading or writing a file asynchronously first thing you need to do is to create an asynchronous channel. For that you can use static open() method of the AsynchronousFileChannel class which opens or creates a file for reading or writing, returning an asynchronous file channel to access the file.

Following code snippet shows how you can create an asynchronous file channel for reading a file.

Path path = Paths.get("F:\\netjs\\test.txt");
AsynchronousFileChannel asyncFileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ)

Reading file asynchronously using AsynchronousFileChannel class

For reading a file asynchronously there are two versions of read() method in the AsynchronousFileChannel class.

  1. read() method that returns a Future.
  2. read() method where you can pass CompletionHandler instance as an argument.

We’ll see examples of of both of these ways to have a better idea.

Reading file asynchronously in Java

1. In the first Java program we’ll use the read method that returns a Future.

abstract Future<Integer> read(ByteBuffer dst, long position)- This method reads sequence of bytes from the opened channel into the buffer passed as argument. Reading starts at the passed file position.

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.Future;

public class AsyncFileRead {
  public static void main(String[] args) {
    Path path = Paths.get("F:\\netjs\\test.txt");
    // buffer into which data is read
    // increase the buffer size if required
    ByteBuffer buffer = ByteBuffer.allocate(4096);
    try(AsynchronousFileChannel asyncChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ)){
      Future<Integer> result = asyncChannel.read(buffer, 0);
      while(!result.isDone()) {
        System.out.println("Waiting for read... ");
        // You can do other processing
      }
      /* File data ready */
      //limit is set to the current position 
      //and position is set to zero
      buffer.flip();
      String data = new String(buffer.array()).trim();
      System.out.println(data);
      buffer.clear();            
    } catch (IOException e) {
      System.out.println("Error while reading file- " + e.getMessage());
      e.printStackTrace();
    }        
  }
}

2. In this Java program for reading file asynchronously we’ll use the read() method where CompletionHandler instance is passed as an argument.

CompletionHandler interface defines two callback methods which you need to implement.

  • completed(V result, A attachment)- Invoked when an operation has completed.
  • failed(Throwable exc, A attachment)- Invoked when an operation fails.
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.channels.CompletionHandler;

public class AsyncFileRead {
  public static void main(String[] args) {
    Path path = Paths.get("F:\\netjs\\test.txt");
    // buffer into which data is read
    // increase the buffer size if required
    ByteBuffer buffer = ByteBuffer.allocate(4096);
    try(AsynchronousFileChannel asyncChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ)){
      asyncChannel.read(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
        // callback methods
        @Override
        public void completed(Integer result, ByteBuffer attachment) {
          System.out.println("Number of bytes read- " + result);
          attachment.flip();
          String data = new String(attachment.array()).trim();
          System.out.println("..." + data.length());
          System.out.println(data);
          attachment.clear();
        }

        @Override
        public void failed(Throwable ex, ByteBuffer attachment) {
          System.out.println("Read operation failed- " + ex.getMessage());                
        }            
      });
      System.out.println("Asynchronous operation in progress.. Once over callback method will be called.");
    } catch (IOException e) {
      System.out.println("Error while reading file- " + e.getMessage());
      e.printStackTrace();
    }        
  }
}

That's all for this topic Read File Asynchronously Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Reading File in Java Using Files.lines And Files.newBufferedReader
  2. Reading All Files in a Folder - Java Program
  3. How to Read Properties File in Java
  4. How to Read File From The Last Line in Java
  5. How to Create PDF From XML Using Apache FOP

You may also like-

  1. Write to a File in Java
  2. Setting And Getting Thread Name And Thread ID - Java Program
  3. How to Untar a File - Java Program
  4. Converting Numbers to Words - Java Program
  5. Nested Class And Inner Class in Java
  6. Generic Class, Interface And Generic Method in Java
  7. Angular Two-Way Data Binding With Examples
  8. Magic Methods in Python With Examples

Friday, April 12, 2024

Creating Tar File And GZipping Multiple Files in Java

If you want to GZIP multiple files that can’t be done directly as you can only compress a single file using GZIP. In order to GZIP multiple files you will have to archive multiple files into a tar and then compress it to create a .tar.gz compressed file. In this post we'll see how to create a tar file in Java and gzip multiple files.

Refer How to Untar a File in Java to see how to untar a file.

Using Apache Commons Compress

Here I am posting a Java program to create a tar file using Apache Commons Compress library. You can download it from here– https://commons.apache.org/proper/commons-compress/download_compress.cgi

Make sure to add commons-compress-xxx.jar in your application’s class path. I have used commons-compress-1.13 version.

Steps to create tar files

Steps for creating tar files in Java are as follows-

  1. Create a FileOutputStream to the output file (.tar.gz) file.
  2. Create a GZIPOutputStream which will wrap the FileOutputStream object.
  3. Create a TarArchiveOutputStream which will wrap the GZIPOutputStream object.
  4. Then you need to read all the files in a folder.
  5. If it is a directory then just add it to the TarArchiveEntry.
  6. If it is a file then add it to the TarArchiveEntry and also write the content of the file to the TarArchiveOutputStream.

Folder Structure used

Here is a folder structure used in this post to read the files. Test, Test1 and Test2 are directories here and then you have files with in those directories. Your Java code should walk through the whole folder structure and create a tar file with all the entries for the directories and files and then compress it.

Test
  abc.txt
  Test1
     test.txt
     test1.txt
  Test2
     xyz.txt

Creating tar file in Java example

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;

public class TarGZIPDemo {
 public static void main(String[] args) {
  String SOURCE_FOLDER = "/home/netjs/Documents/netjs/Test";
  TarGZIPDemo tGzipDemo = new TarGZIPDemo();
  tGzipDemo.createTarFile(SOURCE_FOLDER);

 }
  private void createTarFile(String sourceDir){
    TarArchiveOutputStream tarOs = null;
    try {
      File source = new File(sourceDir);
      // Using input name to create output name
      FileOutputStream fos = new FileOutputStream(source.getAbsolutePath().concat(".tar.gz"));
      GZIPOutputStream gos = new GZIPOutputStream(new BufferedOutputStream(fos));
      tarOs = new TarArchiveOutputStream(gos);
      addFilesToTarGZ(sourceDir, "", tarOs);    
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
      try {
        tarOs.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
 
 public void addFilesToTarGZ(String filePath, String parent, TarArchiveOutputStream tarArchive) throws IOException {
  File file = new File(filePath);
  // Create entry name relative to parent file path 
  String entryName = parent + file.getName();
  // add tar ArchiveEntry
  tarArchive.putArchiveEntry(new TarArchiveEntry(file, entryName));
  if(file.isFile()){
   FileInputStream fis = new FileInputStream(file);
   BufferedInputStream bis = new BufferedInputStream(fis);
   // Write file content to archive
   IOUtils.copy(bis, tarArchive);
   tarArchive.closeArchiveEntry();
   bis.close();
  }else if(file.isDirectory()){
   // no need to copy any content since it is
   // a directory, just close the outputstream
   tarArchive.closeArchiveEntry();
   // for files in the directories
   for(File f : file.listFiles()){        
    // recursively call the method for all the subdirectories
    addFilesToTarGZ(f.getAbsolutePath(), entryName+File.separator, tarArchive);
   }
  }          
 }
}

On opening the created .tar.gz compressed file using archive manager.

creating .tar.gz file in Java

That's all for this topic Creating Tar File And GZipping Multiple Files in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Zipping Files And Folders in Java
  2. Unzip File in Java
  3. Compress And Decompress File Using GZIP Format in Java
  4. Java Program to Convert a File to Byte Array
  5. Reading Delimited File in Java Using Scanner

You may also like-

  1. How to Create Deadlock in Java
  2. Convert int to String in Java
  3. Read or List All Files in a Folder in Java
  4. How to Compile Java Program at Runtime
  5. How HashMap Works Internally in Java
  6. Serialization Proxy Pattern in Java
  7. Bounded Type Parameter in Java Generics
  8. Polymorphism in Java

Tuesday, June 20, 2023

Java Program to Print Line Numbers With Lines in Java

If you want to print line number along with the lines of the file you can do that using LineNumberReader in Java.

LineNumberReader class has a method getLineNumber() that gives the current line number of the file. So, wrap your Reader object with in the LineNumberReader to get this extra functionality to get line numbers.

Printing lines of the file with line number Java code

If you have a file named abc.txt with the following content:

This is a test file.
Line number reader is used to read this file.
This program will read all the lines.
It will give the count.

Then you can get the line numbers using the following Java code.

 
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class LineNumberDemo {
 public static void main(String[] args) {
  LineNumberReader reader = null;
    try {
      reader = new LineNumberReader(new FileReader(new File("F:\\abc.txt")));
      String str;
      // Read file till the end
      while ((str = reader.readLine()) != null){
        System.out.println(reader.getLineNumber() + "- " + str);
      }         
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally { 
      if(reader != null){
        try {
          reader.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
}

Output

 
1- This is a test file.
2- Line number reader is used to read this file.
3- This program will read all the lines.
4- It will give the count.

That's all for this topic Java Program to Print Line Numbers With Lines in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. How to Read File From The Last Line in Java
  2. Reading Delimited File in Java Using Scanner
  3. Reading File in Java Using Files.lines And Files.newBufferedReader
  4. Writing a File Asynchronously Java Program
  5. How to Count Lines in a File in Java

You may also like-

  1. How to Create Password Protected Zip File in Java
  2. How to Read Input From Console in Java
  3. Convert int to String in Java
  4. How to Find Last Modified Date of a File in Java
  5. JavaScript Array slice() method With Examples
  6. Run Python Application in Docker Container
  7. Spring MVC Java Configuration Example
  8. Custom Async Validator in Angular Template-Driven Form

Saturday, June 17, 2023

Reading Delimited File in Java Using Scanner

In this post we'll see how to read delimited file (like CSV) in Java using Scanner class.

A Scanner, when reading input, breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

The scanner can also use delimiters other than whitespace. Scanner class has useDelimiter() method which can be used to change default delimiter. There are two overloaded useDelimiter() methods.

  • useDelimiter(Pattern pattern)- Sets this scanner's delimiting pattern to the specified pattern.
  • useDelimiter(String pattern)- Sets this scanner's delimiting pattern to a pattern constructed from the specified String.

Java program to read CSV file using Scanner

Let's see an example where Scanner class is used to read a CSV file.

If there is a CSV file with following data-

Pride And Prejudice,Jane Austen,20.76
The Murder of Roger Ackroyd,Agatha Christie,25.67
Atlas Shrugged,Ayn Rand,34.56
Gone with the Wind,Margaret Mitchell,36.78

And you want to read and parse the line so that you have Book name, author and price as separate strings.

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class ScanDelimited {

 public static void main(String[] args) {
  // CSV file
  File file = new File("G:\\Temp.csv");
  Scanner sc = null;
  try {
   sc = new Scanner(file);
   // Check if there is another line of input
   while(sc.hasNextLine()){
    String str = sc.nextLine();
    parseLine(str);
   }
   
  } catch (IOException  exp) {
   // TODO Auto-generated catch block
   exp.printStackTrace();
  }
  
  sc.close();
 }
 
 private static void parseLine(String str){
  String book, author, price;
  Scanner sc = new Scanner(str);
  sc.useDelimiter(",");

  // Check if there is another line of input
  while(sc.hasNext()){
   book = sc.next();
   author = sc.next();
   price = sc.next();
   System.out.println("Book - " + book + " Author - " + author + 
     " Price - " + price);  
  }
  sc.close();
 }
}

Output

Book - Pride And Prejudice Author - Jane Austen Price - 20.76
Book - The Murder of Roger Ackroyd Author - Agatha Christie Price - 25.67
Book - Atlas Shrugged Author - Ayn Rand Price - 34.56
Book - Gone with the Wind Author - Margaret Mitchell Price - 36.78

Java program to read pipe (|) delimited file using Scanner

If you have a file where pipe is used as delimiter then you can specify that as delimiter with useDelimiter() method to read the file.

Data

Pride And Prejudice|Jane Austen|20.76
The Murder of Roger Ackroyd|Agatha Christie|25.67
Atlas Shrugged|Ayn Rand|34.56
Gone with the Wind|Margaret Mitchell|36.78
package org.netjs.examples1;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class ScanDelimited {

 public static void main(String[] args) {
  // delimited file
  File file = new File("G:\\abc.txt");
  Scanner sc = null;
  try {
   sc = new Scanner(file);
   // Check if there is another line of input
   while(sc.hasNextLine()){
    String str = sc.nextLine();
    parseLine(str);
   }
   
  } catch (IOException  exp) {
   // TODO Auto-generated catch block
   exp.printStackTrace();
  }
  
  sc.close();
 }
 
 private static void parseLine(String str){
  String book, author, price;
  Scanner sc = new Scanner(str);
  sc.useDelimiter("[|]");

  // Check if there is another line of input
  while(sc.hasNext()){
   book = sc.next();
   author = sc.next();
   price = sc.next();
   System.out.println("Book - " + book + " Author - " + author + 
     " Price - " + price);  
  }
  sc.close();
 } 
}

Output

Book - Pride And Prejudice Author - Jane Austen Price - 20.76
Book - The Murder of Roger Ackroyd Author - Agatha Christie Price - 25.67
Book - Atlas Shrugged Author - Ayn Rand Price - 34.56
Book - Gone with the Wind Author - Margaret Mitchell Price - 36.78

That's all for this topic Reading Delimited File in Java Using Scanner. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Reading File in Java Using Scanner
  2. Reading File in Java Using Files.lines And Files.newBufferedReader
  3. How to Read Input From Console in Java
  4. How to Read File From The Last Line in Java
  5. Creating Tar File And GZipping Multiple Files in Java

You may also like-

  1. Java Program to Reverse a Number
  2. Producer-Consumer Java Program Using wait notify
  3. Difference Between Two Dates in Java
  4. If Given String Sub-Sequence of Another String in Java
  5. finally Block in Java Exception Handling
  6. Callable and Future in Java With Examples
  7. Difference between HashMap and HashTable in Java
  8. Dependency Injection Using factory-method in Spring

Tuesday, June 13, 2023

How to Count Lines in a File in Java

Sometimes you just want to count the number of lines in a file, rather than reading the content of file. The easiest way, I feel, is to use LineNumberReader for counting the lines in a file in Java.

LineNumberReader class has a method getLineNumber() that gives the current line number of the file. So the logic for counting the number of lines in a file is as follows-

Using the LineNumberReader read all the lines of the files until you reach the end of file. Then use getLineNumber() method to get the current line number.

Using the getLineNumber() method you can also display line numbers along with the lines of the file.

Java program to count number of lines in a file

If you have a file with lines as follows-

This is a test file.
Line number reader is used to read this file.
This program will read all the lines.
It will give the count.

Then you can get the count of lines using the following code-

 
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class LineNumberDemo {
  public static void main(String[] args) {
    LineNumberReader reader = null;
    try {
      reader = new LineNumberReader(new FileReader(new File("F:\\abc.txt")));
      // Read file till the end
      while ((reader.readLine()) != null);
      System.out.println("Count of lines - " + reader.getLineNumber());
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally { 
      if(reader != null){
        try {
          reader.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
}

Output

 
Count of lines – 4

That's all for this topic How to Count Lines in a File in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Java Program to Print Line Numbers With Lines in Java
  2. Reading Delimited File in Java Using Scanner
  3. Reading File in Java Using Files.lines And Files.newBufferedReader
  4. How to Read File From The Last Line in Java
  5. Zipping Files And Folders in Java

You may also like-

  1. How to Create Password Protected Zip File in Java
  2. Print Odd-Even Numbers Using Threads And wait-notify Java Program
  3. Convert double to int in Java
  4. Java Program to Get All The Tables in a DB Schema
  5. Java Abstract Class and Abstract Method
  6. Java Collections Interview Questions And Answers
  7. Java Stream API Tutorial
  8. Interface Static Methods in Java

Friday, March 3, 2023

Java Program to Convert a File to Byte Array

There are times when we need to read file content into a byte array like when we need to send the file content over the network or we need to calculate check sum using file data. So in this post we'll see various ways to convert file to a byte array in Java.

Available options for conversion

  1. Using read method of the FileInputStream class. See example.
  2. Using Files.readAllBytes() method Java 7 onward. See example.
  3. Using IOUtils.toByteArray() method provided by Apache commons IO. See example.
  4. Using FileUtils.readFileToByteArray method provided by Apache commons IO. See example.

1. File to byte[] using read method of FileInputStream

You can use java.io.FileInputStream to read file content into a byte array using the read() method. General structure and description of read method as per Java docs is as given below.

public int read(byte[] b) throws IOException

Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.

public class FileToByteArrayDemo {
  public static void main(String[] args) {
    File file = new File("F:\\NetJS\\Articles.txt");
    // Using java.io.FileInputStream
    byte[] bArray = readFileToByteArray(file);
    //displaying content of byte array
    for (int i = 0; i < bArray.length; i++){
      System.out.print((char) bArray[i]);
    }  
  }
    
  /**
   * This method uses java.io.FileInputStream to read
   * file content into a byte array
   * @param file
   * @return
   */
  private static byte[] readFileToByteArray(File file){
    FileInputStream fis = null;
    // Creating a byte array using the length of the file
    // file.length returns long which is cast to int
    byte[] bArray = new byte[(int) file.length()];
    try{
      fis = new FileInputStream(file);
      fis.read(bArray);
      fis.close();                   
    }catch(IOException ioExp){
      ioExp.printStackTrace();
    }
    return bArray;
  }
}

2. File to byte array conversion using Files.readAllBytes()

Java 7 onward you can use static method readAllBytes(Path path) in the Files class for converting file to byte array.

public class FileToByteArrayDemo {
  public static void main(String[] args) {              
    Path path = Paths.get("F:\\NetJS\\Articles.txt");
    try {
      byte[] bArray = Files.readAllBytes(path);
      // reading content from byte array
      for (int i = 0; i < bArray.length; i++){
        System.out.print((char) bArray[i]);
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }       
  }
}

3. Using IOUtils.toByteArray() and FileUtils.readFileToByteArray() methods

Apache commons IO also provides utility methods to read file content into a byte array.

  • IOUtils.toByteArray- Takes FileInputStream object as param.
  • FileUtils.readFileToByteArray- Takes File object as param.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class FileToByteArrayDemo {
  public static void main(String[] args) {
    File file = new File("F:\\NetJS\\Articles.txt");        
    // Using ApacheCommons methods
    readToByteArrayUsingCommons(file);     
  }
    
  /**
   * This method uses apache commons to read
   * file content into a byte array
   * @param file
  */
  private static void readToByteArrayUsingCommons(File file){
    try(FileInputStream fis = new FileInputStream(file)) {
      // Using IOUtils method, it takes FileInputStream 
      // object as param
      byte[] bArray = IOUtils.toByteArray(fis);
      for (int i = 0; i < bArray.length; i++){
        System.out.print((char) bArray[i]);
      }
      // Using FileUtils method, it takes file object
      // as param
      bArray = FileUtils.readFileToByteArray(file);
      //displaying byte array content
      for (int i = 0; i < bArray.length; i++){
        System.out.print((char) bArray[i]);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }        
  }
}

Note that in the method readToByteArrayUsingCommons I have used try-with-resources which is available from Java 7. Closing the input stream will be done automatically by try-with-resources.

Refer try-with-resources in Java 7 to know more about try-with-resources.

That's all for this topic Java Program to Convert a File to Byte Array. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Reading File in Java Using Scanner
  2. Reading File in Java Using BufferedReader
  3. How to Read Input From Console in Java
  4. Write to a File in Java
  5. Unzip File in Java

You may also like-

  1. Convert String to Byte Array Java Program
  2. Count Number of Words in a String Java Program
  3. How to Remove Elements From an Array Java Program
  4. Java Program to Check Prime Number
  5. final Keyword in Java With Examples
  6. How to Loop Through a Map in Java
  7. Java ReentrantLock With Examples
  8. Dependency Injection in Spring Framework

Tuesday, February 28, 2023

Read or List All Files in a Folder in Java

In this post we'll see how to read or list all the files in a directory using Java. Suppose you have folder with files in it and there are sub-folders with files in those sub-folders and you want to read or list all the files in the folder recursively.

Here is a folder structure used in this post to read the files. Test, Test1 and Test2 are directories here and then you have files with in those directories.

Test
  abc.txt
  Test1
    test.txt
    test1.txt
  Test2
    xyz.txt

Java Example to read all the files in a folder recursively

There are two ways to list all the files in a folder; one is using the listFiles() method of the File class which is there in Java from 1.2.

Another way to list all the files in a folder is to use Files.walk() method which is a recent addition in Java 8.

 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Stream;

public class ListFiles {
  public static void main(String[] args) {
    File folder = new File("G:\\Test");
    ListFiles listFiles = new ListFiles();
    System.out.println("reading files before Java8 - Using listFiles() method");
    listFiles.listAllFiles(folder);
    System.out.println("-------------------------------------------------");
    System.out.println("reading files Java8 - Using Files.walk() method");
    listFiles.listAllFiles("G:\\Test");

  }
  // Uses listFiles method  
  public void listAllFiles(File folder){
    System.out.println("In listAllfiles(File) method");
    File[] fileNames = folder.listFiles();
    for(File file : fileNames){
      // if directory call the same method again
      if(file.isDirectory()){
         listAllFiles(file);
      }else{
        try {
          readContent(file);
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
  // Uses Files.walk method   
  public void listAllFiles(String path){
    System.out.println("In listAllfiles(String path) method");
    try(Stream<Path> paths = Files.walk(Paths.get(path))) {
      paths.forEach(filePath -> {
        if (Files.isRegularFile(filePath)) {
          try {
            readContent(filePath);
          } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      });
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   } 
  }
     
  public void readContent(File file) throws IOException{
    System.out.println("read file " + file.getCanonicalPath() );
    try(BufferedReader br  = new BufferedReader(new FileReader(file))){
      String strLine;
      // Read lines from the file, returns null when end of stream 
      // is reached
      while((strLine = br.readLine()) != null){
      System.out.println("Line is - " + strLine);
      }
    }
  }
     
  public void readContent(Path filePath) throws IOException{
    System.out.println("read file " + filePath);
    List<String> fileList = Files.readAllLines(filePath);
    System.out.println("" + fileList);
  }   
}

Output

reading files before Java8 - Using listFiles() method
In listAllfiles(File) method
read file G:\Test\abc.txt
Line is - This file is in Test folder.
In listAllfiles(File) method
read file G:\Test\Test1\test.txt
Line is - This file test is under Test1 folder.
read file G:\Test\Test1\test1.txt
Line is - This file test1 is under Test1 folder.
In listAllfiles(File) method
read file G:\Test\Test2\xyz.txt
Line is - This file xyz is under Test2 folder.
-------------------------------------------------
reading files Java8 - Using Files.walk() method
In listAllfiles(String path) method
read file G:\Test\abc.txt
[This file is in Test folder.]
read file G:\Test\Test1\test.txt
[This file test is under Test1 folder.]
read file G:\Test\Test1\test1.txt
[This file test1 is under Test1 folder.]
read file G:\Test\Test2\xyz.txt
[This file xyz is under Test2 folder.]

Here we have two overloaded listAllFiles() methods. First one takes File instance as argument and use that instance to read files using the File.listFiles() method. In that method while going through the list of files under a folder you check if the next element of the list is a file or a folder. If it is a folder then you recursively call the listAllFiles() method with that folder name. If it is a file you call the readContent() method to read the file using BufferedReader.

Another version of listAllFiles() method takes String as argument. In this method whole folder tree is traversed using the Files.walk() method. Here again you verify if it is a regular file then you call the readContent() method to read the file.

Note that readContent() method is also overloaded one takes File instance as argument and another Path instance as argument.

If you just want to list the files and sub-directories with in the directory then you can comment the readContent() method.

That's all for this topic Read or List All Files in a Folder in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Write to a File in Java
  2. How to Append to a File in Java
  3. How to Read File From The Last Line in Java
  4. How to Find Last Modified Date of a File in Java
  5. Unzip File in Java

You may also like-

  1. How to Create PDF From XML Using Apache FOP
  2. Print Odd-Even Numbers Using Threads And Semaphore Java Program
  3. How to Run Threads in Sequence in Java
  4. How to Display Pyramid Patterns in Java - Part1
  5. Add Double Quotes to a String Java Program
  6. Java Stream API Tutorial
  7. How HashMap Works Internally in Java
  8. Java ReentrantLock With Examples

Sunday, November 20, 2022

Java BufferedWriter Class With Examples

Java BufferedWriter class is used to write text to a character-output stream. This class is used as a wrapper around any Writer (FileWriter and OutputStreamWriter) whose write() operations may be costly. java.io.BufferedWriter makes the write operation more efficient by buffering characters so as to provide for the efficient writing of single characters, arrays, and strings. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.

Java BufferedWriter constructors

  • BufferedWriter(Writer out)- Wraps the passed Writer and creates a buffering character-input stream that uses a default-sized input buffer. Default buffer size for BufferedWriter is 8192 bytes i.e. 8 KB.

    For example-

     BufferedWriter out = new BufferedWriter(new FileWriter("resources/abc.txt"));
     
  • BufferedWriter(Writer out, int sz)- Wraps the passed Writer and creates a new buffered character-output stream that uses an output buffer of the given size.

Java BufferedWriter methods

Methods in BufferedWriter class are as given below-

  • flush()- Flushes the stream.
  • newLine()- Writes a line separator.
  • write(int c)- Writes a single character.
  • write(char[] cbuf, int off, int len)- Writes a portion of an array of characters. Parameter off is the offset from which to start reading characters and parameter len is the number of characters to write.
  • write(String s, int off, int len)- Writes a portion of a String. Parameter off is the offset from which to start reading characters and parameter len is the number of characters to write.

Java BufferedWriter examples

For using BufferedWriter steps are as follows-

  1. Create an instance of BufferedWriter wrapped around another Writer.
  2. Using that instance to write to a file.
  3. Close the stream, you should close the resources in finally block or you can use try-with-resources to automatically manage resources.

1. Using write(int c) method of the Java BufferedWriter class to write single character to a file.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriteRW {
  public static void main(String[] args) throws IOException {
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){
      bw.write(65); //writes A
      bw.write(66); //writes B
      bw.write(67); //writes C
    }
  }
}

2. Using write(char[] cbuf, int off, int len) method of the Java BufferedWriter class to write portion of a character array.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriteRW {
  public static void main(String[] args) throws IOException {
    String str = "This is a test String";
    char[] buffer = str.toCharArray();
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){
      bw.write(buffer, 0, 7);// takes portion from index 0..6
    }
  }
}

3. Using write(String s, int off, int len) of the Java BufferedWriter class to write a portion of a String.

public class FileWriteRW {
  public static void main(String[] args) throws IOException {
    String str = "This is a test String";
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){
      bw.write(str, 0, 7);// takes substring from index 0..6
    }
  }
}

4. Using newLine() method of the Java BufferedWriter class.

public class FileWriteRW {
  public static void main(String[] args) throws IOException {
    String str = "This is a test String";
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){
      bw.write(str, 0, 7);// takes substring from index 0..6
      bw.newLine(); // new line
      bw.write(str, 7, str.length()-7);
      bw.flush(); // flushes the stream
    }
  }
}

That's all for this topic Java BufferedWriter Class With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. Character Streams in Java IO
  2. Byte Streams in Java IO
  3. Write to a File in Java
  4. How to Append to a File in Java
  5. Reading File in Java Using Files.lines And Files.newBufferedReader

You may also like-

  1. How to Read File From The Last Line in Java
  2. Difference Between Thread And Process in Java
  3. LinkedHashSet in Java With Examples
  4. String Vs StringBuffer Vs StringBuilder in Java
  5. Constructor Overloading in Java
  6. Python Program to Display Fibonacci Series
  7. Spring MVC Exception Handling Example Using @ExceptionHandler And @ControllerAdvice
  8. Lazy Initialization in Spring Using lazy-init And @Lazy Annotation

Saturday, November 19, 2022

Creating Temporary File in Java

Sometimes you may want to create temporary file in Java to store some data for the application. Once the job is done temp file can safely be discarded or you can delete it on exit. In this post we'll see how to create a temporary file in Java and how to read and write to temporary file.

In java.io.File class there are two methods for creating a temp file.

  • static File createTempFile(String prefix, String suffix, File directory)- Creates a new empty file in the specified directory, using the passed prefix and suffix strings to generate file name.
  • static File createTempFile(String prefix, String suffix)- Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.

In java.nio.file.Files class also there are two methods for creating temporary files.

  • static Path createTempFile(Path dir, String prefix, String suffix, FileAttribute<?>... attrs)- Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. You can also pass an optional list of file attributes to set atomically when creating the file
  • static Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs)- Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.

Creating temporary file using java.io.File class methods

import java.io.File;
import java.io.IOException;

public class TempFile {

 public static void main(String[] args) {
  try {
   // Using default directory
   File tempFile = File.createTempFile("MyFile", ".temp");
   System.out.println("Temp file path- " + tempFile.getCanonicalPath());
   // Specifying directory
   File testFile = File.createTempFile("MyFile", ".temp", new File("F:\\Temp"));
   System.out.println("Temp file path- " + testFile.getCanonicalPath());
   
   tempFile.deleteOnExit();
   testFile.deleteOnExit();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 
 }
}

Output

Temp file path- C:\Users\netjs\AppData\Local\Temp\MyFile13999667283141733746.temp
Temp file path- F:\Temp\MyFile11668072031090823667.temp

Creating temporary file using java.nio.file.Files class methods

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class TempFile {

 public static void main(String[] args) {
  try {
   // Using default directory
   Path tempFile = Files.createTempFile("MyFile", ".temp");
   System.out.println("Temp file path- " + tempFile);
   // Specifying directory
   Path testFile = Files.createTempFile(Path.of("F:\\Temp"), "MyFile", ".temp");
   System.out.println("Temp file path- " + testFile);
   // Read write temp file operations
   
   // delete file on exit
   tempFile.toFile().deleteOnExit();
   testFile.toFile().deleteOnExit();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 
 }
}

Output

Temp file path- C:\Users\Anshu\AppData\Local\Temp\MyFile2647482374773079441.temp
Temp file path- F:\Temp\MyFile16822359990880283479.temp

Reading and writing temporary file in Java

Once the temporary file is created you will of course use it to write some data and read some data from the temp file. You can use BufferedWriter and BufferedReader for writing to and reading from a temp file.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TempFile {

 public static void main(String[] args) {
  try {
   // Specifying directory
   File testFile = File.createTempFile("MyFile", ".temp", new File("F:\\Temp"));
   System.out.println("Temp file path- " + testFile.getCanonicalPath());
   testFile.deleteOnExit();
   // Writing to temp file
   BufferedWriter bw = new BufferedWriter(new FileWriter(testFile));
   bw.write("Writing to the temp file");
   bw.close();
   // Reading from temp file
   String str;
   BufferedReader br = new BufferedReader(new FileReader(testFile));
   while((str = br.readLine()) != null){
    System.out.println("Line is - " + str);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 
 }
}

Output

Temp file path- F:\Temp\MyFile16468233438314323721.temp
Line is - Writing to the temp file

That's all for this topic Creating Temporary File in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. Buffered Streams in Java IO
  2. How to Create Password Protected Zip File in Java
  3. Compress And Decompress File Using GZIP Format in Java
  4. Reading File in Java Using Files.lines And Files.newBufferedReader
  5. How to Read File From The Last Line in Java

You may also like-

  1. LinkedBlockingDeque in Java
  2. Difference Between sleep And wait in Java Multi-Threading
  3. Functional Interfaces in Java
  4. Equality And Relational Operators in Java
  5. Type Wrapper Classes in Java
  6. Connection Pooling With Apache DBCP Spring Example
  7. Bean Definition Inheritance in Spring
  8. Input Splits in Hadoop

Saturday, September 17, 2022

How to Find Last Modified Date of a File in Java

There are two ways to get the last modified date of a file in Java.

  • Using File.lastModified() method- Using this method you can get the file's last modified timestamp.
  • Using Files.readAttributes() method- Java 7 onward you can use Files.readAttributes() method which returns an object of java.nio BasicFileAttributes that encapsulates all the attributes associated with the file. That way apart from last modified date you can also get the file creation date and several other attributes.

Java program to find the last modified date of a file

Following program uses both of the above mentioned methods to get the last modified date of a file in Java. Note here that when java.io.File's lastModified method is used it returns the time in milliseconds (long) so SimpleDateFormat is used to format it into dd/MM/yyyy format.

Files.readAttributes() method returns an instance of BasicFileAttributes. BasicFileAttributes class has methods creationTime() and lastModifiedTime() to get the file creation date and last modified date. Both of these methods return an instance of FileTime which is converted to milliseconds and then formatted to the desired format using SimpleDateFormat.

public class FileDate {
  public static void main(String[] args) {
    /*For below Java 7*/ 
    // get the file
    File f = new File("F:\\NetJS\\programs.txt");
    // Create a date format
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    // get the last modified date and format it to the defined format
    System.out.println("File last modified date " + sdf.format(f.lastModified()));
        
    /*Java 7 or above using NIO*/
    // Get the file
    Path path = Paths.get("F:\\NetJS\\programs.txt");
    BasicFileAttributes attr;
    try {
      // read file's attribute as a bulk operation
      attr = Files.readAttributes(path, BasicFileAttributes.class);
      // File creation time
      System.out.println("File creation time - " 
        + sdf.format(attr.creationTime().toMillis()));
      // File last modified date
      System.out.println("File modified time - " 
        + sdf.format(attr.lastModifiedTime().toMillis()));        
    } catch (IOException e ) {
        System.out.println("Error while reading file attributes " + e.getMessage());
    }       
  }
}

That's all for this topic How to Find Last Modified Date of a File in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. How to Format Date in Java Using SimpleDateFormat
  2. How to Count Lines in a File in Java
  3. How to Read File From The Last Line in Java
  4. How to Read Properties File in Java
  5. How to Untar a File in Java

You may also like-

  1. How to Run Threads in Sequence in Java
  2. Converting String to Enum Type in Java
  3. How to Run javap Programmatically From Java Program
  4. Count Number of Words in a String Java Program
  5. Java Pass by Value or Pass by Reference
  6. final Vs finally Vs finalize in Java
  7. How to Loop Through a Map in Java
  8. Dependency Injection in Spring Framework

Monday, September 12, 2022

Buffered Streams in Java IO

In our Java IO Stream classes tutorial we have already seen the Byte stream classes in Java and character stream classes in Java. But using these classes directly, without any buffering, results in very inefficient reading or writing of streams in turn making you program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive. To make IO operations more efficient the Java platform implements buffered I/O streams.

Buffered streams in Java

Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty.

Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.

Buffered stream wraps the unbuffered stream to convert it into a buffered stream. For that unbuffered stream object is passed to the constructor for a buffered stream class. For example wrapping FileReader and FileWriter to get BufferedReader and BufferedWriter.

BufferedReader br = new BufferedReader(new FileReader("abc.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("write.txt"));

Buffered streams classes in Java

There are four buffered stream classes-

BufferedInputStream and BufferedOutputStream are used to wrap unbuffered byte streams to create buffered byte streams.

BufferedReader and BufferedWriter are used to wrap unbuffered character streams to create buffered character streams.

Flushing buffered streams

Since the characters or bytes are stored in a buffer before writing to the file so flushing the buffer is required sometimes without waiting for it to fill. There is an explicit flush() method for doing that. In most buffered stream implementations even close() method internally calls flush() method before closing the stream. Note that the flush method is valid on any output stream, but has no effect unless the stream is buffered.

That's all for this topic Buffered Streams in Java IO. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. getPath(), getCanonicalPath() and getAbsolutePath() Methods in Java
  2. How to Count Lines in a File in Java
  3. Creating Temporary File in Java
  4. Reading Delimited File in Java Using Scanner
  5. How to Create PDF in Java Using OpenPDF

You may also like-

  1. Converting String to int in Java
  2. continue Statement in Java With Examples
  3. ResultSet Interface in Java-JDBC
  4. Method Reference in Java
  5. AutoBoxing And UnBoxing in Java
  6. Java Stream API Interview Questions And Answers
  7. Configuring DataSource in Spring Framework
  8. registerShutdownHook() Method in Spring Framework

Wednesday, September 7, 2022

How to Read Input From Console in Java

In this post three ways are given to read user input from keyboard (console) in Java-

  • First way uses the InputStreamReader wrapped in a BufferedReader.
  • Second way to read input from console uses the Scanner class from Java 5.
  • Third way uses the System.console which was introduced in Java 6.

Read input from console in Java using BufferedReader

public class ReadFromConsole {

 public static void main(String[] args) {
  // Using BufferedReader
  System.out.print("Please enter user name : ");   
  BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
  String s;
  try {   
   s = bufferRead.readLine();
   System.out.println("You entered- " + s);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }    
 }
}

Output

Please enter user name : netjs
You entered- netjs

In the program you can see that an InputStreamReader is wrapped with in a BufferedReader to read text from a character-input stream.

InputStreamReader wraps System.in where in is a field in a System class. in is the "standard" input stream. This stream is already open and ready to supply input data. Typically this stream corresponds to keyboard input or another input source specified by the host environment or user.

Read input from console using Scanner

Scanner class, added in Java 5 is another option to read input from console in Java. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. nextLine() method of the Scanner class advances this scanner past the current line and returns the input that was skipped.

Scanner class also has methods for different data types like nextInt(), nextDouble(), nextFloat() etc.

public class ReadFromConsole {
  public static void main(String[] args) {
    // Using Scanner 
    System.out.print("Please enter user name : ");
    Scanner scanIn = new Scanner(System.in);
    String scanLine = scanIn.nextLine();
    System.out.println("You entered- " + scanLine);
    System.out.println("Entered int value- " + scanIn.nextInt());
    System.out.println("Entered float value- " + scanIn.nextFloat());
    scanIn.close();      
  }
}

Output

Please enter user name : nets
You entered- nets
3 78.90
Entered int value- 3
Entered float value- 78.9

Read input from console using System.console

console method in System class returns the unique Console object associated with the current Java virtual machine, if any.

A word of caution, if running the code from eclipse, System.console() will throw null pointer exception.

Please follow this discussion to know more about this exception- http://stackoverflow.com/questions/104254/java-io-console-support-in-eclipse-ide
public class ReadFromConsole {
  public static void main(String[] args) {
    //Using System.console()
    String username = System.console().readLine("Please enter user name : ");   
    System.out.println("You entered : " + username);      
  }
}

Output

Please enter user name : netjs
You entered : netjs

That's all for this topic How to Read Input From Console in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. How to Read File From The Last Line in Java
  2. How to Find Last Modified Date of a File in Java
  3. How to Read Properties File in Java
  4. Read or List All Files in a Folder in Java
  5. Zipping Files And Folders in Java

You may also like-

  1. Count Number of Words in a String Java Program
  2. Check if Given String or Number is a Palindrome Java Program
  3. How to Find Common Elements Between Two Arrays Java Program
  4. Producer-Consumer Java Program Using ArrayBlockingQueue
  5. Difference Between Abstract Class And Interface in Java
  6. Fail-Fast Vs Fail-Safe Iterator in Java
  7. Difference Between throw And throws in Java
  8. Volatile Keyword in Java With Examples

Monday, August 29, 2022

Fix Scanner.nextLine() Skipping Input After Another next Methods

In this article we’ll see why do we have the issue of Scanner.nextLine() skipping the input if used just after any other next method of the java.util.Scanner class and how to fix this problem.

What is the problem

If you are here to look for a solution to this problem you might already have encountered a similar scenario. Here is a program where next() method of the Scanner class is used to get a word as input, then nextLine() is used, then nextInt() and again nextLine() is used.

import java.util.Scanner;

public class ScannerDemo {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a word: ");
		String s = sc.next();
		
		System.out.println("Enter name: ");
		String name = sc.nextLine();
		
		System.out.println("Enter a number: ");
		int i = sc.nextInt();
		
		System.out.println("Enter address: ");
		String addr = sc.nextLine();
		
		System.out.println("Entered word is- " + s);
		System.out.println("Entered name is- " + name);
		System.out.println("Entered number is- " + i);
		System.out.println("Entered address is- " + addr);
	}

}

Output

Enter a word: 
test
Enter name: 
Enter a number: 
1
Enter address: 
Entered word is- test
Entered name is- 
Entered number is- 1
Entered address is- 

As you can see both nextLine() methods are skipped here.

What is the reason of skipping

When you use any of the next method be it next(), nextInt() etc. The input is read till the content ends without reading the newline character which is there because of pressing "enter".

If there is a nextLine() method just after any of the other next methods of the Scanner class, it ends up reading just the newline character (‘\n’) left by the previous next method usage.

As per the documentation of the nextLine() method in Scanner.

Advances this scanner past the current line and returns the input that was skipped.

In the scenario of using it after any other next method it advances past the left over newline character and returns just that input. So it seems, as if nextLine() is skipped.

How to fix this issue

Simple solution is to use an extra nextLine() method in such scenario to consume the left over new line character ('\n') so that the following nextLine() can take the actual input from the user. If we do that to the program used as example previously.

public class ScannerDemo {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a word: ");
		String s = sc.next();
		// extra nextLine
		sc.nextLine();
		System.out.println("Enter name: ");
		String name = sc.nextLine();
		
		System.out.println("Enter a number: ");
		int i = sc.nextInt();
		
		sc.nextLine();
		System.out.println("Enter address: ");
		String addr = sc.nextLine();
		
		System.out.println("Entered word is- " + s);
		System.out.println("Entered name is- " + name);
		System.out.println("Entered number is- " + i);
		System.out.println("Entered address is- " + addr);
	}
}

Output

Enter a word: 
test
Enter name: 
Ramesh Jaiswal
Enter a number: 
10
Enter address: 
1 VIP Road, New Delhi
Entered word is- test
Entered name is- Ramesh Jaiswal
Entered number is- 10
Entered address is- 1 VIP Road, New Delhi

As you can see with the inclusion of two extra sc.nextLine() methods problem of Scanner.nextLine() method skipping the input is solved.

That's all for this topic Fix Scanner.nextLine() Skipping Input After Another next Methods. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. getPath(), getCanonicalPath() and getAbsolutePath() Methods in Java
  2. Buffered Streams in Java IO
  3. How to Read Input From Console in Java
  4. Read File Asynchronously Java Program
  5. Java Program to Delete File And Directory Recursively

You may also like-

  1. Difference Between StackOverflowError and OutOfMemoryError in Java
  2. How to Get The Inserted ID (Generated ID) in JDBC
  3. Java Concurrency Interview Questions And Answers
  4. Static Synchronization in Java Multi-Threading
  5. Spring MVC Generate Response as JSON Example
  6. registerShutdownHook() Method in Spring Framework
  7. Global Keyword in Python With Examples
  8. Angular One-Way Data Binding Using String Interpolation

Friday, August 26, 2022

How to Untar a File in Java

In this post we'll see a Java program showing how to untar a tar file. It has both the steps to first decompress a .tar.gz file and later untar it.

Using Apache Commons Compress

Apache Commons Compress library is used in the code for untarring a file. You can download it from here \– https://commons.apache.org/proper/commons-compress/download_compress.cgi.

Make sure to add commons-compress-xxx.jar in your application’s class path. I have used commons-compress-1.13 version.

Java example to untar a file

This Java program has two methods deCompressGZipFile() method is used to decompress a .tar.gz file to get a .tar file. Using unTarFile() method this .tar file is untarred.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;

public class UnTarDemo {
  public static void main(String[] args) {
    // Path to input file, which is a 
    // tar file compressed to create gzip file
    String INPUT_FILE = "G:\\Test.tar.gz";
    // This folder should exist, that's where
    // .tar file will go
    String TAR_FOLDER = "G:\\TarFile";
    // After untar files will go to this folder
    String DESTINATION_FOLDER = "G:\\Temp";
    UnTarDemo unTarDemo = new UnTarDemo();
    try {
      File inputFile = new File(INPUT_FILE);
      String outputFile = getFileName(inputFile, TAR_FOLDER);
      System.out.println("outputFile " + outputFile);
      File tarFile = new File(outputFile);
      // Calling method to decompress file
      tarFile = unTarDemo.deCompressGZipFile(inputFile, tarFile);
      File destFile = new File(DESTINATION_FOLDER);
      if(!destFile.exists()){
        destFile.mkdir();
      }
      // Calling method to untar file
      unTarDemo.unTarFile(tarFile, destFile);            
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
    
  /**
   * 
   * @param tarFile
   * @param destFile
   * @throws IOException
   */
  private void unTarFile(File tarFile, File destFile) throws IOException{
    FileInputStream fis = new FileInputStream(tarFile);
    TarArchiveInputStream tis = new TarArchiveInputStream(fis);
    TarArchiveEntry tarEntry = null;
        
    // tarIn is a TarArchiveInputStream
    while ((tarEntry = tis.getNextTarEntry()) != null) {
      File outputFile = new File(destFile + File.separator + tarEntry.getName());        
      if(tarEntry.isDirectory()){            
        System.out.println("outputFile Directory ---- " 
            + outputFile.getAbsolutePath());
        if(!outputFile.exists()){
          outputFile.mkdirs();
        }
      }else{
        //File outputFile = new File(destFile + File.separator + tarEntry.getName());
        System.out.println("outputFile File ---- " + outputFile.getAbsolutePath());
        outputFile.getParentFile().mkdirs();
        //outputFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(outputFile); 
        IOUtils.copy(tis, fos);
        fos.close();
      }
    }
    tis.close();
  }
    
  /**
   * Method to decompress a gzip file
   * @param gZippedFile
   * @param newFile
   * @throws IOException
   */
  private File deCompressGZipFile(File gZippedFile, File tarFile) throws IOException{
    FileInputStream fis = new FileInputStream(gZippedFile);
    GZIPInputStream gZIPInputStream = new GZIPInputStream(fis);
    
    FileOutputStream fos = new FileOutputStream(tarFile);
    byte[] buffer = new byte[1024];
    int len;
    while((len = gZIPInputStream.read(buffer)) > 0){
      fos.write(buffer, 0, len);
    }        
    fos.close();
    gZIPInputStream.close();
    return tarFile;               
  }
    
  /**
   * This method is used to get the tar file name from the gz file
   * by removing the .gz part from the input file
   * @param inputFile
   * @param outputFolder
   * @return
   */
  private static String getFileName(File inputFile, String outputFolder){
    return outputFolder + File.separator + 
      inputFile.getName().substring(0, inputFile.getName().lastIndexOf('.'));
  }
}

That's all for this topic How to Untar a File in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Creating Tar File And GZipping Multiple Files in Java
  2. Zipping Files And Folders in Java
  3. How to Create Password Protected Zip File in Java
  4. Compressing and Decompressing File in GZIP Format
  5. How to Read File From The Last Line in Java

You may also like-

  1. Reading File in Java Using Files.lines And Files.newBufferedReader
  2. Write to a File in Java
  3. Producer-Consumer Java Program Using volatile
  4. How to Inject Prototype Scoped Bean into a Singleton Bean in Spring
  5. Spring NamedParameterJdbcTemplate Insert, Update And Delete Example
  6. Interface Default Methods in Java
  7. Lambda Expressions in Java 8
  8. StringBuffer Class in Java With Examples