1

ByteArrayファイルに aを書きました。そして、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();
    }
}
4

3 に答える 3