您的位置:首页 > 移动开发 > Android开发

android私有文件资源文件的存取

2011-04-14 14:03 393 查看
一、私有文件夹下的文件存取(/data/data/包名)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.http.util.EncodingUtils;

public void writeFileData(String fileName,String message){
try{
FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = message.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}

public String readFileData(String fileName){
String res="";
try{
FileInputStream fin = openFileInput(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
e.printStackTrace();
}
return res;
}

二、从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写)
public String getFromRaw(String fileName){
String res = "";
try{
InputStream in = getResources().openRawResource(R.raw.test1);
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
in.close();
}
catch(Exception e){
e.printStackTrace();
}
return res ;
}
三、从asset中获取文件并读取数据(资源文件只能读不能写)
public String getFromAsset(String fileName){
String res="";
try{
InputStream in = getResources().getAssets().open(fileName);
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: