Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有一种方法可以在不使用ObjectInputStream的情况下将FileInputStream转换为对象?_Java_Binaryfiles - Fatal编程技术网

Java 有没有一种方法可以在不使用ObjectInputStream的情况下将FileInputStream转换为对象?

Java 有没有一种方法可以在不使用ObjectInputStream的情况下将FileInputStream转换为对象?,java,binaryfiles,Java,Binaryfiles,我想知道不使用ObjectInputStream是否可以从FileInputStream获取对象 static class MyObject implements Serializable{ int i; } public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { MyObject obj = new MyObject()

我想知道不使用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);
        }
    }
}
我为什么要这样做?我最近一直在做一个项目,它从游戏中读取.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;
        }
    }
}

您只需去掉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);
        }
    }
}

您只需去掉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);
        }
    }
}

我只想确定你写的数据和我写的一样。在dat文件所需的头之后使用对象输出流。无论您做什么,写入都需要与读取相匹配。如果你想让我匹配的话,就把代码写出来。好的。我已经设法让它工作了。处理大部分内容的代码库有一个用于处理.DAT的文档(与从代码中导出它的代码相同,即.DAT),因此我能够相应地修改代码。除此之外,你的回答确实有效。谢谢我只想确定你写的数据和我写的一样。在dat文件所需的头之后使用对象输出流。无论您做什么,写入都需要与读取相匹配。如果你想让我匹配的话,就把代码写出来。好的。我已经设法让它工作了。处理大部分内容的代码库有一个用于处理.DAT的文档(与从代码中导出它的代码相同,即.DAT),因此我能够相应地修改代码。除此之外,你的回答确实有效。谢谢