2021-04-19

一,写出流
–1,概述
是指 从 Java程序 写出到 磁盘中 的过程
根据写出 数据的格式不同,,可以采用字符流 字节流
字符流 只能写出 字符数据
字节流 可以 写出各种类型的数据 --重点!!
读取工具类:InputStream - FileInputStream - BufferedInputStream
写出工具类:OutputStream - FileOutputStream - BufferedOutputStream
–2,工具类
- OutputStream :是父类,被修饰成抽象类,不能new,学共性的方法
void close()
关闭此输出流并释放与此流有关的所有系统资源。
void flush()
刷新此输出流并强制写出所有缓冲的输出字节。
void write(byte[] b)
将 b.length 个字节从指定的 byte 数组写入此输出流。
void write(byte[] b, int off, int len)
将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流
abstract void write(int b)
- FileOutputStream :是子类,研究new
FileOutputStream(String name)
创建一个向具有指定名称的文件中写入数据的输出文件流。
FileOutputStream(String name, boolean append)
创建一个向具有指定 name 的文件中写入数据的输出文件流。
- BufferedOutputStream :是子类,研究new
BufferedOutputStream(OutputStream out)
创建一个新的缓冲输出流,以将数据写入指定的底层输出流。

--3,测试
	package cn.tedu.io;

	import java.io.*;
	//测试 字节流写出
	//总结:
	//1,效率上来讲谁快? BufferedOutputStream > FileOutputStream
	//2,为什么?BufferedOutputStream底层维护了一个数组byte[] buf:8192,
	    //可以缓冲一批数据,实现了批量写出,提高一个一个写的效率
	public class Test2 {
	    public static void main(String[] args) throws IOException {
	//        method();//FileOutputStream
	        method2();//BufferedOutputStream
	    }
	    //BufferedOutputStream
	    public static void method2() throws IOException {
	        //1,创建对象
	        OutputStream out = new BufferedOutputStream(
	//                            new FileOutputStream("E:\\iotest\\1.txt") );//数据覆盖
	                              //数据追加
	                            new FileOutputStream("E:\\iotest\\1.txt",true));
	        //2,调用方法写出
	        out.write(90);
	        //3,关闭资源
	        out.flush();//把数据从out流里,刷出到磁盘里
	//        out.close();//flush+close
	//        out.write(90);
	    }
	    //FileOutputStream
	    public static void method() throws IOException {
	        //1,创建对象--参数是文件的路径
	        OutputStream out = new FileOutputStream("E:\\iotest\\1.txt",true);
	        //2,调用方法写出
	        out.write(97);//一个一个的写出
	        out.write(98);
	        out.write(99);
	        out.write(100);

	        byte[] bs = {50,51,52};
	        out.write(bs);//一个数组里的数据都写出
	        //3,关闭资源
	        out.close();
	// close()之后不能继续写出,否则抛出异常:Stream Closed
	//        out.write(100);
	    }
	}

二,IO综合练习
–1,文件复制
package cn.tedu.io;

	import java.io.*;
	import java.util.Scanner;

	//复制文件
	public class Test3 {
	    public static void main(String[] args)  throws IOException {
	        String from = new Scanner(System.in).nextLine();//1,源文件
	        String to = new Scanner(System.in).nextLine(); //2,目标文件
	        copyOf(from,to); //3,复制
	        System.out.println("文件复制完成...");
	    }
	    //复制文件
	    public static void copyOf(String from, String to) throws IOException {
	        //1,读取from文件
	    InputStream in = new BufferedInputStream(new FileInputStream(from));
	        //2,写出to文件
	    OutputStream out = new BufferedOutputStream(new FileOutputStream(to));
	        //3,读一个写一个
	        int b = 0;//定义变量,记录读到的数据

	        long start = System.currentTimeMillis();//计时开始ms
	        while( ( b = in.read() ) != -1){//一个一个的读,没数据时返回了-1
	            out.write(b);//写出去
	        }
	        long end = System.currentTimeMillis();//计时结束ms
	        System.out.println(end-start);//97ms->2ms

	        //4,关闭资源
	        in.close();
	        out.close();
	    }
	}

--2,批量读写
	package cn.tedu.io;

	import java.io.*;
	import java.util.Scanner;

	//复制文件
	public class Test3 {
	    public static void main(String[] args)  throws IOException {
	        String from = new Scanner(System.in).nextLine();//1,源文件
	        String to = new Scanner(System.in).nextLine(); //2,目标文件
	        copyOf(from,to); //3,复制
	        System.out.println("文件复制完成...");
	    }
	    //复制文件
	    public static void copyOf(String from, String to) throws IOException {
	        //1,读取from文件
	    InputStream in = new BufferedInputStream(new FileInputStream(from));
	        //2,写出to文件
	    OutputStream out = new BufferedOutputStream(new FileOutputStream(to));
	        //3,读一个写一个
	        int b = 0;//定义变量,记录读到的数据

	        long start = System.currentTimeMillis();//计时开始ms
	//        while( ( b = in.read() ) != -1){//一个一个的读,没数据时返回了-1
	//            out.write(b);//写出去
	//        }
	        byte[] buf = new byte[8192];//模拟底层的数组容量8K
	        while( ( b = in.read(buf) ) != -1){//一个数组一个数组的读
	            out.write(buf);//一个数组一个数组的写
	        }
	        long end = System.currentTimeMillis();//计时结束ms
	        System.out.println(end-start);//97ms->2ms->0ms

	        //4,关闭资源
	        in.close();
	        out.close();
	    }
	}

