Java输入输出流——字符流概述、字节字符转换流、其他字符流
字符流概述
字符输入流Reader
字符输出流Writer
字节字符转换流
InputStreamReader
OutputStreamWriter
查看api
InputStreamReader构造方法
InputStreamReader的方法
OutputStreamWriter构造方法
OutputStreamWriter的方法
注:getEncoding()方法的作用是获取字符编码。
案例:字符输入流
在IOProj工程下新建study.txt文件。
创建字符输入流,读出文件内容方式1
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReaderDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileInputStream fis = new FileInputStream("study.txt");
InputStreamReader isr = new InputStreamReader(fis);
int n=0;
char[] cbuf = new char[10];
while((n=isr.read())!=-1) {
System.out.print((char)n);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
Hello,study1!
Hello,study2!
Hello,study3!
Hello,study4!
Hello,study5!
Hello,study6!
创建字符输入流,读出文件内容方式2
try {
FileInputStream fis = new FileInputStream("study.txt");
InputStreamReader isr = new InputStreamReader(fis);
int n=0;
char[] cbuf = new char[10];
while((n=isr.read(cbuf))!=-1) {
String s = new String(cbuf);
System.out.print(s);
}
isr.close();
fis.close();
}
运行结果:
Hello,study1!
Hello,study2!
Hello,study3!
Hello,study4!
Hello,study5!
Hello,study6!
,study6!
运行发现最后一次读操作出错了,这是因为创建的字符数组长度为10,而最后一次Hello,study6!
长度未到10,因此,要将 String s = new String(cbuf);
转换成实际长度 String s = new String(cbuf,0,n);
。即0代表从第1个数据进行字符串的转换,而n代表字符串的转换长度。
try {
FileInputStream fis = new FileInputStream("study.txt");
InputStreamReader isr = new InputStreamReader(fis);
int n=0;
char[] cbuf = new char[10];
// while((n=isr.read())!=-1) {
// System.out.print((char)n);
// }
while((n=isr.read(cbuf))!=-1) {
String s = new String(cbuf,0,n);
System.out.print(s);
}
isr.close();
fis.close();
}
转换后运行结果:
Hello,study1!
Hello,study2!
Hello,study3!
Hello,study4!
Hello,study5!
Hello,study6!
案例:字符输出流
从study.txt读文件并写入study1.txt文件中。
package com.study.charstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class ReaderDemo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f1 = new File("study1.txt");
try {
f1.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
FileInputStream fis = new FileInputStream("study.txt");
InputStreamReader isr = new InputStreamReader(fis);
FileOutputStream fos = new FileOutputStream(f1);
OutputStreamWriter osw = new OutputStreamWriter(fos);
int n=0;
char[] cbuf=new char[10];
while((n=isr.read(cbuf))!=-1) {
String s = new String(cbuf,0,n);
System.out.print(s);
osw.write(cbuf,0,n);
osw.flush();
}
fis.close();
fos.close();
isr.close();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
运行程序,在study1.txt内显示内容:
Hello,study1!
Hello,study2!
Hello,study3!
Hello,study4!
Hello,study5!
Hello,study6!
以和study.txt不同的编码方式写入study1.txt文件中
FileInputStream fis = new FileInputStream("study.txt");
InputStreamReader isr = new InputStreamReader(fis);
FileOutputStream fos = new FileOutputStream(f1);
OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
运行程序,在study1.txt内显示中文乱码。
解决方式:保持读数据和写数据的编码方式一致即可(要么不写,写就写一致的)。
FileInputStream fis = new FileInputStream("study.txt");
InputStreamReader isr = new InputStreamReader(fis,"GBK");
FileOutputStream fos = new FileOutputStream(f1);
OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
其他字符流
BufferedReader
构造方法:
常用方法:
BufferedWriter
构造方法:
常用方法:
案例:BufferedReader
try {
FileInputStream fis = new FileInputStream("study.txt");
InputStreamReader isr = new InputStreamReader(fis,"GBK");
BufferedReader br = new BufferedReader(isr);
FileOutputStream fos = new FileOutputStream("study1.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
BufferedWriter bw = new BufferedWriter(osw);
int n=0;
char[] cbuf=new char[10];
while((n=br.read(cbuf))!=-1) {
// String s = new String(cbuf,0,n);
// osw.write(cbuf,0,n);
// osw.flush();
bw.write(cbuf,0,n);
bw.flush();
}
fis.close();
fos.close();
isr.close();
osw.close();
br.close();
bw.close();
}