将游戏信息保存到android中的文件

将游戏信息保存到android中的文件,android,file,load,save,andengine,Android,File,Load,Save,Andengine,我还是新手。我正在制作一个android应用程序来作曲。因此,用户可以创建自己的音乐。如何保存用户在界面中选择的内容?示例:有3个按钮可供选择,按钮1用户选择“a”,按钮2用户选择“c”,按钮3用户选择“f”。如何将其保存到类似.txt文件的文件中,然后在用户想要再次播放时加载它 对不起,如果你真的不懂我的语言。也许你能给我一些例子保存和加载到文件?我在stackoverflow中读了一些,但是太多了,我仍然找不到我的答案。您可以使用来保存文件: File newxmlfile1 = new F

我还是新手。我正在制作一个android应用程序来作曲。因此,用户可以创建自己的音乐。如何保存用户在界面中选择的内容?示例:有3个按钮可供选择,按钮1用户选择“a”,按钮2用户选择“c”,按钮3用户选择“f”。如何将其保存到类似.txt文件的文件中,然后在用户想要再次播放时加载它


对不起,如果你真的不懂我的语言。也许你能给我一些例子保存和加载到文件?我在stackoverflow中读了一些,但是太多了,我仍然找不到我的答案。

您可以使用来保存文件:

File newxmlfile1 = new File(Environment.getExternalStorageDirectory()+"/kadirGameLevels");
   newxmlfile1.mkdirs();


   int i = 1;
   File newxmlfile;
   do{
       String filename = i+".lvl";
       newxmlfile = new File(Environment.getExternalStorageDirectory()+"/kadirGameLevels/"+filename);
       i++;
   }while(newxmlfile.exists());

   try{
           newxmlfile.createNewFile();
   }catch(IOException e){
           Log.e("IOException", "exception in createNewFile() method");
   }
   //we have to bind the new file with a FileOutputStream
   FileOutputStream fileos = null;        
   try{
           fileos = new FileOutputStream(newxmlfile);
   }catch(FileNotFoundException e){
           Log.e("FileNotFoundException", "can't create FileOutputStream");
   }
要将文件下载到应用程序,请执行以下操作:

    File file = new File(Environment.getExternalStorageDirectory() + "/kadirGameLevels/1.lvl");
        DataInputStream stream = new DataInputStream(new FileInputStream(file));

为什么要归档?为什么不共享偏好

 private void saveCurrentScore(long currentScore) {
            try {
                android.content.SharedPreferences.Editor editor = prefs.edit();
                editor.putLong("currentscore", currentScore);
                editor.commit();

            } catch (Exception e) {
                e.printStackTrace();
            }

        }


        private String getCurrentScore() {
            try{prefs = PreferenceManager.getDefaultSharedPreferences(activity);
            return ""+  prefs.getLong("currentscore", 0);
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            return "";
        }

注意:-如果您正在使用andengine,也可以查看SecureSharedReferences

Google about SharedReferences。这是在应用程序会话之间保存和持久化数据的最简单方法。我仍然不太明白。示例:字符串file1=“a”;字符串file2=“b”;字符串file3=“c”;我想把它保存到一个文件中,让我们使用.txt,也许txt里面会有这个3变量。我想把它加载到我的应用程序中。因此,应用程序将知道file1=“a”file2=“b”file3=“c”,因为我希望用户可以在他们之间共享他们的数据。在这个应用程序中,我制作了一个用于作曲的应用程序。我的应用程序中没有分数。我想将我创作的音乐的数据保存在.txt这样的文件中,然后在需要时将其加载到应用程序中。您可以将该文件保存在存储器中,并将该文件的路径保存在共享首选项中