--3,关闭资源
	package cn.tedu.io;

	import java.io.*;
	import java.util.Scanner;

	//复制文件
	public class Test3 {
	    public static void main(String[] args)  throws IOException {
	        String from = new Scanner(System.in).nextLine();//1,源文件
	        String to = new Scanner(System.in).nextLine(); //2,目标文件
	        copyOf(from,to); //3,复制
	        System.out.println("文件复制完成...");
	    }
	    //复制文件
	    public static void copyOf(String from, String to)  {
	        InputStream in = null;//为了finally也能用
	        OutputStream out = null;//为了finally也能用
	        try{
	            //1,读取from文件
	            in = new BufferedInputStream(new FileInputStream(from));
	            //2,写出to文件
	            out = new BufferedOutputStream(new FileOutputStream(to));
	            //3,读一个写一个
	            int b = 0;//定义变量,记录读到的数据

	            long start = System.currentTimeMillis();//计时开始ms
	    //        while( ( b = in.read() ) != -1){//一个一个的读,没数据时返回了-1
	    //            out.write(b);//写出去
	    //        }
	            byte[] buf = new byte[8192];//模拟底层的数组容量8K
	            while( ( b = in.read(buf) ) != -1){//一个数组一个数组的读
	                out.write(buf);//一个数组一个数组的写
	            }
	            long end = System.currentTimeMillis();//计时结束ms
	            System.out.println(end-start);//97ms->2ms->0ms
	        }catch (Exception e){
	            e.printStackTrace();
	        }finally {
	            //TODO 改造 -- 为了保证资源一定会被释放,必须放入finally中
	            //4,关闭资源
	            try {
	                in.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	            try {
	                out.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	    }
	}

--4,IO练习
	package cn.tedu.io;

	import java.io.BufferedInputStream;
	import java.io.IOException;
	import java.io.InputStream;

	//IO练习: 读取键盘输入的数据并打印
	public class Test4 {
	    public static void main(String[] args) {
	        InputStream in = null;//为了finally都认识
	        try{
	            //1,创建读取流
	            in = new BufferedInputStream(System.in);
	            //2,开始读取
	            int b = 0 ;
	            while ( ( b = in.read() ) != -1){
	                System.out.println(b);
	            }
	        }catch (IOException e){
	            e.printStackTrace();
	        }finally {
	            //3,释放资源
	            IOUtils.close(in);
	        }
	    }
	}
--5,IOUtils
	package cn.tedu.io;

	import java.io.*;
	//提取close()共性代码,提高代码的复用性
	public class IOUtils {

	//参数Closeaable是InputStream和OutputStream的父接口
	//既可以关闭读取流也可以关闭写出流
	    public static void close(Closeable in){
	        //避免空指针异常:java.lang.NullPointerException
	        if(in != null){
	            try {
	                in.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	    }

	}

三,泛型
–1,概述
数组的优点:查询高效(有下标)
数组的缺点:数据类型单一,长度不能改变,遍历方式单一
集合可以解决数组的所有缺点。
集合中元素的类型很丰富。后期使用有异常,使用泛型。
泛型 通常 配合集合 一起使用,用来约束集合中元素的类型。
泛型是(Generics)是JDK1.5 的一个新特性.标志<>
–2,泛型的声明位置
–类上: class Person{}
–方法上: public void eat(E a){}
–3,测试
package cn.tedu.io;

	import java.util.ArrayList;
	import java.util.List;
	//测试 泛型
	public class Test5 {
	    public static void main(String[] args) {
	        //1,不用泛型---集合可以存各种类型的数据
	        List list = new ArrayList();
	        list.add(1);
	        list.add(1.1);
	        list.add("jack");
	        list.add(true);
	        list.add(new Object());
	        list.add('我');
	        System.out.println(list);

	        //2,约束集合里元素的类型 -- 用泛型
	        List<String> list2 = new ArrayList<>();
	        list2.add("jack");//通过了 类型检查
	//泛型的作用1: 可以约束集合中元素的类型,如果类型没通过泛型的检查直接报错
	//        list2.add(10.5);//没通过 类型检查
	        list2.add("rose");
	        System.out.println(list2);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值