Question: I need to convert 2 byte array ( byte[2] ) to integer value in java. How can I do that?

Answer: 
Well, each byte is an integer in the range -128..127, so you need a way to map a pair of integers to a single integer. There are many ways of doing that, depending on what you have encoded in the pair of bytes. The most common will be storing a 16-bit signed integer as a pair of bytes. Converting that back to an integer depends on whether you store it big-endian form:

```
(byte_array[0]<<8) + (byte_array[1] & 0xff)

```

or little endian:

```
(byte_array[1]<<8) + (byte_array[0] & 0xff)

```
```
import java.io.*;
public class ByteArray {

    public static void main(String[] args) throws IOException {
        File f=new File("c:/users/sample.txt");
        byte[]b={1,2,3,4,5};
        ByteArrayInputStream is=new ByteArrayInputStream(b);
        int i;
        while((i=is.read())!=-1) {
            System.out.println((int)i); 
            FileOutputStream f1=new FileOutputStream(f);
            FileOutputStream f2=new FileOutputStream(f);
            ByteArrayOutputStream b1=new ByteArrayOutputStream();
            b1.write(6545);
            b1.writeTo(f1);
            b1.writeTo(f2);
            b1.close();
        }

```