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

Monday, 22 April 2013

Convert InputStream to byte array in Java

Leave a Comment

Here is complete code example of reading InputStream as byte array in Java. This Java program has two methods, one uses Apache commons IOUtils library to convert InputStream as byte array, while other uses core Java class methods. If you look at Apache commons code, it's just a one liner and it's tested for various kind of input e.g. text file, binary file, images, and both large and small files.

 By writing your own method for common utilities, which is good in sense of ownership; It's difficult to get same kind of testing exposure. That's the reason I prefer to use open source libraries, like Apache commons and Google Guava, along with JDK. They effectively complement standard Java library, and with Maven, it’s pretty easy to manage dependency.

In this example, we are reading a small text file using FileInputStream in Java.

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import org.apache.commons.io.IOUtils; 
public class InputStreamToByteArray { 
   public static void main(String args[]) throws FileNotFoundException, IOException 
   { //Converting InputStream to byte array using apche commons IO library 
     int length = toByteArrayUsingCommons(new FileInputStream("C:/temp/abc.txt")).length; 
  System.out.println("Length of byte array created from InputStream in Java using IOUtils : " + length); 
  
  //Converting InputStream to Byte arrray using Java code 
  length = toByteArrayUsingJava(new FileInputStream("C:/temp/abc.txt")).length; 
  System.out.println("Length of Byte array created from FileInputStream in Java : " + length); 
   } 
   /* * Converts InputStream to ByteArray in Java using Apache commons IOUtils class */ 
   
   public static byte[] toByteArrayUsingCommons(InputStream is) throws IOException{ 
     return IOUtils.toByteArray(is); 
  } 
  
  /* * Read bytes from inputStream and writes to OutputStream, later converts * OutputStream to byte array in Java. */ 
  public static byte[] toByteArrayUsingJava(InputStream is) throws IOException{
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    int reads = is.read(); 
    while(reads != -1)
    { 
      baos.write(reads); 
   reads = is.read(); 
    }
    return baos.toByteArray(); 
    } 
}

 Output: 
 Length of byte array created from InputStream in Java using IOUtils : 27
 Length of Byte array created from FileInputStream in Java : 27 


Read More...

Monday, 11 March 2013

read File in Java using BufferedReader, Scanner, Files with Encoding support and FileReader

Leave a Comment

While working with files in Java, we need to read them. Earlier, I wrote about java property files and here I am providing different ways through which we can read a file in java.
java.nio.file.Files: If you want to read all the contents of a file into byte array or read all lines to a list, we can use Files class. Files class is introduced in Java 7 and it’s good if you want to load all the file contents. You should use this method only when you are working on small files and you need all the file contents in memory.
java.io.FileReader: You can use FileReader to get the BufferedReader and then read files line by line. FileReader doesn’t support encoding and works with the system default encoding, so it’s not very efficient way of reading file in java.
java.io.BufferedReader: BufferedReader is good if you want to read file line by line and process on them. It’s good for processing large file and it supports encoding also. BufferedReader is synchronized, so read operations on a BufferedReader can safely be done from multiple threads. BufferedReader default buffer size is 8KB.
java.util.Scanner: If you want to read file line by line or based on some java regular expression, Scanner is the class to use. Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods. Scanner is not synchronized and hence not thread safe.
Here is the example class showing how we can read file in java using Scanner, Files, BufferedReader with Encoding support and FileReader.
package com.harit;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
 
public class JavaReadFile {
 
    public static void main(String[] args) throws IOException {
        String fileName = "/Users/pankaj/source.txt";
         
        //using Java 7 Files class to process small files, get complete file data
        readUsingFiles(fileName);
         
        //using Scanner class for large files, to read line by line
        readUsingScanner(fileName);
         
        //read using BufferedReader, to read line by line
        readUsingBufferedReader(fileName);
        readUsingBufferedReaderJava7(fileName, StandardCharsets.UTF_8);
        readUsingBufferedReader(fileName, StandardCharsets.UTF_8);
         
        //read using FileReader, no encoding support, not efficient
        readUsingFileReader(fileName);
    }
 
    private static void readUsingFileReader(String fileName) throws IOException {
        File file = new File(fileName);
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line;
        while((line = br.readLine()) != null){
            //process the line
            System.out.println(line);
        }
        br.close();
        fr.close();
         
    }
 
    private static void readUsingBufferedReader(String fileName, Charset cs) throws IOException {
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis, cs);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while((line = br.readLine()) != null){
            //process the line
            System.out.println(line);
        }
        br.close();
         
    }
 
    private static void readUsingBufferedReaderJava7(String fileName, Charset cs) throws IOException {
        Path path = Paths.get(fileName);
        BufferedReader br = Files.newBufferedReader(path, cs);
        String line;
        while((line = br.readLine()) != null){
            //process the line
            System.out.println(line);
        }
        br.close();
    }
 
    private static void readUsingBufferedReader(String fileName) throws IOException {
        File file = new File(fileName);
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line;
        while((line = br.readLine()) != null){
            //process the line
            System.out.println(line);
        }
        //close resources
        br.close();
        fr.close();
    }
 
    private static void readUsingScanner(String fileName) throws IOException {
        Path path = Paths.get(fileName);
        Scanner scanner = new Scanner(path);
        //read line by line
        while(scanner.hasNextLine()){
            //process each line
            String line = scanner.nextLine();
        }
    }
 
    private static void readUsingFiles(String fileName) throws IOException {
        Path path = Paths.get(fileName);
        //read file to byte array
        byte[] bytes = Files.readAllBytes(path);
        //read file to String list
        List allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
    }
 
}
Read More...

File permission in java

Leave a Comment
In Java, file permissions are very OS specific: *nix , NTFS (windows) and FAT/FAT32, all have different kind of file permissions. Java comes with some generic file permission to deal with it.  Check if the file permission allow : 

file.canExecute(); – return true, file is executable; false isnot. 

file.canWrite(); – return true, file is writable; false is not. 

file.canRead(); – return true, file is readable; false is not. 

Set the file permission : file.setExecutable(boolean); – true, allow execute operations; false to disallow it. 

file.setReadable(boolean); – true, allow read operations; false to disallow it. file.setWritable(boolean); – true, allow write operations; false to disallow it. 

In *nix system, you may need to configure more specifies about file permission, e.g set a 777 permission for a file or directory, however, Java IO classes do not have ready method for it, but you can use the following dirty workaround :

Runtime.getRuntime().exec("chmod 777 file");




import java.io.File;
import java.io.IOException;
 
public class FilePermissionExample 
{
    public static void main( String[] args )
    {	
    	try {
 
	      File file = new File("D://shellscript.sh");
 
	      if(file.exists()){
	    	  System.out.println("Is Execute allow : " + file.canExecute());
		  System.out.println("Is Write allow : " + file.canWrite());
		  System.out.println("Is Read allow : " + file.canRead());
	      }
 
	      file.setExecutable(false);
	      file.setReadable(false);
	      file.setWritable(false);
 
	      System.out.println("Is Execute allow : " + file.canExecute());
	      System.out.println("Is Write allow : " + file.canWrite());
	      System.out.println("Is Read allow : " + file.canRead());
 
	      if (file.createNewFile()){
	        System.out.println("File is created!");
	      }else{
	        System.out.println("File already exists.");
	      }
 
    	} catch (IOException e) {
	      e.printStackTrace();
	    }
    }
}


Read More...

Create a file

Leave a Comment
String filename = “test.txt”;
String workingDir = System.getProperty(“user.dir”);
File file = new File(workingDir, filename);

Read More...

Create File Path In Java

Leave a Comment
package harit;

import java.io.File;
import java.io.IOException;
 
public class FilePath
{
    public static void main( String[] args )
    {	
    	try {
 
    	  String filename = "log.txt";
    	  String finalfile = "";
    	  String workingDir = System.getProperty("user.dir");
 
    	  finalfile = workingDir + File.separator + filename;
 
    	  System.out.println("Final filepath : " + finalfile);
    	  File file = new File(finalfile);
 
	  if (file.createNewFile()){
	     System.out.println("Done");
	  }else{
	     System.out.println("File already exists!");
	  }
 
    	} catch (IOException e) {
	      e.printStackTrace();
	}
    }
}

Read More...

Create a file in java

Leave a Comment
package harit;

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

public class CreateFile {

	public static void main(String args[]) throws IOException
	{
		File file = new File("D:\\logg.txt");
		if(file.createNewFile())
		{
			System.out.println("Create New File");
		}
		else
		{
			System.out.println("File Already Exist");
		}
	}
}


Read More...