public static void main(String[]args) throws IOException{ String path = "E:/圖片/111.jpg"; //圖片地址 File file = new File(path); //將圖片轉換成file類型的文件 FileInputStream fis = new FileInputStream(file); //創建輸入流 byte[] byt = new byte[fis.available()]; //字節數據 available() 返回的實際可讀字節數,也就是總大小 StringBuilder str = new StringBuilder();// 不建議用String fis.read(byt); // 從輸入流中讀取一定數量的字節,並將其存儲在緩沖區數組 b 中。以整數形式返回實際讀取的字節數。 for (byte bs : byt) { // 將字節數組byte[] 轉換為二進制,在以字符串的形式顯示出來 //Integer.toBinaryString(bs)返回無符號整數的二進制的字符串表示形式 str.append(Integer.toBinaryString(bs)); } str.toString(); System.out.println("輸出二進制字符串+==="+str); // 把字節數組的圖片寫到另一個地方 File apple = new File("D:/apple.jpg"); FileOutputStream fos = new FileOutputStream(apple); fos.write(byt); fos.flush(); fos.close(); }