如何在 libgdx 中创建一个新文件?

Posted

技术标签:

【中文标题】如何在 libgdx 中创建一个新文件?【英文标题】:How to create a new file in libgdx? 【发布时间】:2015-04-29 16:28:05 【问题描述】:

我正在尝试使用 Libgdx 在我的游戏中创建一个新文件(用于保存目的)。我不知道该怎么做,所有教程似乎都是写/读但不创建文件。

 try 
    //File file = new File("LnRSave.txt");
    FileHandle file = Gdx.files.local("LnRSave.txt");
    // if file doesnt exists, then create it
    if (!file.exists()) 

        file.createNewFile();
    

    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(jPoika.toJson(inventory));
    bw.close();

    System.out.println("Done");

 catch (IOException e) 
    e.printStackTrace();

【问题讨论】:

【参考方案1】:

你可以这样做:

FileHandle file = Gdx.files.local("myfile.txt");
file.writeString("My god, it's full of stars", false);

查看文档here

【讨论】:

【参考方案2】:

我推荐你使用 libgdx 提供的Preferences 类。使用Preferences prefs = Gdx.app.getPreferences("YourAppName") 获取您的偏好,您可以使用prefs.contains("inventory") 检查密钥是否存在。使用prefs.putInteger("inventory", value) 添加或更改一个值(您也可以使用putString、putFloat 等),然后使用prefs.flush()

【讨论】:

以上是关于如何在 libgdx 中创建一个新文件?的主要内容,如果未能解决你的问题,请参考以下文章

如何在LibGDX中实现Sugar ORM

导出在 Blender 中创建并在 libgdx 中使用的 fbx 对象的选项

什么命令在终端中创建一个新文件?

如何在 Java 中创建一个新的 zip 文件并向其中添加一个大目录?

如何在目标 c ipad 应用程序中创建要写入的新文本文件

在 Libgdx 中创建水