如何从java的字节数组中读取数据

时间:2011-05-07 12:38:45

标签: java mp3 bytearray

大家好 我将一个mp3文件转换为字节数组,我从字节数组中读取,但它在第15行显示空指针异常 我的代码:

public class MainClass {
 static byte[] bytesarray = null;
  public static void main(String args[]){
 try {

    FileInputStream fis=new FileInputStream("D:\\taxi.mp3");
    try {
        fis.read(bytesarray,0,32);
        System.out.println(bytesarray.length);
    } catch (IOException e) {
        e.printStackTrace();
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}



ByteArrayInputStream in = new ByteArrayInputStream(bytesarray);     
for (int i=0; i<32; i++) {
    int c;
    while ((c = in.read()) != -1) {
    if (i == 0) {
    System.out.print((char) c);
    } else {
    System.out.print(Character.toUpperCase((char) c));
    }
    }
    System.out.println();

    } 

} }

2 个答案:

答案 0 :(得分:3)

static byte[] bytesarray = new byte[32];应该做的工作,你没有初始化你的数组...

请参阅documentation of read

答案 1 :(得分:1)

static byte[] bytesarray = new byte[32];
相关问题