2011-05-07 27 views
0
讀取的字節數組數據

大家好 我轉換的MP3文件轉換成字節數組,我從字節數組讀,但它顯示的行數空指針異常15 我的代碼:如何從Java

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(); 

    } 

}}

回答

3

static byte[] bytesarray = new byte[32];應該做的工作,你沒有初始化數組...

documentation of read

+0

感謝它與給予大小。 – 2011-05-07 12:42:52

+0

如果你沒有像這樣將數組硬編碼爲null:「static byte [] bytesarray = null;」,那麼你實際上可能會發出警告,說你試圖訪問一個未初始化的變量。由於這個原因,Java中的最佳實踐不是將您的值初始化爲null,false等。與C中實際推薦的不同點在於...... – 2013-02-08 13:16:37

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