Saturday, November 1, 2014

Java read byte array



Read byte array from file using Data Input Stream

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadByteArrayFromFile
{
public static void main(String[] args)
{
String strFilePath = "C://FileIO//readByteArray.txt";
try
{
FileInputStream fin = new FileInputStream(strFilePath);    DataInputStream din = new DataInputStream(fin);
byte b[] = new byte[10];
din.read(b);
din.close();
}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
   }
 }
}

Read byte from file using Data Input Stream

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadByteFromFile
{
public static void main(String[] args)
{
 String strFilePath = "C://FileIO//readByte.txt";
 try
{
FileInputStream fin = new FileInputStream(strFilePath);  DataInputStream din = new DataInputStream(fin);  byte b
= din.readByte();
System.out.println("byte : " + b);
din.close();
}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
    }
  }
}

Read char from file using Data Input Stream

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;  
public class ReadCharFromFile
{

public static void main(String[] args)
{
String strFilePath = "C://FileIO//readChar.txt";
try
{
FileInputStream fin = new FileInputStream(strFilePath);
DataInputStream din = new DataInputStream(fin);
 char ch = din.readChar();
 System.out.println("Char : " + ch);
 din.close();
}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
    }
  }
}

Read double from file using Data Input Stream

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadDoubleFromFile
{
public static void main(String[] args)
{
String strFilePath = "C://FileIO//readDouble.txt";
try
FileInputStream fin = new FileInputStream(strFilePath);DataInputStream din = new DataInputStream(fin);
double d = din.readDouble();
System.out.println("Double : " + d);
din.close();    
}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
    }
  }
}

Read float from file using Data Input Stream

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadFloatFromFile
{
public static void main(String[] args)
{
String strFilePath = "C://FileIO//readFloat.txt";
try
FileInputStream fin = new FileInputStream(strFilePath); DataInputStream din = new DataInputStream(fin);
float f = din.readFloat();
System.out.println("float : " + f);
din.close();
}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
     }
   }
}

No comments:

Post a Comment