Android Java IO写入与读取数据

首次发布:2020-10-04 22:51
public void save(String fileName,String content)  {
    try {
        FileOutputStream  fileOutputStream=openFileOutput(fileName,MODE_PRIVATE);
        fileOutputStream.write(content.getBytes());
        fileOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public String read(String fileName)  {
    try {
        FileInputStream fileInputStream=openFileInput(fileName);
        byte[] buff=new byte[1024];
        StringBuffer stringBuffer=new StringBuffer("");
        int len=0;
        while ((len=fileInputStream.read(buff))>0){
            stringBuffer.append(new String(buff,0,len));
        }
        return  stringBuffer.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}