城管希课堂之流
字节流 又叫万能流
以字节为单位 进行数据的传输
流的参照物(程序)
输出流
程序 --> 文件 (写文件使用输出流)
输入流
文件 --> 程序 (读文件使用输入流)
OutputStream 字节输出流
InputStream 字节输入流
以上两个类 是所有字节流的父类
写文件步骤
1.创建要绑定的文件
2.创建输出流 并绑定文件
3.写文件
4.关闭流资源
//创建文件字节输出流//创建file(给输出流绑定一个输出文件)//给出的路径 可以没有该文件 系统会帮你创建出来File file = new File("/Users/lanou/Desktop/test/wwww.txt");FileOutputStream fos = new FileOutputStream(file); // 写文件// 注意 : 流这块 全是抛的IOException// 传入int值的方法 是按 ASCII码输入// 一次写入一个字节fos.write(49);fos.write(48);fos.write(48);fos.write(65);// 创建一个字节数组byte[]b = {66,67,68,69};fos.write(b);// 直接写字符串// 字符串转字节数组 getBytes()// 字节数组转字符串 字符串的构造方法fos.write("wanglong".getBytes());fos.write(b,1,2);// 关闭资源fos.close();
字符流
FIleWriter 写文件 字符输出流
Reader 读文件 字符输入流
转换流
OutputStreamWriter (字符流转向字节流的桥梁)
1.程序中写入字符时 先使用转换流 根据转换流想查询码表格式去查询
2.如果查的是GBK格式 那么一个中文字符 就查到两个字节的 字节编码
3.这个字节最后给到了 构建转换流时 传入的字节流
4.通过这个字节流 按字节写入到文件中
InputStreamReader(读取文件 字节流通向字符流的桥梁)
1.按字节读取文件 将字节编码给到转换流
2.如果是UTF-8的 需要三个字节
3.使用字符流读取到程序中
public static void getUTF8() throws IOException{// 创建一个字节输出流FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test/UTF8.txt");// 创建一个转换流OutputStreamWriter osw = new OutputStreamWriter(fos);// 写文件osw.write("刘斌");// 注意:// 1.一般只关 外层的流就可以// 2.自己创建的流自己关 获取系统的流 不用管osw.close();}//按GBK 格式写入文件 还写刘斌public static void getGBK() throws IOException{FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test/GBK.txt");OutputStreamWriter osw = new OutputStreamWriter(fos, "gbk");osw.write("刘斌");osw.close();}// 按UTF8格式读文件// 按GBK格式读文件public static void setUTF8() throws IOException{FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/test/UTF8.txt");InputStreamReader isr = new InputStreamReader(fis,"gbk");int num2=0;char[]b = new char[1024];while ((num2 = isr.read(b))!=-1) {//字符数组 转字符串System.out.println(new String(b,0,num2));}isr.close();}
流 就是用来读写文件的 ----效率问题
缓冲流(高效流)
内部自带一个缓冲区(字节数组)
BufferedOutputStream(写文件)缓冲字节数据流
BufferedInputStream(读文件)缓冲字节输入流
abstract class test{//声明复制文件路径成员变量public String src = "/Users/lanou/Desktop/test/头号玩家_bd.mp4";public String dest = "/Users/lanou/Desktop/test/1235555456.mp4";public void printTime() throws IOException{long start = System.currentTimeMillis();System.out.println(start);//要计算的程序copyFile();long end = System.currentTimeMillis(); System.out.println(end-start);}//抽象方法//注意 异常在继承中的使用public abstract void copyFile() throws IOException;
}// 字节文件流复制文件测试
class copy1 extends test{@Overridepublic void copyFile() throws IOException{// 一个一个读FileInputStream fis = new FileInputStream(src);FileOutputStream fos = new FileOutputStream(dest);//读int num = 0;while ((num=fis.read())!=-1) {fos.write(num); }fis.close();fos.close();}
//把3个文件合并成一个文件public static void merge3() throws IOException{File file1 = new File("F:\\a.txt");File file2 = new File("F:\\b.txt");File file3 = new File("F:\\c.txt");File file4 = new File("F:\\d.txt");//建立对应的输入输出流对象FileOutputStream fileOutputStream = new FileOutputStream(file4);FileInputStream fileInputStream1 = new FileInputStream(file1);FileInputStream fileInputStream2 = new FileInputStream(file2);FileInputStream fileInputStream3 = new FileInputStream(file3); //创建序列流对象Vector vector = new Vector();vector.add(fileInputStream1);vector.add(fileInputStream2);vector.add(fileInputStream3);Enumeration e = vector.elements();SequenceInputStream sequenceInputStream = new SequenceInputStream(e);//读取文件数据byte[] buf = new byte[1024];int length = 0;while((length = sequenceInputStream.read(buf))!=-1){fileOutputStream.write(buf, 0, length);}sequenceInputStream.close();fileOutputStream.close();}//SequenceInputStream合并文件夹public static void meege2() throws IOException{File inFile1 = new File("F:\\a.txt");File inFile2 = new File("F:\\b.txt");File outFile = new File("F:\\c.txt"); FileOutputStream fileOutputStream = new FileOutputStream(outFile);FileInputStream fileInputStream1 = new FileInputStream(inFile1);FileInputStream fileInputStream2 = new FileInputStream(inFile2); //建立序列流对象SequenceInputStream inputStream = new SequenceInputStream(fileInputStream1, fileInputStream2);byte[] buf = new byte[1024];int len=0;while((len = inputStream.read(buf))!=-1){fileOutputStream.write(buf, 0, len);}inputStream.close();fileOutputStream.close();}//合并a.txt和b.txt内容public static void merge1() throws IOException{File inFile1 = new File("F:\\a.txt");File inFile2 = new File("F:\\b.txt");File outFile = new File("F:\\c.txt");FileOutputStream fileOutputStream = new FileOutputStream(outFile);FileInputStream fileInputStream1 = new FileInputStream(inFile1);FileInputStream fileInputStream2 = new FileInputStream(inFile2);ArrayList list = new ArrayList();list.add(fileInputStream1);list.add(fileInputStream2);byte[] buf = new byte[1024];int length = 0;for(int i=0;i
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
