比较文件的大小和该文件的字节数组的大小
Compare size of file and size of byte array of that file
当我们转换文件时(比如 - 图片)
对于byteArray,如何大概比较它们(图像文件和byteArray)?
是否还有一种方法可以首先将图像文件转换为 byteArray,并且此 byteArray 必须写入文本文件中..
在此之后再次从文本文件中读取这些 byteArray 行并制作 byteArray 并因此转换为图像文件...... 我是初学者,只是为了知识目的我想知道....
您可以像这样将图像转换为字节数组。
BufferedImage bImage = ImageIO.read(new File("sample.jpg"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", bos );
byte [] data = bos.toByteArray();
是的,你可以直接比较 2 byteArray。
只需做类似
的事情
if (bytearray1.length == bytearray2.length)
//its same
尝试将 ByteArray 写入文本文件
public void writeToFile(byte[] array)
{
try
{
String path = "/data/data/YOURFILE.txt"; //provide your path here
File file = new File(path);
if (!file.exists()) { //just to check if file is actually present
file.createNewFile();
}
FileOutputStream stream = new FileOutputStream(path);
stream.write(array);
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
}
当我们转换文件时(比如 - 图片) 对于byteArray,如何大概比较它们(图像文件和byteArray)?
是否还有一种方法可以首先将图像文件转换为 byteArray,并且此 byteArray 必须写入文本文件中.. 在此之后再次从文本文件中读取这些 byteArray 行并制作 byteArray 并因此转换为图像文件...... 我是初学者,只是为了知识目的我想知道....
您可以像这样将图像转换为字节数组。
BufferedImage bImage = ImageIO.read(new File("sample.jpg"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", bos );
byte [] data = bos.toByteArray();
是的,你可以直接比较 2 byteArray。
只需做类似
的事情if (bytearray1.length == bytearray2.length)
//its same
尝试将 ByteArray 写入文本文件
public void writeToFile(byte[] array)
{
try
{
String path = "/data/data/YOURFILE.txt"; //provide your path here
File file = new File(path);
if (!file.exists()) { //just to check if file is actually present
file.createNewFile();
}
FileOutputStream stream = new FileOutputStream(path);
stream.write(array);
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
}