安卓c语⾔打开⽂件,Android编程之⽂件读写操作与技巧总结
【经典收藏】
本⽂实例总结了Android⽂件读写操作。分享给⼤家供⼤家参考,具体如下:
在Android中的⽂件放在不同位置,它们的读取⽅式也有⼀些不同。
本⽂对android中对资源⽂件的读取、数据区⽂件的读取、SD卡⽂件的读取及RandomAccessFile的⽅式和⽅法进⾏了整理。供参考。⼀、资源⽂件的读取:
1) 从resource的raw中读取⽂件数据:
String res = "";
try{
//得到资源中的Raw数据流
InputStream in = getResources().openRawResource(st);
//得到数据的⼤⼩
int length = in.available();
byte [] buffer = new byte[length];
//读取数据
//依的编码类型选择合适的编码,如果不调整会乱码
res = String(buffer, "BIG5");
//关闭
in.close();
}catch(Exception e){
e.printStackTrace();
}
2) 从resource的asset中读取⽂件数据
String fileName = ""; //⽂件名字
String res="";
try{
//得到资源中的asset数据流
InputStream in = getResources().getAssets().open(fileName);
int length = in.available();
byte [] buffer = new byte[length];
in.close();
res = String(buffer, "UTF-8");
}catch(Exception e){
e.printStackTrace();
android编程入门指南 pdf}
⼆、读写/data/data/⽬录上的⽂件:
//写数据
public void writeFile(String fileName,String writestr) throws IOException{ try{
FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE); byte [] bytes = Bytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}
//读数据
public String readFile(String fileName) throws IOException{
String res="";
try{
FileInputStream fin = openFileInput(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
res = String(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
三、读写SD卡中的⽂件。也就是/mnt/sdcard/⽬录下⾯的⽂件 :
//写数据到SD中的⽂件
public void writeFileSdcardFile(String fileName,String write_str) throws IOException{ try{
FileOutputStream fout = new FileOutputStream(fileName);
byte [] bytes = Bytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}
//读SD中的⽂件
public String readFileSdcardFile(String fileName) throws IOException{
String res="";
try{
FileInputStream fin = new FileInputStream(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
res = String(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
四、使⽤File类进⾏⽂件的读写:
//读⽂件
public String readSDFile(String fileName) throws IOException {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
int length = fis.available();
byte [] buffer = new byte[length];
res = String(buffer, "UTF-8");
fis.close();
return res;
}
//写⽂件
public void writeSDFile(String fileName, String write_str) throws IOException{
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
byte [] bytes = Bytes();
fos.write(bytes);
fos.close();
}
五、另外,File类还有下⾯⼀些常⽤的操作:
String Name = Name(); //获得⽂件或⽂件夹的名称:
String parentPath = Parent(); //获得⽂件或⽂件夹的⽗⽬录
String path = AbsoultePath();//绝对路经
String path = Path();//相对路经
File.mkDir(); //建⽴⽂件夹
File.isDirectory(); //判断是⽂件或⽂件夹
File[] files = File.listFiles(); //列出⽂件夹下的所有⽂件和⽂件夹名
File.delete(); //删除⽂件夹或⽂件
六、使⽤RandomAccessFile进⾏⽂件的读写:
RandomAccessFile的使⽤⽅法⽐较灵活,功能也⽐较多,可以使⽤类似seek的⽅式可以跳转到⽂件的任意位置,从⽂件指⽰器当前位置开始读写。
它有两种构造⽅法
new RandomAccessFile(f,"rw");//读写⽅式
new RandomAccessFile(f,"r");//只读⽅式
使⽤事例:
/*
* 程序功能:演⽰了RandomAccessFile类的操作,同时实现了⼀个⽂件复制操作。
*/
import java.io.*;
public class RandomAccessFileDemo {
public static void main(String[] args) throws Exception {
RandomAccessFile file = new RandomAccessFile("file", "rw");
// 以下向file⽂件中写数据
file.writeInt(20);// 占4个字节
file.writeDouble(8.236598);// 占8个字节
file.writeUTF("这是⼀个UTF字符串");// 这个长度写在当前⽂件指针的前两个字节处,可⽤readShort()读取
file.writeBoolean(true);// 占1个字节
file.writeShort(395);// 占2个字节
file.writeLong(2325451l);// 占8个字节
file.writeUTF("⼜是⼀个UTF字符串");
file.writeFloat(35.5f);// 占4个字节
file.writeChar('a');// 占2个字节
file.seek(0);// 把⽂件指针位置设置到⽂件起始处
// 以下从file⽂件中读数据,要注意⽂件指针的位置
System.out.println("——————从file⽂件指定位置读数据——————");
System.out.adInt());
System.out.adDouble());
System.out.adUTF());
file.skipBytes(3);// 将⽂件指针跳过3个字节,本例中即跳过了⼀个boolean值和short值。
System.out.adLong());
file.adShort()); // 跳过⽂件中“⼜是⼀个UTF字符串”所占字节,注意readShort()⽅法会移动⽂件指针,所以不⽤加2。
System.out.adFloat());
//以下演⽰⽂件复制操作
System.out.println("——————⽂件复制(从file到fileCopy)——————");
file.seek(0);
RandomAccessFile fileCopy=new RandomAccessFile("fileCopy","rw");
int len=(int)file.length();//取得⽂件长度(字节数)
byte[] b=new byte[len];
fileCopy.write(b);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。