Reader(字符输入流,抽象类,不能直接创建对象)的直接子类是InputStreamReader(字节转换流–桥梁)的直接子类是FileReader,相当于FileReader是Reader的间接子类
Writer–>OutputStreamWriter–>FileWriter,相当于FileWriter是Writer的间接子类
注意
1.输出流会在默认路径自动创建文件对象
2.使用字符输出流往文件写数据的时候,还要有一个刷新的过程
3.关流的close方法自带刷新的效果,如果忘记写了flush(),而写了close(),也是能够将字符写进文本文件中
字符输入流
package com.bianyiit.cast;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class ZiFuIO1 {
public static void main(String[] args) throws IOException {
//字符流:字符输入流(读数据)FileReader
Reader rd = new FileReader("D:\\a.txt");
/*int read = rd.read();
System.out.println((char)read);*/
char[] arr=new char[2];
int length;
while((length=rd.read(arr))!=-1){
String s = new String(arr, 0, length);
System.out.print(s);
}
}
}
字符输出流
//字符流:字符输出流(写数据)FileWriter
package com.bianyiit.cast;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class ZiFuIO2 {
public static void main(String[] args) throws IOException {
//字符输出流(写数据)Writer-->OutputStreamWriter-->FileWriter
//创建字符输出流对象
Writer wt = new FileWriter("D:\\b.txt");
//输出流会在默认路径自动创建文件对象
String s="世界你好!!";
//把字符串转换成字符数组
char[] chs = s.toCharArray();
//把字符数组写入文本文件中
wt.write(chs); //没写进去
//直接将字符串写入文本文件中
wt.write(s); //同样没写进去
//使用字符输出流往文件写数据的时候,还要有一个刷新的过程
wt.flush(); //刷新该流的缓冲
//关流的close方法自带刷新的效果,如果忘记写了flush(),而写了close(),能够写进文本文件中
}
}
利用字符流对文本进行复制
package com.bianyiit.cast;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ZiFuIO3 {
public static void main(String[] args) throws IOException {
//使用字符流进行文件的复制
//close()默认有刷新方法flush(),而使用jdk7的新特性,会默认有close()
try(
FileReader rd = new FileReader("D:\\a.txt");
FileWriter fw = new FileWriter("D:\\k.txt")){;
char[] arr=new char[1024];
int length;
while((length=rd.read(arr))!=-1){
fw.write(arr,0,length);
}
}
}
}
注意:不要用字符流去操作图片,视频等,会导致内容无法显示,因为编码错误
转换流
InputStreamReader 字节输入转换流 字节流–字符流
package com.bianyiit.cast;
import java.io.*;
public class ZiFuIO4 {
public static void main(String[] args) throws IOException {
//需求:只给了字节流对象,但是规定要使用字符流操作文本文件
//需要使用转换流 InputStreamReader 将字节输入流到字符输入流的转换
InputStream is = new FileInputStream("D:\\a.txt");
InputStreamReader isr = new InputStreamReader(is);
char[] arr=new char[1024];
int length;
while((length=isr.read(arr))!=-1){
String s=new String(arr,0,length);
System.out.println(s);
}
}
}
OutputStreamWriter 字节输出转换流 字符流–字节流
package com.bianyiit.cast;
import java.io.*;
public class ZhuanHuanLiuDemo2 {
public static void main(String[] args) throws IOException {
//InputStreamReader 字节输入转换流 字节流--字符流
//OutputStreamWriter 字节输出转换流 字符流--字节流
//转换流不是两个流角色发生改变,而是输入和输出的时候用到了两个不同的流,这两个流之间需要一根桥梁搭建起来
InputStream is = new FileInputStream("D:\\a.txt");
InputStreamReader isr = new InputStreamReader(is);
//isr调用的是字符流的方法
FileOutputStream fs = new FileOutputStream("D:\\b.txt");
OutputStreamWriter osw = new OutputStreamWriter(fs);
//osw调用还是字符流的方法
//流的转换不是真正的字节流变成了字符流,也不是字符流变成字节流
//假设输出的时候是字节流,而输入的时候是字符流
//假设输出的时候是字符流,而输入的时候是字节流
}
}
缓冲流
字节缓冲输入流 BufferedInputStream
package com.bianyiit.cast;
import java.io.*;
import java.nio.Buffer;
public class BufferDemo1 {
public static void main(String[] args) throws IOException {
//使用缓冲流的原因:1.减少对硬盘的访问次数;2.提高了读写的效率
InputStream is = new FileInputStream("D:\\a1.txt");
BufferedInputStream bis = new BufferedInputStream(is);
/*int read = bis.read(); //这里一次还是读的是一个字节
System.out.println((char)read);*/
//method(bis);
//一次读一个数组
byte[] arr=new byte[1024];
int length;
while((length=bis.read(arr))!=-1){
String s = new String(arr, 0, length);
System.out.println(s);
}
}
private static void method(BufferedInputStream bis) throws IOException {
byte[] arr=new byte[1024];
int read;
int i=0;
while((read=bis.read())!=-1){
byte b=(byte)read;
arr[i]=b;
i++;
}
String s = new String(arr, 0, i);
System.out.println(s);
//数组的长度只有1k,如果txt里面的数据超过1k,就会报数组越界异常
}
}
注意:使用第二种方法,当数组的长度只有1k,如果txt里面的数据超过1k,就会报数组越界异常
字节缓冲输出流 BufferedOutputStream
package com.bianyiit.cast;
import java.io.*;
public class BufferDemo2 {
public static void main(String[] args) throws IOException {
//字节输出缓冲流
OutputStream os = new FileOutputStream("D:\\a1.txt");
BufferedOutputStream bos = new BufferedOutputStream(os);
//字符串-->字节数组
bos.write("hello".getBytes());
bos.flush();//记得要刷新
bos.close();
}
}
字符缓冲输入流 BufferedReader
package com.bianyiit.cast;
import java.io.*;
public class BufferDemo3 {
public static void main(String[] args) throws IOException {
//字符缓冲流
Reader rs = new FileReader("D:\\b.txt");
BufferedReader br = new BufferedReader(rs);
/*char[] arr=new char[1024];
int length;
while((length=br.read(arr))!=-1){
String s = new String(arr, 0, length);
System.out.println(s);
}*/
//一次读一行(字符缓冲流特有的方法readline())
String s;
while((s=br.readLine())!=null){
//可以将后面的空字节全部读出来
System.out.println(s);
}
}
}
字符输出缓冲流 BufferedWriter
package com.bianyiit.cast;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferDemo4 {
public static void main(String[] args) throws IOException {
//字符输出缓冲流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\b.txt",true));
bw.write("打开一个文件夹!!");
//BufferWriter特有的功能:newline()进行换行
bw.newLine();
bw.write("我是一个superman");
bw.flush(); //需要刷新一下
bw.close();
}
}