java - 有没有办法可以在不使用 ObjectInputStream 的情况下将 FileInputStream 转换为对象?

标签 java binaryfiles

我想知道是否可以在不使用 ObjectInputStream 的情况下从 FileInputStream 获取对象。

我为什么要这么做?我最近一直在开发一个项目,它从游戏中读取 .DAT 文件并将其转换为 .OBJ - 然而这些 .DAT 文件有一个问题:它们的流 header 始终是 0xFACEAF0E。有没有办法绕过 ObjectInputStream 对流 header 的限制并从这些文件之一获取对象?

这是我需要帮助的代码。

package xan_code;

/*
 * Purpose: Determine file extension and run it through the code to move it to an OBJ.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

import main.BeginConversion; //Import the conversion code. Returns a string based on the file
import xan_code.dathandler.ReadBinary; //Get the .DAT reading functions

@SuppressWarnings("serial")
public class HandleFiles extends Main { //Extend main to get the log from the opener UI.
    static BeginConversion converter = new BeginConversion(); //Get the converter for XML files since this will also read XMLs derived from the .DAT
    static String DatText = ""; //The "text" to return for the .DAT (To pack into the decoded file)
    static Object _object; //THIS IS THE VARIABLE OF THE OBJECT I NEED IN ORDER TO CONVERT THE .DAT TO A MODEL
    @SuppressWarnings("static-access")
    public static String convert(File file, boolean isXML, FileInputStream FIS) { //Convert. Passes in the .DAT or .XML file, a boolean to whether or not its extension is .XML, and the FileInputStream from file
        if (isXML) { //If it's an XML
            String xml = ""; //This is the text to store the XML as a string
            String obj = ""; //This is the text to store the WaveFront OBJ (Model format) as a string
            try {
                xml = new Scanner(file).useDelimiter("\\Z").next(); //use the scanner to get the string of the XML
                obj = converter.BeginConvert(xml); //Pass the XML into the java files required to read from the XML and convert it to an OBJ. They return the text from an OBJ file.
            } catch (Exception e) {
                //Exceptions are handled before, though to be safe...
                e.printStackTrace();
            }
            return obj; //Return that text to Main so I can create the file.
        } else { //We have a .DAT
            try {
                //HELP REQUIRED HERE. NEED TO GET _object FROM THE FILE WITHOUT USING ObjectInputStream
                DatText = ReadBinary.Read(file, _object); //Right now this actually returns the text of an XML, but that doesn't matter much at the moment.
            } catch (IOException e) {
                DatText = "Unexpected error while reading .DAT file!";
                e.printStackTrace();
            }
            return DatText;
        }
    }
}

最佳答案

您可以使用 FileInputStream 去除 ObjectInputStream 不应该首先看到的额外字节。

static class MyObject implements Serializable{
    int i;
}

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    MyObject obj = new MyObject();
    obj.i = 77;
    File testFile = new File("test.dat");
    try (FileOutputStream fos = new FileOutputStream(testFile)) {
        fos.write(new byte[]{(byte) 0xFA, (byte) 0xCE, (byte) 0xAF, (byte) 0x0E});
        try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(obj);
        }
    }
    try (FileInputStream fis = new FileInputStream(testFile)) {
        byte b4[] = new byte[4];
        fis.read(b4);
        try (ObjectInputStream ois = new ObjectInputStream(fis)) {
           MyObject newObj = (MyObject) ois.readObject();
            System.out.println("newObj.i = " + newObj.i);
        }
    }
}

关于java - 有没有办法可以在不使用 ObjectInputStream 的情况下将 FileInputStream 转换为对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33247204/

相关文章:

java - python cli 中的 java BCryptPasswordEncoder() 等效于什么?

java - 从 Android 设备发出发布请求时出现意外的状态行

java - Tomcat 8 启动多次 [LifecycleException]

java - 在 Java 中将一个列表映射到另一个列表的最优雅的方法是什么?

java - 我需要有关二进制文件的帮助

c - 从二进制文件更新记录(项目的一部分)(段错误)

java - Spring boot schema.sql 不适用于 mysqldump 文件

.net - 是否有一种经过验证的方法可以在 .NET 中非常快速地访问非常大(> 1 GB)二进制文件的特定区域

C++用fstream替换二进制文件中的数据

c - 是否可以使用霍夫曼编码来压缩二进制文件?