IO流

package IOStream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//读和写
public class DataProcess {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File f=new File("C:\\Users\\Hasaky\\Desktop\\testWrite.txt");
		

	}
	
	//通过字符流读数据
	static String readbychar01(File f) {
		String result=null;
		try {
			FileReader fr=new FileReader(f);
			char[] ch=new char[1024];
			int len=0;
			while((len=fr.read(ch))!=-1) {
				result=new String(ch,0,len);
			}
			fr.close();
		}catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}
	
	//通过字符流写数据
	static void writebychar01(File f,String content) {
		try {
			FileWriter fw=new FileWriter(f);
			fw.write(content);
			fw.flush();
			fw.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	//通过字符缓冲流读数据
	static String readbychar02(File f) {
		String result=null;
		try {
			BufferedReader br=new BufferedReader(new FileReader(f));
			char[] ch=new char[1024];
			int len=0;
			while((len=br.read(ch))!=-1) {
				result=new String(ch,0,len);
			}
			br.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}
	
	//通过字符缓冲流写数据
	static void writebychar02(File f,boolean Boolean,String content) {
		try {
			BufferedWriter bw=new BufferedWriter(new FileWriter(f, Boolean));
			bw.write(content);
			bw.flush();
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public static String readbybyte02(File f) {
		String result=null;
		try {
			BufferedInputStream bis=new BufferedInputStream(new FileInputStream(f));
			byte[] buff=new byte[1024];
			int len=0;
			while((len=bis.read(buff))!=-1){
				result=(new String(buff,0,len));
			}
			bis.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		return result;
		
	}
	public static void writebybyte02(File F,boolean Boolean,String Content) {
		//boolean判断是否是追写
		try {
			BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(F,Boolean));
			int len=Content.length();
			for(int i=0;i<len;i++) {
				bos.write((int)Content.charAt(i));
			}
			bos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值