简体   繁体   中英

When I create a file through code I can't see it in eclipse

So I'm making a game engine in java, it has two parts: the editor and the game engine itself. In the editor I serialize Scene objects and write them into.scene files for the engine to read, but the file doesn't show up in eclipse, I can see it in the file explorer so it is created. for a while I didn't pay attention because I can still read the file using the File class. But then I tried exporting it into a runnable jar file and the build didn't work, so I tried using getClass().getResource("path") and I ran into issues, as I understand the file is not being added to the build path for some weird reason even thou it's in the src folder, can I add the file to the build path programmatically or do I need to do something else?

Here is some code:

public File createSceneFile(String path) {
    try {
        File file = new File(path);
        
        if(file.createNewFile()) {
            FileOutputStream fileStream = new FileOutputStream(path);
            ObjectOutputStream os = new ObjectOutputStream(fileStream);
            os.writeObject(scene);
            os.close();
        }
            
        return file;
    }catch(IOException e) {
        e.printStackTrace();
        return null;
    }
}
    
public File updateScene(String path) {
    try {
        FileOutputStream fileStream = new FileOutputStream(path);
        ObjectOutputStream os = new ObjectOutputStream(fileStream);
        os.writeObject(scene);
        os.close();
        
        return new File(path);
    }catch(IOException e) {
        e.printStackTrace();
        return null;
    }
}

Code to read the.scene files:

public Scene getScene(URL url) {
    try {
        InputStream inStream = url.openStream();
        ObjectInputStream ois = new ObjectInputStream(inStream);
        Scene s = null;
        try {
            s = (Scene)ois.readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        ois.close();
        
        s.instantiation = this;
        return s;
    }catch(IOException e) {
        e.printStackTrace();
        return null;
    }
}

Thanks in advance

Most likely you need to refresh the project so that Eclipse sees that the new file has been added. Select the project and hit F5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM