您的位置:首页 > 编程语言

安卓第一行代码之数据存储

2018-03-30 19:46 351 查看
文件存储的基本思路:
写入:
String data = "this is the data";
FileOutputStream out =null;
BufferedWriter writer = null;
try
{
    out = openFileOutput("content_path",MODE_PRIVATE/MODE_APPEND);

    writer = new BufferedWriter(new OutputStreamWriter(out));

    writer.write(data);

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

}
finally
{
    try
        {
            if(writer!=null)

            {
                writer.close();

            }

            catch(IOException e)

            {
                e.printStackTrace();

            }

        }

}
读取:
FileInputStream in =null;
BufferedReader reader =null;
StringBuilder builder = new StringBuilder();
try
{
    in = openFileInput("content_path");

    reader = new BufferedReader(new InputStreamReader(in));

    String line="";
    while(line=reader.readLine()!=null)

    {
        builder.append(line);

    }

}

catch(IOException e)
{
    e.printStackTrace();

}
finally
{
    try
        {
            if(reader!=null)

            {
                reader.close();

            }

        }
            catch(IOException e)

            {
                e.printStackTrace();

            }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: