一、IO流的概念和作用:流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称和抽象。(涉及到数据传输都是流)
二、IO流的分类
1.根据处理数据类型的不同分为:字符流和字节流
2.根据数据流向不同分为:输入流和输出流
3.根据同数据源之间的直接关系分为:节点流(直接打交道)和处理流(缓冲流、包装流)
三、字节流和字符流
1.字符流的由来:因为数据编码不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。
2.字节流和字符流的区别:
(1)读写单位:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。(GBK-2个字节,UTF-8-3个字节)
(2)处理对象不同:字节流能处理所有类型的数据(如图片。avi等),而字符流只能处理字符类型的数据。
(3)字节流:一次读入或读出是8位二进制。
(4)字符流:一次读入或读出是16/24位二进制。//所有字符输入流的基类:Reader
//字符流和字节流的基础
public class DemoA {
//字符流的读取
public static void a(){
//所有字符输入流的基类:Reader
//因为Reader是抽象类,想要实例化通过FileReader子类来进行
//FileReader类:文件读取类,需要提供被读取的文件
try{
File file = new File("E://1","a.txt");
Reader reader = new FileReader(file);
char[] chars = new char[10];
/*int len = reader.read(chars);//将内容读出来写入chars数组中 len:实际读了多少个
String str = new String(chars,0,len);//从0开始取,取len个*/
int len;
while((len=reader.read(chars))!=-1){
String str = new String(chars,0,len);
System.out.println(str);
}
reader.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
a();
}
}
//字节流的读取
public static void b(){
//InputStream:是所有字节读取流的基类
try{
File file = new File("E://1//a.txt");
InputStream in = new FileInputStream(file);
byte[] bytes = new byte[10];//如果刚好取的汉字的字节大于字节数组长度,
// 会出现乱码情况,这也是不用字节流表示字符的原因
int len = in.read(bytes);
String str = new String(bytes,0,len);
System.out.println(str);
in.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
//返回这个输入流中可以被读的剩下的bytes字节的估计值;
// int size = fis.available() ;
//字符流的写入操作
public static void c(){
try{
File file = new File("E://1//a.txt");
Writer writer = new FileWriter(file);
String str = " aaasssd";
writer.write(str);
writer.flush();//刷新 想要立即写入就要写,要不就不会写入
writer.close();//关闭后也会刷新
}catch(Exception e){
System.out.println(e.getMessage());
}
}
//字节流的写入操作
public static void d(){
try{
File file = new File("E://1//a.txt");
OutputStream out = new FileOutputStream(file,true);//追加,不覆盖
String str = new String("asfsddd");
byte[] bytes = str.getBytes();
out.write(bytes);
out.flush();
out.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
六、节点流和处理流
1.节点流:直接与数据源相连的流,读取或者写入。
(1)直接使用节点流,读写不方便,为了更快的读写文件,才有了处理流。
(2)
2.常用的节点流
(1)父类:InputStream、OutputStream、Reader、Writer
(2)文件:FileInputStream、FileOutputStream、FileReader、FileWriter文件进行处理的节点流
(3)数组:ByteArrayInputStream、ByteArrayOutputStream、CharArrayReader、CharArrayWriter对数组进行处理的节点流(对应的不再是文件,而是内存中的一个数组)
(4)字符串:StringReader、StringWriter对字符串进行处理的节点流
(5)管道:PipedInputStream、PipedOutputStream、PipedReader、PipedWriter对管道进行处理的节点流
3.处理流:处理流和节点流一块使用,在节点流的基础上,再套接一层,套接在节点流上的就是处理流。(处理流是建立在节点流的基础上,处理流不能单独使用,是为了读取和写去效率的) 处理流的构造方法总是要带一个其他的流对象做参数。一个流对象经过其他流的多次包装,称为流的链接。
(1)常用的处理流:
①缓冲流:BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter增加缓冲功能,避免频繁读写硬盘。
②转换流:InputStreamReader、OutputStreamReader实现字节流和字符流之间的转换。
③数据流:DataInputStream、DataOutputStream等提供将基础数据类型写入到文件中,或者读取出来。
//缓冲流和转换流
public class DemoB {
public static void a() throws IOException {
File file = new File("E://1//a.txt");
}
public static void b() throws IOException{
File file = new File("E://1//a.txt");
Writer writer = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(writer);
bw.write("写入的内容");
bw.newLine();
bw.flush();
bw.close();
writer.close();
}
public static void c() throws IOException{//没有readline()
File file = new File("E://1//a.txt");
InputStream in = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(in);
byte[] bytes = new byte[10];
int len = bis.read(bytes);//放在内存中 刷新时写入 in. 直接写进去了
String str = new String(bytes,0,len);
System.out.println(str);
bis.close();
in.close();
}
public static void d() throws IOException{//没有readline()
File file = new File("E://1//a.txt");
OutputStream in = new FileOutputStream(file);
BufferedOutputStream bis = new BufferedOutputStream(in);
byte[] bytes = new byte[10];
bis.write(bytes);//写入到缓冲区 没有直接写入
bis.flush();
bis.close();
in.close();
}
//将输入字节流转换成字符流
public static void e() throws IOException{
File file = new File("E://1//a.txt");
InputStream in = new FileInputStream(file);
Reader reader = new InputStreamReader(in);
}
//将输出字节流转换成字符流
public static void f() throws IOException{
File file = new File("E://1//a.txt");
OutputStream in = new FileOutputStream(file);
Writer writer = new OutputStreamWriter(in);
//代码演示
public class DemoA {
public static void 输入(){
try{
//要有一个文件
File file = new File("E://1","a.txt");
//判断文件是否存在
boolean b = file.exists();
if(b){//单向通道 只能从文件往Java走
//建立Java和文件之间的输入流(读取流)通道
Reader reader = new FileReader(file);
//建立一个缓冲输入流
BufferedReader br = new BufferedReader(reader);
//读取内容(一行一行的读取)
String line = br.readLine();
System.out.println(line);
//读取全部内容
String line2;
while((line2=br.readLine())!=null){
System.out.println(line2);
}
//关闭操作 倒着关
br.close();
reader.close();
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static void 输出(){//Java往文件里走
try{
//要有一个文件
File file = new File("E://1","a.txt");
//建立一个Java同文件之间的输出流
Writer writer = new FileWriter(file,true);//默认为false,覆盖;改为true,不覆盖;
//建立一个输出缓冲流
BufferedWriter bw = new BufferedWriter(writer);
bw.write("aaaaaab");
bw.newLine();//换行 或者bw.write("aaaaaaa\r\n");//记事本 写字板、world \n已经换了
for (int i = 0; i < 10; i++) {
bw.write("aaaaa\r\n");
}
bw.close();
writer.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static void 复制(){
try{
File file1 = new File("E://1","a.txt");
File file2 = new File("E://2",file1.getName());
Reader in = new FileReader(file1);
Writer out = new FileWriter(file2);
BufferedReader br = new BufferedReader(in);
BufferedWriter bw = new BufferedWriter(out);
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
out.close();
in.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static void File(){
File file = new File("D://asd");//创建一个File对象
String str = " ";
File[] nums = file.listFiles();//file对象的所有子目录
print(str,nums);//输出所有文件
}
public static void print(String str,File[] files){
if(files == null) {
return;
}
else{
for (int i = 0; i < files.length; i++) {
System.out.println(str+files[i].getName());
print(str+" ",files[i].listFiles());
}
}
}
public static void main(String[] args){
File();
}
}