Android读写文件示例
16lz
2021-12-04
import java.io.BufferedOutputStream;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class TalentFile{public final static int READ = 1,WRITE = 2; File file = null;OutputStream output = null;InputStream input = null;public boolean Create(String path,int rw){file = new File(path); try { if((rw&WRITE)==WRITE) output = new BufferedOutputStream(new FileOutputStream(file)); if((rw&READ)==READ) input = new BufferedInputStream(new FileInputStream(file)); } catch(FileNotFoundException e) { com.Loge("TalentFile.Create:"+e.getMessage()); return false; } catch(SecurityException e){ com.Loge("TalentFile.Create:"+e.getMessage()); return false; }return true;}public void Close(){try {if(output!=null)output.close();if(input!=null)input.close();} catch (IOException e) {com.Loge("TalentFile.Close:"+e.getMessage());}}public long Length(){if(file==null) return 0;long len = 0;try{len = file.length();}catch(SecurityException e){com.Loge("TalentFile.Length:"+e.getMessage());}return len;}public void Flush(){try {if(output!=null)output.flush();} catch (IOException e) {com.Loge("TalentFile.Flush:"+e.getMessage());}}/**读取最多*/public int Read(byte[] buf,int offset,int length){int readLen = 0;try {readLen = input.read(buf,offset,length);} catch (IOException e) {com.Loge("TalentFile.Read:"+e.getMessage());} catch(IndexOutOfBoundsException e){com.Loge("TalentFile.Read:"+e.getMessage());}return readLen;}/***/public int Read(byte[] buf,int offset){int readLen = 0;try {readLen = input.read(buf,offset,buf.length);} catch (IOException e) {com.Loge("TalentFile.Read:"+e.getMessage());} catch(IndexOutOfBoundsException e){com.Loge("TalentFile.Read:"+e.getMessage());}return readLen;}public boolean Write(byte[] buf,int offset){ try {output.write(buf,offset,buf.length);} catch (IOException e) {com.Loge("TalentFile.Write:"+e.getMessage());return false;} catch (IndexOutOfBoundsException e){com.Loge("TalentFile.Write:"+e.getMessage());return false;} return true;}public boolean Write(byte[] buf,int offset,int count){ try {output.write(buf,offset,count);} catch (IOException e) {com.Loge("TalentFile.Write:"+e.getMessage());return false;} catch (IndexOutOfBoundsException e){com.Loge("TalentFile.Write:"+e.getMessage());return false;} return true;}}
更多相关文章
- android中设置进度条读取
- android 保存和读取文件
- Android读取本地TXT文件
- android 保存和读取文件
- Android(安卓)Http网络数据传输备忘
- Android常用权限
- android 读写文件
- Android(安卓)uses-permission 总结
- android 读取sd卡中的图片