• Java基础之IO流


    很长时间都没有更新了,最近在补充JavaSE的一些细节部分

    关于IO流的一些总结

    首先要介绍的是File类,File类用于对文件和目录的一些操作

    1.创建文件CreateNewFile()

    2.对文件的信息的获取getName(),getAbsolutePath()

    3.判断是否是文件isFile()

    4.遍历整个目录下的文件 File[] listFile(),返回一个元素为文件的对象数组,可以使用方法来遍历数组


    然后引入了流的概念

    以内存为参考系,以方向可以分为输入流(读),输出流(写)

    以流的内容来划分,可以分为字节流和字符流

    上图四个类都是抽象类,由抽象类可以向下派生出多个子类

    字节流是将各种数据通过字节(byte)来传输,比如视频,图片都可以转换为二进制,字符流是将数据通过字符(char)来传输,一般是文本文件

    输入方法都有read()方法,用于读取文件数据,输出方法都有writer()方法,用于将数据写入文件


    文件输入输出(实现类)

    1.FileInputStream,FileInputStream (文件字节类)

    2.FileReader,FileWriter (文件字符类)

    //文件写操作
    static void write(){
    
    	FileOutputStream out = new FileOutputStream("src\test.txt");
    	String str = "HelloIOStream";
    	byte[] b = str.getbyte();	
    	out.write(b);
    	out.close();
    }	
    
    //文件读操作
    static void read(){
    	FileOutputStream in = new FileInputStream("src\test.txt");
    	int temp;
    	while((temp = in.read()) != -1){
    		System.out.print((char)temp);
    	}
    	in.close();
    	
    }
    

    转换流(实现类)

    1.InputStreamReader (将InputStream类型转换为Reader类型)

    2.OutputStreamWriter (将OutputStream类型转换为Writer类型)

    这里补充一下标准输入输出流

    1.标准输入流System.in(InputStream类型)

    //直接从控制台读取
    BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
    	String temp = null;
    	while((temp = in.readLine())!=null){
    				 System.out.println(temp);
    	}
    

    2.标准输出流System.out

    //直接写在控制台中
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    	out.write("HelloWorld");
    	out.flush();
    	out.close();
    

    缓冲输入输出流(实现类)

    1.BufferedInputStream,BufferedOutputStream(缓冲字节流)

    BufferedInputStream in = new BufferedInputStream(new FileInputStream(src\test.txt));
    

    2.BufferedReader,BufferedWriter(缓冲字符流)

    缓冲流只不过是对文件流的封装,扩展了功能,可以将多个字节/字符同时读/写,大大提高效率

    //FileInputStream作为参数 	
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(src\test.txt));
    

    在BufferedReader和BufferedWriter中分别新增了方法readLine()和newLine()

    readLine()方法读取一行,返回字符串,如果没有下一行返回null

    newLine()方法在写入文件的时候添加换行

    打印流(实现类)

    1.PrintOutputStream

    2.PrintWriter

    System.out.println();中System是类,in是其中的字段,它是OutputStream类型

    所以可调用其中的print(),println()方法

    对象序列化流

    首先来谈谈什么叫做序列化,我们可以将一些普通的数据类型通过数据流存入文件,也可以将对象的状态持久化的保存在文件之中,而这个过程就称之为序列化,反之从文件之中获取对象称之为反序列化

    1.ObjectInputStream,ObjectOutputStream 对象字节流

    2.ObjectReader,ObjectWriter 对象字符流

    • 构造一个对象并继承Serializable接口,不进行序列化的字段加transient

        class Student implements Serializable{
        Student(int i , String n, int a){
        		id = i;
        		name = n;
        		age = a;
        }
        		int id;
        		String name;
        		transient int age;
        }
      
    • 将对象作为参数引入流中

        //将对象写入到流中
        static void write(){
        		Student s1 = new Student(1,"lilili",18);
        		Student s2 = new Student(2,"asdasd",23);
        		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("src\test.txt"));
        		out.writeObject(s1);
        		out.writeObject(s2);
        		out.flush(); //刷新流
        		out.close();
        }
      
        //将对象从流中读取出来
        static void read()
        {
        		ObjectInputStream in = new ObjectInputStream(new FileOutputStream(""src\test.txt));
        		Student temp;
        		while((temp =(Student)in.readObject())!= null){
        			System.out.println(temp.id+temp.name+temp.age);
        	}
        }
  • 相关阅读:
    卷积神经网络-目标探测-笔记
    卷积神经网络-目标分类简述
    深度学习-卷积神经网络的发展-笔记
    深度学习-卷积神经网络笔记
    深度学习-传统神经网络
    深度学习-初识深度学习-笔记
    数据结构-链式队列-C++
    查看线程的id名字正在运行的线程数量[getName()--ident--enumerate--activeCount]--线程池--协程(gevent)--优先级队列(LifoQueue)
    线程-线程的(锁、死锁现象、守护线程)
    ftp(网络传输)--BaseRequesHandler(tcp协议下的多个客户端连接)--urandom(随机生成字节字符串)--hmac(类似于hashlib)
  • 原文地址:https://www.cnblogs.com/libowen/p/6139273.html
Copyright © 2020-2023  润新知