我ByteArray
在一个文件中写了一个。然后我试图ByteArray
从同一个文件中读回它..
public static void main(String[] args) throws Exception {
//.. some code
writeFile(allWrittenBytesTest);
readFile();
}
/**
* Write the file in Java
* @param byteArray
*/
public static void writeFile(byte[] byteArray) {
try{
File file = new File("bytearrayfile");
boolean success = file.delete();
if(!success) {
System.out.println("not able to delete the file");
}
FileOutputStream output = new FileOutputStream(file);
IOUtils.write(byteArray, output);
} catch (Exception ex) {
ex.printStackTrace();
}
现在,我无法理解如何从同一个文件中读取 ByteArray?下面是我的 readFile 方法-
public static void readFile() {
BufferedReader reader = null;
try {
File file = new File("bytearrayfile");
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
// this doesn't work I know but not sure how to read that?
DataInputStream inTest = new DataInputStream(new ByteArrayInputStream(line));
// some other code to deserialize that ByteArray
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}