public void writeFileData(String filename, String message)
{
FileOutputStream fs = null;
try
{
fs = openFileOutput(filename, MODE_PRIVATE);
byte[] msg = message.getBytes();
fs.write(msg);
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException ioe)
{
}
finally
{
if(fs != null)
{
try
{
fs.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
public String readFileData(String filename)
{
FileInputStream fis = null;
String result = null;
try
{
fis = openFileInput(filename);
int len = fis.available();
byte[] msg = new byte[len];
fis.read(msg);
result = EncodingUtils.getString(msg, ENCODING);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally
{
if(fis != null)
{
try
{
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return result;
}
简单的读写操作
最新推荐文章于 2022-02-21 12:12:41 发布
本文介绍了一个简单的文件读写方法实现,包括将字符串数据写入文件和从文件中读取字符串数据的功能。通过使用FileOutputStream和FileInputStream,该方法能够有效地进行文件数据的写入和读取。
592

被折叠的 条评论
为什么被折叠?



