当前位置: 移动技术网 > IT编程>移动开发>Android > Android 读写文件方法汇总

Android 读写文件方法汇总

2019年07月24日  | 移动技术网IT编程  | 我要评论

12306账号泄露,太原公安局长苏浩,京城81号

一、 从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写)
复制代码 代码如下:

string res = "";
try{
inputstream in = getresources().openrawresource(r.raw.bbi);
//在\test\res\raw\bbi.txt,
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
//res = encodingutils.getstring(buffer, "utf-8");
//res = encodingutils.getstring(buffer, "unicode");
res = encodingutils.getstring(buffer, "big5");
//依bbi.txt的编码类型选择合适的编码,如果不调整会乱码
in.close();
}catch(exception e){
e.printstacktrace();
}

mytextview.settext(res);//把得到的内容显示在textview上

二、 从asset中获取文件并读取数据(资源文件只能读不能写)
复制代码 代码如下:

string filename = "yan.txt"; //文件名字
string res="";
try{
inputstream in = getresources().getassets().open(filename);
// \test\assets\yan.txt这里有这样的文件存在
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
res = encodingutils.getstring(buffer, "utf-8");
}catch(exception e){
e.printstacktrace();
}

三、 从sdcard中去读文件,首先要把文件通过\android-sdk-windows\tools\adb.exe把本地计算机上的文件copy到sdcard上去,adb.exe push e:/y.txt /sdcard/, 不可以用adb.exe push e:\y.txt \sdcard\ 同样: 把仿真器上的文件copy到本地计算机上用: adb pull ./data/data/com.tt/files/test.txt e:/
复制代码 代码如下:

string filename = "/sdcard/y.txt";
//也可以用string filename = "mnt/sdcard/y.txt";
string res="";
try{
fileinputstream fin = new fileinputstream(filename);
//fileinputstream fin = openfileinput(filename);
//用这个就不行了,必须用fileinputstream
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();
}
mytextview.settext(res);

四、 写文件, 一般写在\data\data\com.test\files\里面,打开ddms查看file explorer是可以看到仿真器文件存放目录的结构的
复制代码 代码如下:

string filename = "test.txt";
string message = "fffffff11111fffff" ;
writefiledata(filename, message);
public voidwritefiledata(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();
}
}

五、 写, 读data/data/目录(相当ap工作目录)上的文件,用openfileoutput
复制代码 代码如下:

//写文件在./data/data/com.tt/files/下面
public voidwritefiledata(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();
}
}
//-------------------------------------------------------
//读文件在./data/data/com.tt/files/下面
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;
}

六、 写, 读sdcard目录上的文件,要用fileoutputstream, 不能用openfileoutput
复制代码 代码如下:

//写在/mnt/sdcard/目录下面的文件
public voidwritefilesdcard(string filename,string message){
try{
//fileoutputstream fout = openfileoutput(filename, mode_private);
fileoutputstream fout = newfileoutputstream(filename);
byte [] bytes = message.getbytes();
fout.write(bytes);
fout.close();
}
catch(exception e){
e.printstacktrace();
}
}
//读在/mnt/sdcard/目录下面的文件
public string readfilesdcard(string filename){
string res="";
try{
fileinputstream fin = new fileinputstream(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;
}

注: openfileoutput是在raw里编译过的,fileoutputstream是任何文件都可以

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网