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

Android 文件读写操作方法总结

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

习永盛,鬼遮眼吓你不是目的,万妖之祖txt下载

android 文件读写操作方法总结

在android中的文件放在不同位置,它们的读取方式也有一些不同。

本文对android中对资源文件的读取、数据区文件的读取、sd卡文件的读取及randomaccessfile的方式和方法进行了整理。供参考。

一、资源文件的读取:

      1) 从resource的raw中读取文件数据:

string res = ""; 
try{ 
 
 //得到资源中的raw数据流 
 inputstream in = getresources().openrawresource(r.raw.test); 
 
 //得到数据的大小 
 int length = in.available();   
 
 byte [] buffer = new byte[length];   
 
 //读取数据 
 in.read(buffer);   
 
 //依test.txt的编码类型选择合适的编码,如果不调整会乱码 
 res = encodingutils.getstring(buffer, "big5"); 
  
 //关闭  
 in.close();    
 
 }catch(exception e){ 
  e.printstacktrace();   
 } 

 2) 从resource的asset中读取文件数据

string filename = "test.txt"; //文件名字 
string res=""; 
try{ 
 
 //得到资源中的asset数据流 
 inputstream in = getresources().getassets().open(filename); 
 
 int length = in.available();   
 byte [] buffer = new byte[length];   
 
 in.read(buffer);    
 in.close(); 
 res = encodingutils.getstring(buffer, "utf-8");  
 
 }catch(exception e){ 
 
  e.printstacktrace();   
 
 } 

二、读写/data/data/<应用程序名>目录上的文件:

//写数据 
public void writefile(string filename,string writestr) throws ioexception{ 
 try{ 
 
  fileoutputstream fout =openfileoutput(filename, mode_private); 
 
  byte [] bytes = writestr.getbytes(); 
 
  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]; 
   fin.read(buffer);  
   res = encodingutils.getstring(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 = write_str.getbytes(); 
 
  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]; 
   fin.read(buffer);  
 
   res = encodingutils.getstring(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]; 
   fis.read(buffer);  
 
   res = encodingutils.getstring(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 = write_str.getbytes(); 
 
  fos.write(bytes); 
 
  fos.close(); 
} 

五、另外,file类还有下面一些常用的操作:

string name = file.getname(); //获得文件或文件夹的名称: 
string parentpath = file.getparent(); //获得文件或文件夹的父目录 
string path = file.getabsoultepath();//绝对路经 
string path = file.getpath();//相对路经 
file.createnewfile();//建立文件 
file.mkdir(); //建立文件夹 
file.isdirectory(); //判断是文件或文件夹 
file[] files = file.listfiles(); //列出文件夹下的所有文件和文件夹名 
file.renameto(dest); //修改文件夹和文件名 
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.println(file.readint()); 
 system.out.println(file.readdouble()); 
 system.out.println(file.readutf()); 
 
 file.skipbytes(3);// 将文件指针跳过3个字节,本例中即跳过了一个boolean值和short值。 
 system.out.println(file.readlong()); 
 
 file.skipbytes(file.readshort()); // 跳过文件中“又是一个utf字符串”所占字节,注意readshort()方法会移动文件指针,所以不用加2。 
 system.out.println(file.readfloat()); 
  
 //以下演示文件复制操作 
 system.out.println("——————文件复制(从file到filecopy)——————"); 
 file.seek(0); 
 randomaccessfile filecopy=new randomaccessfile("filecopy","rw"); 
 int len=(int)file.length();//取得文件长度(字节数) 
 byte[] b=new byte[len]; 
 file.readfully(b); 
 filecopy.write(b); 
 system.out.println("复制完成!"); 
 } 
} 

七、读取资源文件时能否实现类似于seek的方式可以跳转到文件的任意位置,从指定的位置开始读取指定的字节数呢?

答案是可以的。

在fileinputstream和inputstream中都有下面的函数:

public long skip (long bytecount); //从数据流中跳过n个字节 
public int read (byte[] buffer, int offset, int length); //从数据流中读取length数据存在buffer的offset开始的位置。offset是相对于buffer的开始位置的,不是数据流。 

