城管希课堂之流

字节流  又叫万能流

以字节为单位 进行数据的传输

流的参照物(程序)

输出流

程序 --> 文件 (写文件使用输出流)

输入流

文件 --> 程序 (读文件使用输入流)


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{

	@Override
	public 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<FileInputStream> vector = new Vector<FileInputStream>();

      vector.add(fileInputStream1);

      vector.add(fileInputStream2);

      vector.add(fileInputStream3);

      Enumeration<FileInputStream> 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<FileInputStream> list = new ArrayList<FileInputStream>();

      list.add(fileInputStream1);

      list.add(fileInputStream2);

      byte[] buf = new byte[1024];

      int length = 0;

      for(int i=0;i<list.size();i++){

        FileInputStream fileInputStream = list.get(i);

        while((length = fileInputStream.read(buf))!=-1){

           fileOutputStream.write(buf, 0, length);

        }

        fileInputStream.close();

      }

      fileOutputStream.close();

   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值