1、读取媒体文件,将字节转换为0~255的字符存储到文本文件中
@Test
public void saveImg() throws IOException {
FileInputStream fileInputStream = new FileInputStream("1.webp");
FileWriter fileWriter = new FileWriter("1.txt");
int i = -1;
while ((i = fileInputStream.read()) != -1){
String b = i+"\r\n";//便于下面按行读取:将数字转换为字符串,便于下面将字符串转换为数字
fileWriter.write(b);
}
fileInputStream.close();
fileWriter.close();
}
2、读取文本文件,还原媒体文件
public void getImg() throws IOException{
FileReader fileReader = new FileReader("1.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
FileOutputStream fileOutputStream = new FileOutputStream("2.webp");
String str;
while ((str = bufferedReader.readLine()) != null){
byte c = (byte) Integer.parseInt(str);
fileOutputStream.write(c);
}
fileReader.close();
fileOutputStream.close();
}