可以使用这两个函数来实现类似于seek的操作,请看下面的测试代码:
[java] view plain copy
//其中read_raw是一个txt文件,存放在raw目录下。 
//read_raw.txt文件的内容是:"abcdefghijklmnopqrst" 
public string getrawstring() throws ioexception { 
  
 string str = null; 
  
 inputstream in = getresources().openrawresource(r.raw.read_raw); 
  
 int length = in.available(); 
 byte[] buffer = new byte[length]; 
  
 in.skip(2); //跳过两个字节 
 in.read(buffer,0,3); //读三个字节 
  
 in.skip(3); //跳过三个字节 
 in.read(buffer,0,3); //读三个字节 
  
 //最后str="ijk" 
 str = encodingutils.getstring(buffer, "big5"); 
  
  
 in.close(); 
  
 return str; 
} 

 从上面的实例可以看出skip函数有点类似于c语言中的seek操作,但它们之间有些不同。

需要注意的是:

1、skip函数始终是从当前位置开始跳的。在实际应用当中还要再判断一下该函数的返回值。
2、read函数也始终是当前位置开始读的。
3、另外,还可以使用reset函数将文件的当前位置重置为0,也就是文件的开始位置。

如何得到文件的当前位置?

我没有找到相关的函数和方法,不知道怎么样才能得到文件的当前位置,貌似它也并不是太重要。

八、如何从fileinputstream中得到inputstream?

public string readfiledata(string filename) throws ioexception{ 
 string res=""; 
 try{ 
   fileinputstream fin = new fileinputstream(filename); 
  inputstream in = new bufferedinputstream(fin); 
 
   ... 
 
  } 
  catch(exception e){ 
   e.printstacktrace(); 
  } 
 
} 

九、apk资源文件的大小不能超过1m,如果超过了怎么办?我们可以将这个数据再复制到data目录下,然后再使用。复制数据的代码如下:

public boolean assetscopydata(string strassetsfilepath, string strdesfilepath){ 
  boolean bissuc = true; 
  inputstream inputstream = null; 
  outputstream outputstream = null; 
   
  file file = new file(strdesfilepath); 
  if (!file.exists()){ 
   try { 
    file.createnewfile(); 
    runtime.getruntime().exec("chmod 766 " + file); 
   } catch (ioexception e) { 
    bissuc = false; 
   } 
    
  }else{//存在 
   return true; 
  } 
   
  try { 
   inputstream = getassets().open(strassetsfilepath); 
   outputstream = new fileoutputstream(file); 
    
   int nlen = 0 ; 
    
   byte[] buff = new byte[1024*1]; 
   while((nlen = inputstream.read(buff)) > 0){ 
    outputstream.write(buff, 0, nlen); 
   } 
    
   //完成 
  } catch (ioexception e) { 
   bissuc = false; 
  }finally{ 
   try { 
    if (outputstream != null){ 
     outputstream.close(); 
    } 
    
    if (inputstream != null){ 
     inputstream.close(); 
    } 
   } catch (ioexception e) { 
    bissuc = false; 
   } 
    
  } 
   
  return bissuc; 
 }  

 总结:

1、apk中有两种资源文件,使用两种不同的方式进行打开使用。

raw使用inputstream in = getresources().openrawresource(r.raw.test);
asset使用inputstream in = getresources().getassets().open(filename);

这些数据只能读取,不能写入。更重要的是该目录下的文件大小不能超过1m。

同时,需要注意的是,在使用inputstream的时候需要在函数名称后加上throws ioexception。

2、sd卡中的文件使用fileinputstream和fileoutputstream进行文件的操作。

3、存放在数据区(/data/data/..)的文件只能使用openfileoutput和openfileinput进行操作。

注意不能使用fileinputstream和fileoutputstream进行文件的操作。

4、randomaccessfile类仅限于文件的操作,不能访问其他io设备。它可以跳转到文件的任意位置,从当前位置开始读写。

5、inputstream和fileinputstream都可以使用skip和read(buffre,offset,length)函数来实现文件的随机读取。

 

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

相关文章:

验证码:
移动技术网