繁体   English   中英

当我通过代码创建文件时,我在 eclipse 中看不到它

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

所以我在java做一个游戏引擎,它有两部分:编辑器和游戏引擎本身。 在编辑器中,我序列化 Scene 对象并将它们写入 .scene 文件以供引擎读取,但该文件未显示在 eclipse 中,我可以在文件资源管理器中看到它,因此创建了它。 有一段时间我没有注意,因为我仍然可以使用File class 读取文件。但后来我尝试将其导出到可运行的 jar 文件中,但构建没有成功,所以我尝试使用getClass().getResource("path") ,我遇到了问题,据我所知,由于一些奇怪的原因,文件没有被添加到构建路径中,即使它在 src 文件夹中,我可以通过编程方式将文件添加到构建路径中,还是我需要做点别的?

这是一些代码:

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

读取.scene文件的代码:

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

提前致谢

您很可能需要刷新项目,以便 Eclipse 看到新文件已添加。 Select 项目并按 F5。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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