read and skip number of bytes in java using FileInputStream
output
import java.io.*;
class inputstrm
{
public static void main(String [] args) throws Exception
{
int size;
FileInputStream inputstrm = new FileInputStream("inputstrm.java");
//to get size in bytes
System.out.println("available bytes" +(size = inputstrm.available()));
//to read first 50 bytes
System.out.println("reading 50 bytes");
byte bytearray[] = new byte[50];
if(inputstrm.read(bytearray) !=50)
{
System.out.println("could not get 50 bytes");
}
System.out.println(new String(bytearray,0,50));
//skip 50 bytes
System.out.println("skipped 50 bytes");
inputstrm.skip(50);
//read after skipping 50 bytes
System.out.println("reading 50 bytes");
if(inputstrm.read(bytearray) !=50)
{
System.out.println("could not get 50 byte");
}
System.out.println(new String(bytearray,0,50));
//close file
inputstrm.close();
}
}
output
0 comments :
Post a Comment