字符流
InputStreamReader和OutputStreamWriter
- InputStreamReader
构造方法:
InputStreamReader(InputStream is):用默认的编码(GBK)读取数据
InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
读数据:
public int read() 一次读取一个字符
public int read(char[] cbuf) 一次读取一个字符数组 如果没有读到 返回-1
InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
int ch = in.read(); //一次读取一个字符
System.out.println(ch);
blic static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
char[] chars=new char[1024];//字符数组,充当缓冲区
int len = in.read(chars); //返回值,是实际读取到的字符个数
InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
char[] chars = new char[1024];//字符数组,充当缓冲区
int len = in.read(chars, 0, 3); //一次读取3个字符,装入缓冲区中
System.out.println(len);
String s = String.valueOf(chars, 0, len);
System.out.println(s);
in.close();
- OutputStreamWriter
构造方法:
OutputStreamWriter(OutputStream out):根据默认编码(GBK)把字节流的数据转换为字符流
OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流
OutputStreamWriter out = out=new OutputStreamWriter(new FileOutputStream("a.txt",),"utf-8");
写数据:
public void write(int c) 写一个字符
public void write(char[] cbuf) 写一个字符数组
public void write(char[] cbuf,int off,int len) 写一个字符数组的 一部分
public void write(String str) 写一个字符串
public void write(String str,int off,int len) 写一个字符串的一部分
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("f.txt"));
out.write("我很快乐");
out.flush();
out.write("我很快乐");
out.flush();
out.close();//刷新并关闭
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("MyTest2.java"));
char[] chars = new char[1000];
out.write(chars,0,len);
out.flush();//字符流记得刷新一下
out.close();
使用InputStreamReader和OutputStreamWriter两种方式复制文件
一次复制一个字符
InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("MyTest2.java"));
//读一个字符,写一个字符来复制
int len=0;//用来记录你读取到的那个字符
while ((len=in.read())!=-1){
out.write(len);
out.flush();//字符流记得刷新一下
}
in.close();
out.close(); //关闭并刷新
一次读写一个字符数组
InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("MyTest2.java"));
char[] chars = new char[1000];
int len=0;//读取到有效字符个数
while ((len=in.read(chars))!=-1){
out.write(chars,0,len);
out.flush();//字符流记得刷新一下
}
in.close();
out.close();
FileReader和FileWriter
转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,
所以,为了简化我们的书写,转换流提供了对应的子类。它们的构造方法如下
以复制文件为例,使用这一对流来演示
FileReader in = new FileReader("src/MyTest.java");
FileWriter out = new FileWriter("MyTest3.java");
char[] chars=new char[1000];
int len=0;
while ((len=in.read(chars))!=-1){
//System.out.println("读取的次数"+len);
out.write(chars,0,len);
out.flush();
}
in.close();
out.close();
高效字符流BufferedReader和BufferedWriter
高效的字符流
高效的字符输出流: BufferedWriter
构造方法: public BufferedWriter(Writer w)
高效的字符输入流: BufferedReader
构造方法: public BufferedReader(Reader e)
除了有父类继承的方法外,它还有特殊的方法
BufferedWriter: public void newLine():根据系统来决定换行符 具有系统兼容性的换行符
BufferedReader: public String readLine():一次读取一行数据 是以换行符为标记的 读到换行符就换行 没读到数据返回null
下面使用这一对流对文件进行复制
BufferedReader bfr = new BufferedReader(new FileReader("回顾.java"));
BufferedWriter bfw = new BufferedWriter(new FileWriter("aaa.java"));
String line=null;
while ((line=bfr.readLine())!=null){
bfw.write(line);
bfw.newLine(); //写出一个换行符,具有平台兼容性
bfw.flush();
}
bfr.close();
bfw.close();