文件读写操作汇总解析
来源:互联网 发布:linux漏洞列表 编辑:程序博客网 时间:2024/05/22 05:10
转载博客:http://www.cnblogs.com/lianghui66/archive/2013/09/05/3303546.html
InputStream 字节流输入类,读取byte数组
Reader 字符流读取类,读取Char数组和String对象
FileInputStream 文件输入字节流
BufferedReader 带缓冲的字符流读取
InputStreamReader jiang字节流转换为字符流的一个类,构造方法的参数是字节输入流
FileReader 继承InputStreamReader类,构造方法的参数是文件对象或者文件路径名称
public void write(String fileName,StringwriteString) throws Exception{try{ FileOutputStreamfout=openFileOutput(fileName,MODE_PRIVATE); byte[]bytes=writeString.getBytes(); fout.write(bytes); fout.close(); }catch(Exception e){ e.printStackTrace(); }} public String read(String fileName){String res=””;try{ FileInputStreamfin=openFileInput(fileName,MODE_PRIVATE); intlength=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;}
用BufferedReader和FileReader读文件
import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.io.File;import android.util.Log;private String getCoulometerValue(String batterypath){String procElectricityStr;try{BufferedReader reader=new BufferedReader(new FileReader(batterypath),256);try{procElectricityStr=reader.readLine(); }finally{ reader.close(); } return procElectricityStr; }catch(IOException e){ e.printStackTrace(); return “Unavailable”; }}
用BufferedReader,InputStreamReader,FileInputStream(BufferedWriter,OutputStreamWriter,FileOutputStream)读写文件
public String load(String fileName){FileInputStream in=null;BufferedReader reader=null;StringBuilder content=new StringBulider();try{in=openFileInput(fileName);reader=new BufferedReader(new InputStreamReader(in));String line=””;while((line=reader.readLine())!=null){content.append(line); } }catch(IOException e){ e.printStackTrace() }finally{ if(reader!=null) try{reader.close() }catch(IOException e){ e.printStackTrace(); } } } return content.toString();}
public void Save(String fileName,String data){FileOutputStream out=null;BufferedWriter writer=null;try{out=openFileOutput(fileName,Context.MODE_PRIVATE);writer=new BufferedWriter(new OutputStreamWriter(out));writer.write(data); }catch(IOException e){ e.printStackTrace(); }finally{ if(writer!=null){ writer.close(); }catch(IOException){ e.printStackTrace(); } }}
0 0
- 文件读写操作汇总解析
- HDFS文件的读写操作理论解析
- json文件的解析(读写操作)
- Matlab的各种数据读取、文件读写等操作汇总
- Matlab的各种数据读取、文件读写等操作汇总
- Android读写文件汇总
- MATLAB读写文件汇总
- 文件读写函数汇总
- Android-解析/生成xml、文件读写、数据库操作
- 05 - 数据操作一:文件读写与XML解析、SharedPreferences
- 【Android读写文件方法汇总】
- Android 读写文件方法汇总
- 文件解析漏洞汇总
- 文件读写操作
- 另一个文件读写操作
- [C++]文件读写操作
- 读写文件操作
- xml文件操作( 读写)
- 解决IE浏览器的only one scriptx object can be used per browser window问题
- Java不用中间量 交换A、B
- Python小白第一个小程序出现的问题
- php拾漏
- 采用Autolayout布局时,调试由于限制条件冲突出现的警告。
- 文件读写操作汇总解析
- Android 检测代码规范和检测bug的工具汇总整理
- Xen 启动客户虚拟机的过程分析
- [紫书] 生成1~n的全排列
- java并发编程——Future实现“任务的提交”与 "任务的执行"相分离
- 在手机端访问pc服务器端口方法
- JAVA编码集转换
- android 使用InstanceState保存和恢复数据
- PHP常用函数(4)json_decode(),json_encode()