文件读写操作汇总解析

转载博客:http://www.cnblogs.com/lianghui66/archive/2013/09/05/3303546.html

InputStream 字节流输入类,读取byte数组

Reader 字符流读取类,读取Char数组和String对象


FileInputStream 文件输入字节流

BufferedReader 带缓冲的字符流读取


InputStreamReader j将字节流转换为字符流的一个类,构造方法的参数是字节输入流

FileReader 继承InputStreamReader类,构造方法的参数是文件对象或者文件路径名称


用FileInputStream(FileOutputStream)读写文件

public void write(String fileName,String writeString) throws Exception{
try{
         FileOutputStream fout=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{
         FileInputStream fin=openFileInput(fileName,MODE_PRIVATE);
         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;
}

用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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值