第九天:IO流
File类
1.1 File概述
打开API,搜索File类。阅读其描述:File文件和目录路径名的抽象表示形式。即,Java中把文件或者目录(文件夹)都封装成File对象。也就是说如果我们要去操作硬盘上的文件,或者文件夹只要找到File这个类即可,那么我们就要研究研究File这个类中都有那些功能可以操作文件或者文件夹呢
我们对文件进行创建和删除操作。
IO:作用---是用来操作数据用的
内存(java程序)与硬盘之间进行读和写数据要使用一个媒介,我们称之为流。
通过数据的传输方式不一样,把流分为两类,分别:
输入流:把硬盘中的数据读到内存中来(把文件中的内容读到内存中)---导出
输出流:把内存中的数据写到硬盘中去(从程序中往文件中写数据)----导入
通过传输的数据类型不同,我们名义上可以把流分为:
字节流
字符流:字符流的底层还是字节流
字符通过码表转换为字节,字符=字节+码表
流的具体细分:
字节输入流:InputStream (抽象类不能直接实例化,可以使用多态,用实现类(子类))FileInputStream
字节输出流:OutputStream FileOutputStream
字符输入流:Reader FileReader
字符输出流:Writer FileWriter
字节流
1.从文件中读数据
第一种方式
例子:
package com.swlz.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class IODemo1 { public static void main(String[] args) throws IOException { //计算机只能识别二进制数0和1 //使用流进行进行传输数据的时候,最终都会转换为二进制数传输 // String s="詹栈";//把字符转换为二进制数这个过程称为编码 //使用字节输入流把文件中的数据读出来 //创建字节输入流对象 InputStream is=new FileInputStream("D:\\aaa\\e.txt"); //一次读一个字节 //从此输入流中读取一个字节数据 //如果达到文件数据的末尾没有数据了。则会返回-1,说明已经到文件的末尾了没有数据了 int read; while ((read=is.read())!=-1){//读到一个字节就强转为字符 System.out.print((char)read);//打印字符a 转换为打印字符a 强转类型 } // int read = is.read(); // System.out.println(read);//97 a在Ascall码表中的位置 // System.out.println((char)read);//打印字符a 转换为打印字符a 强转类型 } }
第二种方式:
package com.swlz.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class IODemo2 { public static void main(String[] args) throws IOException { //使用字节输入流一次读一个字节的第二中方式:读一个字节就把它存在字节数组中去 byte[] arr=new byte[1024];//字节数组 InputStream is=new FileInputStream("D:\\aaa\\e.txt"); // int read = is.read(); // byte b=(byte)read;//强转为byte类型 // arr[0]=b;//读到的字节放到字节数组中先存起来 int read; int i=0; while ((read=is.read())!=-1){ //读把读到的字节一个一个的存在字节数组中 arr[i]=(byte)read;//arr[0] arr[i] 读到末尾为止 i++; } // 把读到所有字节转为字符串 String s = new String(arr, 0, i);//0到i,从开始0到最后末尾i去读 读的是arr数组里面的字节 System.out.println(s);//打印字符串 } }
2.往文件中写数据
例子:
package com.swlz.io; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class IODemo3 { public static void main(String[] args) throws IOException { //往文件里面写数据 //创建流对象 要想在文件后面追加数据要append:true OutputStream os=new FileOutputStream("D:\\aaa\\y.txt",true); //要写入文件的数据 String s="zhanjuzhang"; //得到字节数组 一个字节一个字节的往文件写数据 byte[] bytes = s.getBytes(); //开始写数据 os.write(bytes); } }
3.扩展:文件的复制
package com.swlz.io; import java.io.*; public class IODemo4 { public static void main(String[] args) throws IOException { InputStream is=null; OutputStream os=null; try { //文件的复制 //创建输入流对象 读数据出来 is=new FileInputStream("D:\\aaa\\a.txt"); //创建输出流对象 写数据 写操作 os=new FileOutputStream("D:\\aaa\\d.txt"); //创建字节数据 暂时存储读出来的文件数据 字节全部存在这个字节数组中 int length; byte[] arr=new byte[1024]; // 边读 while ((length=is.read(arr))!=-1){ os.write(arr,0,length);//边写 } } catch (IOException e) { e.printStackTrace(); } finally { //释放资源 //在不为null情况,释放资源 if(is!=null) { is.close(); } if(is!=null) { os.close(); } } } }
记得关闭流,释放资源。
2.字符流
缓冲区就在内存中
缓冲区的大小只有8kb
超出8kb就会溢出去
2.1使用字符输入流读数据
例子:
public class ReaderDemo { public static void main(String[] args) throws Exception{ /* 使用字符输入流读数据 Reader---FileReader * */ //创建字符输入流读对象 Reader rd=new FileReader("D:\\b.txt"); //一次读一个字符 /* int read = rd.read(); System.out.println((char)read);*/ /*int read; while ((read=rd.read())!=-1){ System.out.print((char)read); }*/ //一次读一个字符第二种方式 /* char[] chs=new char[1024]; int i=0; int read; while((read=rd.read())!=-1){ chs[i]=(char)read; i++; } String s = new String(chs, 0, i); System.out.println(s);*/ //读数据的第三种方式---一次读一个字符数组 char[] chs=new char[1024]; int length; while((length=rd.read(chs))!=-1){ String s = new String(chs, 0, length); System.out.println(s); } } }
2.2 使用字符输出流写数据
public class WriterDemo { public static void main(String[] args) throws Exception{ /*使用字符输出流写数据 * Writer-----FileWriter * */ //创建字符输出流对象 Writer wt=new FileWriter("a.txt"); String s="wobuaini"; /* char[] chs = s.toCharArray(); //直接写字符数组 wt.write(chs);*/ //第二种方式---直接写字符串 wt.write(s); // wt.flush();---刷新缓冲区 //字符流关流的操作 具有刷新缓冲区的功能 wt.close(); } }
2.3 使用字符流实现文件复制:
代码如下:
public class CopyDemo { public static void main(String[] args) throws Exception{ /*使用字符串进行文件的复制*/ //创建字符输入流对象 Reader rd=new FileReader("D:\\CC.txt"); //创建字符输出流对象 Writer wt=new FileWriter("a.txt"); char[] chs=new char[1024]; int length; while((length=rd.read(chs))!=-1){ wt.write(chs,0,length); } rd.close(); wt.close(); } }
3.转换流
1.1 基本概述
转换流是字节流通向字符流的一个桥梁
转换流能够将我们的字节流转换成字符流,在操作一些文本文件的时候,能够以字符流的形式去使用此流
1. InputStreamReader :字节输入流-----字符输入流
能够完成字节输入流到字符输入流的转换
public class InputStreamReaderDemo { public static void main(String[] args) throws Exception{ InputStream is=new FileInputStream("a.txt"); //把给定的字节流转换成字符流 --转换流可以解决中文乱码问题 InputStreamReader isr=new InputStreamReader(is); char[] chs=new char[1024]; int length; while((length=isr.read(chs))!=-1){ String s = new String(chs, 0, length); System.out.println(s); } isr.close(); } }
2.OutputStreamWriter
是字符流通向字节流的桥梁
OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的charset将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集
例子:
public static void main(String[] args) throws Exception{ /*转换流:------字节流转换成字符流 * * 只有一个字节输入流inputStream,使用的是字符流操作文本文件 * */ // method1(); //创建字节输出流 OutputStream os=new FileOutputStream("a.txt"); //创建转换流对象 OutputStreamWriter osw=new OutputStreamWriter(os); //写数据 osw.write("nihao,shuaige"); osw.close(); }
4.缓冲流
经过分析我们发现,无论是字节流还是字符流,在操作数据的时候,都是一个字节一个字节(一个字符一个字符的操作),每一次操作都需要和硬件资源打交道
使用缓冲流可以解决这个问题
缓冲流会自带缓冲区(内存中一块区域),从磁盘获取或者写入数据的时候,能够一次性获取过个数据,以后再通过流去操作的时候,就是直接从缓冲区中获取数据了,从而能够减少内存与硬盘交互的次数,提高性能
1.使用缓冲流可以解决这个问题
缓冲流会自带缓冲区(内存中一块区域),从磁盘获取或者写入数据的时候,能够一次性获取过个数据,以后再通过流去操作的时候,就是直接从缓冲区中获取数据了,从而能够减少内存与硬盘交互的次数,提高性能
1 字节流
InputStream BufferedInputStream
OutputStream BufferedOutputStream
1. BufferedInputStream
例子:
public class BufferedInputSteamDemo1 { public static void main(String[] args) throws Exception{ //创建字节输入缓冲流对象 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\b.txt")); // 虽然看上去还是一个字节一个字节读,但是先放到缓冲区,然后一次性读出来 /* int read; while ((read=bis.read())!=-1){ System.out.print((char)read); }*/ byte[] arr=new byte[1024]; int length; while((length=bis.read(arr))!=-1){ String s = new String(arr, 0, length); System.out.println(s); } } }
2.BufferedOutputStream
例子:
public class BufferedOutPutStreamDemo { public static void main(String[] args) throws Exception{ //创建字节输出缓冲流对象 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a.txt")); //写数据 bos.write("hello".getBytes()); //字节输出缓冲流写数据的时候一定要记得刷新,因为使用缓冲区比之前的普通流多了一个缓冲区 bos.close(); } }
3.BufferedReader
例子:
public class BufferedReaderDemo1 { public static void main(String[] args) throws Exception{ /*缓冲流(高效率)四个 Reader BufferedReader Writer BufferedWriter * InputStream BufferedInputStream OutputStream BufferedOutputStream * * */ //创建字符输入流对象 Reader rd=new FileReader("d:\\b.txt"); //创建字符输入缓冲流对象 /*BufferedReader(Reader in) 创建一个使用默认大小输入缓冲区的缓冲字符输入流。*/ BufferedReader br=new BufferedReader(rd); /*如果已到达流末尾,则返回 null */ String s; while ((s=br.readLine())!=null){ System.out.println(s); } //关闭了外层流相当于一起把内层流也关闭 br.close(); /*String s = br.readLine(); System.out.println(s); String s1 = br.readLine(); System.out.println(s1); String s2 = br.readLine(); System.out.println(s2); String s3 = br.readLine(); System.out.println(s3);//null是readLine()的临界点*/ } }
4.BufferedWriter 例子:
public class BufferedWriterDemo1 { public static void main(String[] args) throws Exception{ /*创建字符输出缓冲流对象*/ BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt")); //写数据 bw.write("nihao"); // bw.write("\r\n"); //换行的方法 bw.newLine();//牛栏 bw.write("java"); //记得刷新 bw.close(); } }
3.文件复制:
public class BufferedCopyDemo{ public static void main(String[] args) throws Exception{ //创建字符输入缓冲流对象 BufferedReader br = new BufferedReader(new FileReader("d:\\b.txt")); //创建字符输出缓冲流对象 BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\bb.txt")); //边读编写 String s; while((s=br.readLine())!=null){ bw.write(s); //每写一行newline()一下。 bw.newLine(); } //关流 br.close(); bw.close(); } }
总结:
1.IO Input(输入) Output(输出) 流
IO流分类:
1. 流向
输入流 读取数据 FileReader Reader
输出流 写出数据 FileWriter Writer
数据类型
2. 字节流
字节输入流 读取数据 InputStream
字节输出流 写出数据 OutputStream
3. 字符流
字符输入流 读取数据 Reader
字符输出流 写出数据 Writer
4.转换流
5.缓存流