Android:将arrayList保存到文件不起作用

Posted

技术标签:

【中文标题】Android:将arrayList保存到文件不起作用【英文标题】:Android: Saving arrayList to file not working 【发布时间】:2015-12-10 00:06:56 【问题描述】:

我有一个包含recyclerView 的活动DrinkWater。我正在尝试将名为 dataarrayList 保存到内部存储中以供将来参考,但不知何故它不起作用。

我确实在清单文件中添加了权限,所以这不是问题。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

下面是DrinkWater.java 文件的(简化)代码。出于调试目的,我做了几个 Toast。因此不要错过 //cmets

    public class DrinkWater extends ActionBarActivity 

Toolbar toolbar;
private RecyclerView recyclerView;
private InfoTodayAdapter adapter;
FileOutputStream fos;
FileInputStream fis;
List<InformationToday> data = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drink_water);

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview_today);
    adapter = new InfoTodayAdapter(this,getData());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));


public List<InformationToday> getData()
    try 
        fis = getBaseContext().openFileInput("arrayListToday");
        ObjectInputStream ois = new ObjectInputStream(fis);
        //Below Toast just for debugging. 
        Toast.makeText(this,"Try block for read data called 1",Toast.LENGTH_SHORT).show(); 
        data = (List<InformationToday>) ois.readObject(); // Cause of the error. 
        //Below toast for debugging. But it is never executed.
        Toast.makeText(this,"Try block for read data called 2",Toast.LENGTH_SHORT).show();  
        ois.close();
     catch (FileNotFoundException e) 
        e.printStackTrace();
        Log.d("TAG A","arrayListToday file not found");
     catch (Exception e)
        Log.d("TAG A","Exception generated 1");
    
    return data;



public void addWater(View view)    // This method adds data to the recyclerView. It is called by on click of the Floating Action Button
    InformationToday i = new InformationToday();
    if(view.getId() == R.id.fifty_button)
    
        i.volume = "50";
        Log.d("TAG A","i.volume = 50");
    
    else
    
        i.volume = "100";
    
    data.add(0,i);          
    adapter.update(data);   //This method is described in recyclerView adapter. It updates the recyclerView data.
    try 
        fos = getBaseContext().openFileOutput("arrayListToday",Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        Toast.makeText(this,"Try block for wrtie data called 1",Toast.LENGTH_SHORT).show(); //For debugging.
        oos.writeObject(data); // Cause of the error. 
        Toast.makeText(this,"Try block for write data called 2",Toast.LENGTH_SHORT).show(); //For debugging. It is never executed.
        oos.close();
     catch (FileNotFoundException e) 
        Toast.makeText(this,"Catch: File not gound exception",Toast.LENGTH_SHORT).show();
        Log.d("TAG A", "Catch: File not found");
        e.printStackTrace();
    catch (Exception e)
        Toast.makeText(this,"Catch: Exception e",Toast.LENGTH_SHORT).show();
        Log.d("TAG A","Catch: Exception e");
    
    com.getbase.floatingactionbutton.FloatingActionsMenu f = (FloatingActionsMenu) findViewById(R.id.multiple_actions);
    f.collapseImmediately();



没有这样的错误。但 Logcat 的可能异常:“未知错误(代码 14)无法打开数据库。” 2.“这里不支持读取空字符串” 3.“column context_id is not unique (code 19)”

解决错误的问题(代码 14)无法打开数据库没有帮助。

【问题讨论】:

为什么你不使用数据库 但问题与数据库有关 @bpA 遗憾的是,就目前而言,我对数据库没有经验。而且我的应用程序也不需要太多数据以供将来参考。是的,我是开发新手。是的,我很快就会使用数据库,但现在,需要按原样解决它。 :) 【参考方案1】:

您必须序列化对象,然后将它们写入文件。

保存(无异常处理代码):

FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(data);
os.close();
fos.close();

试试这个,让我知道这是否适合你。

Reference Link

【讨论】:

花了我一些时间来理解serializable,但现在终于可以使用了。谢谢

以上是关于Android:将arrayList保存到文件不起作用的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Android 的 MainActivity 中保存 ArrayList?

在 Android 的 SQLite 数据库中保存 ArrayList

自定义对象的 Android ArrayList - 保存到 SharedPreferences - 可序列化?

将位图从 arraylist 保存到 SD 卡 - 保存的文件不可读

在 Android 上跨会话从文件中写入然后读取 ArrayList<Object>?

将位图列表保存到 Parse.com |安卓 |