//上下文对象,要初始化
Context mContext;
/**
* 保存文件到手机内存
* @param filename
* @param content
* @param append
* @return
*/
public boolean saveFileToROM(String filename, String content, boolean append)
{
try
{
File file = new File(mContext.getFilesDir(), filename);
FileOutputStream fos = new FileOutputStream(file, append);
fos.write(content.getBytes());
fos.close();
return true;
} catch (Exception e)
{
e.printStackTrace();
return false;
}
}
/**
* 读取文件内容来自手机内存
* @param filename
* @return
*/
public String readFileFromROM(String filename){
File file = new File(mContext.getFilesDir(),filename);
try{
FileInputStream fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String line;
StringBuffer output = new StringBuffer();
while((line = reader.readLine()) != null)
{
output.append(line);
}
reader.close();
return output.toString();
}catch(Exception e){
e.printStackTrace();
return null;
}
}
android,保存文件到手机内存和读取
最新推荐文章于 2024-08-09 17:19:32 发布