android文件读写模板 java程序员

写文件:

FileOutputStream out = null;
try
{
    out = openFileOutput(filename, MODE_PRIVATE);
    byte[] content = (写入的文本).getBytes();
    out.write(content);
}
catch(Exception e)
{
    e.printStackTrace();
}
finally
{
    if(out != null)
    {
        try
        {
            out.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

读文件:

FileInputStream input = null;
try
{
    input = openFileInput(filename);
    Byte []buffer = new Byte[1024];
    StringBuilder sb = new StringBuilder();
    while(input.read(buffer) != -1)
    {
        String content_part = new String(buffer, 0, buffer.length);
        sb.append(content_part);
    }
}
catch(FileNotFoundException e)
{
    e.printStackTrace();
}
catch(Exception e)
{
    e.printStackTrace();
}
finally
{
    if(input != null)
    {
        try
        {
            input.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

原文地址:https://www.cnblogs.com/java20130725/p/3215855.html