有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java有没有一种不用ObjectInputStream就可以将FileInputStream转换为对象的方法?

我想知道不使用ObjectInputStream是否可以从FileInputStream获取对象

我为什么要这样做?我最近在做一个项目,上面写着。DAT文件来自游戏并将其转换为。OBJ-这些有一个陷阱。但是,DAT文件:它们的流头始终为0xFACEAF0E。有没有办法绕过ObjectInputStream对流头的限制,从这些文件中获取对象

这是我需要帮助的代码

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;
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    您只需去掉ObjectInputStream在使用FileInputStream时不应首先看到的额外字节

    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);
            }
        }
    }