简体   繁体   English

如何从Java的字节数组读取数据

[英]how to read data from byte array from java

hi all i convert a mp3 file into byte array and i read from byte array but it shows null pointer exception on line number 15 my code: 大家好,我将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();

    } 

} } }}

static byte[] bytesarray = new byte[32]; should do the work, you didn't initialize your array... 应该做的工作,您没有初始化数组...

See the documentation of read . 请参阅read文档

static byte[] bytesarray = new byte[32];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM