【Java-IO】IO入门

根据处理的对象分为:字节流和字符流


字节流可以处理所有的数据:通常以“Stream”结尾,读取一个字节,返回一个字节。


字符流则只能处理纯文本数据:通常以“Reader”或者“Writer”结尾,需要通过查找编码表,才能返回字符


一、字节流读写文件

try {
	FileInputStream fis = new FileInputStream("files/test.txt");
	FileOutputStream fos = new FileOutputStream("files/new.txt");
			
	byte[]bytes = new byte[1024];
	while(fis.read(bytes)!=-1){
		fos.write(bytes);
	}
			
	System.out.println("copy done");
	fos.close();
	fis.close();
			
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}

注意点:

1. 记得读写完成,要close();

2. FileInputStream.read(byte[])返回的是读取byte到缓存的长度,如果没有了,返回-1

3. 如果读取byte[]后想转成String,最好设置编码

try {
	FileInputStream fis = new FileInputStream("files/test.txt");
			
	byte[] input = new byte[1024];
	fis.read(input);
			
	String inputString = new String(input,"UTF-8");
	System.out.println(inputString);
			
	fis.close();
			
			
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (IOException e){
	e.printStackTrace();
}

   写数据也一样

try{
	FileOutputStream fos = new FileOutputStream("files/test.txt");
			
	String s = "这是我写入txt的内容";
	byte[] bytes = s.getBytes("UTF-8");
			
	fos.write(bytes);
			
	System.out.println("写入完成");
	fos.close();
			
			
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (UnsupportedEncodingException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}


二、字符流读写文件

try {
	FileInputStream fis = new FileInputStream("files/test.txt");
	FileOutputStream fos = new FileOutputStream("files/new.txt");
	InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
	OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

	char[] temp = new char[50];
	int len = 0;
	while((len = isr.read(temp))!=-1){
		osw.write(temp, 0, len);
		//				osw.write(temp);
		//				System.out.print(temp);
	}

	System.out.println("copy done");

	osw.close();
	fos.close();
	isr.close();
	fis.close();


} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (UnsupportedEncodingException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}

注意点:

1. 获取read数据的时候,得注意长度,因为最后一次读取的时候,往往不能填满char[]

2. 输入和输出流的编码得一致

三、带有缓冲区的字符流读写

try {
	FileInputStream fis = new FileInputStream("files/test.txt");
	FileOutputStream fos = new FileOutputStream("files/new.txt");
	InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
	OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
	BufferedReader br = new BufferedReader(isr);
//			BufferedWriter bw = new BufferedWriter(osw);
	PrintWriter pw = new PrintWriter(osw,true);
	
	String line = null;
	while((line = br.readLine())!=null){
//				bw.write(line+"\n");
		pw.println(line);
	}
	
//	bw.flush();
//	pw.flush();
	pw.close();
//	bw.close();
	osw.close();fos.close();
	br.close();isr.close();fis.close();
	
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (UnsupportedEncodingException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}

注意点:

1.  使用BufferedReader  readline时,读出的字符串是不带\n的

2.  在关闭输出流之前要用flush()强制把缓冲区的内容写出

3.  可以使用PrintWriter的println方法写,这个自带\n,其次PrintWriter在new的时候,可以设置autoflush为true,就不用   

     在关闭之前flush

四、FileReader和FileWriter

如果只针对简单的纯文本文件进行读写操作,可以使用FileReader和FileWriter来代替FileInputStream以及OutputStream转InpttStreamReader/OutputStreamWriter

try {
	FileInputStream fis = new FileInputStream("files/test.txt");
	FileOutputStream fos = new FileOutputStream("files/new.txt");
	
	byte[]bytes = new byte[1024];
	while(fis.read(bytes)!=-1){
		fos.write(bytes);
	}
	
	System.out.println("copy done");
	fos.close();
	fis.close();
	
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}

注意点:

1. 记得flush,记得\n

2. FileReader/Writer 使用的是系统默认的字符集编码,所以可能存在编码的问题。如果出问题了,请使用FileInputStream -> InputStreamReader,并构造时规定编码












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值