FileInputStream is;
FileOutputStream out;
byte[] data = new byte[1024];
int readBytes = 0;
try {
is = new FileInputStream(filePath);
out = new FileOutputStream("C:/Users/gopir/Desktop/me1Copy.jpeg");
while ((readBytes = is.read(data)) > 0) {
out.write(data,0,readBytes);
}
out.flush();
out.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我只是想将图像转换为二进制,二进制转换为图像。图像到二进制转换工作。为了检查完整性,我只是尝试将二进制数据转换为图像。它创建空图像文件。这段代码有什么问题?
还有其他办法吗?我对使用apache common等外部jar不感兴趣。
修改
这就是我创建二进制文件的方式
File imageFile = new File(pathName); // Any file Path
BufferedInputStream bis = null; // To read the given file
BufferedWriter bw = null; //To write the binary equivalent of the file contents
String outputFile = "C:\\Users\\gopir\\Desktop\\file1.txt"; // output file name
try {
bw = new BufferedWriter(new FileWriter(outputFile)); //Output writer stream
bis = new BufferedInputStream(new FileInputStream(imageFile)); // Input file reader stream
int read;
String text;
while((read = bis.read()) != -1){ // read
text = Integer.toString(read,2); // convert to string
while (text.length() < 8) { // if length less than 8, prepend 0's
text="0"+text;
}
bw.write(text); //write it to a file
}
bis.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}