【问题标题】:Using internal storage to save list of integers使用内部存储保存整数列表
【发布时间】:2012-08-01 10:33:50
【问题描述】:

我正在尝试通过将每个整数保存在内部存储中文件的新行中来在我的应用程序中保存整数列表。 为了检索它,我逐行读取它并将每个行值(解析为整数)放入我的整数列表中。 我知道数据库更适合这种东西,但这应该可行。

我现在尝试了很长一段时间,但它似乎从来没有工作过。尝试阅读时,我总是得到一个空指针异常。我记录了“行”,它给出了它应该具有的值。但是

保存一个id,将其添加为新字符串:

private void saveToFavorites(Integer saveFav) {
            String favstr = String.valueOf(saveFav);
            BufferedWriter writer = null;
            try {
              writer = new BufferedWriter(new OutputStreamWriter(openFileOutput("favorites", MODE_WORLD_WRITEABLE)));
              writer.newLine();
              writer.append((favstr));
              System.out.println(" added to favs :"+ saveFav);
            } catch (Exception e) {
                    e.printStackTrace();
            } finally {
              if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
              }
            }
        } 

以及阅读方法:

  @SuppressWarnings("null")
    private List<Integer> readFileFromInternalStorage() {
            List<Integer> favs = null;
            BufferedReader input = null;
            try {
              input = new BufferedReader(new InputStreamReader(openFileInput("favorites")));
              String line;
              while ((line = input.readLine()) != null) {
                  System.out.println("readFileFromInternalStorage line value: "+ line );
                 favs.add(Integer.parseInt(line));
              }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("readFileFromInternalStorage: fail" );
            } finally {
            if (input != null) {
              try {
                input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
              }
            }
            return favs;
        }  

在其他活动中。我认为它会工作,但它显然没有。回读时,logline: System.out.println("readFileFromInternalStorage line value: "+ line ); 显示 line 的值等于最后添加的 id 和一个空行,而不是其他行。所以逐行保存失败。同样,当将其解析为整数时,它会失败,这很奇怪,因为它只是一个数字。

08-01 12:29:54.190: I/System.out(1540): readFileFromInternalStorage line value: 
08-01 12:29:54.190: I/System.out(1540): readFileFromInternalStorage line value: 301

有人知道我需要改变什么吗?

【问题讨论】:

    标签: android string storage internal


    【解决方案1】:

    由于IntegerSerializable,我建议序列化整个List

    private void saveList(List<Integer> list) {
    try {
                File file = Environment.getExternalStorageDirectory();
                File filename = new File(file, "yourfilename");
                fos = new FileOutputStream(filename);
                out = new ObjectOutputStream(fos);
                out.writeObject(list);
                out.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    }
    
    private void readList()
    {
    
    
        try {
                    File file = Environment.getExternalStorageDirectory();
                    File filename = new File(file, "yourfilename");
                    fis = new FileInputStream(filename);
                    in = new ObjectInputStream(fis);
                    List<Integer> list= (List<Integer>) in.readObject();
                    in.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                }
    
    }
    

    【讨论】:

    • 我正在尝试这个,但我得到一个错误:android filenotfoundexception 没有这样的文件或目录,对于我放入文件名的字符串。我应该放什么? write_external_memory 在我的清单中,不是这样
    【解决方案2】:

    试试这个可能对你有帮助:- 1 - 字符串 saveFav = 包含此形式的所有整数 I1+"/"I2+"/"I3;

    2:- 然后保存到文件中

         private void saveToFavorites(String saveFav) { 
           //right here your code for write into file saveFave string
     }  
    

    在读取文件读取字符串和拆分(“/”)。它为我工作。

    【讨论】:

      【解决方案3】:

      这里有一些工作代码,可以在手机内存中读取和写入整数。 您可以创建一个数组或整数列表,基本上只是对其进行迭代,直到所有整数都被保存/读取到内存/从内存中读取:

      这是将 int 写入内存的代码:

      public void writePrimitiveInternalMemory(String filename, int value) {
              SharedPreferences preferences = this.getPreferences(Activity.MODE_PRIVATE);
              SharedPreferences.Editor editor = preferences.edit();
              editor.putInt(filename, value);
              editor.commit();
      
          }
      

      这是从内存中读取的代码:

      public int readPrimitiveInternalMemoryInteger(String filename) {
              SharedPreferences preferences = this.getPreferences(Activity.MODE_PRIVATE);
              return preferences.getInt(filename, 0);
      
          }
      

      希望对你有帮助!

      【讨论】:

      • 使用共享偏好我遇到了一些麻烦,因为它没有在我想要的时候提交,之前问过一些关于这种行为的问题,这就是我选择内部存储的原因。
      【解决方案4】:

      您没有分配整数列表...

      List<Integer> favs = null;
      

      分配一个新的arraylist..

      List<Integer> favs = new ArrayList<Integer>();
      

      【讨论】:

        猜你喜欢
        • 2014-01-29
        • 1970-01-01
        • 2021-01-28
        • 2022-01-12
        • 1970-01-01
        • 2018-02-20
        • 2014-09-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多