zoukankan      html  css  js  c++  java
  • Java之io

    io中的流:(可以想象一条河流,上游时输入流,下游是输出流)(每一对输入输出流是对应的)

    所有的流系统自动分配缓冲区,我们也可以自己设定缓冲区(使用方法  new BufferedInputStream(is,size))

    inputstream(父类,抽象类)  Fileinputstream(读取文件输入流)

    ouputstream(父类,抽象类)  Fileoutputstream(文件输出流)

    上面所说的是字节流,字节流和字符流可以转换:使用方法:InputstreamReader(is)  OutputStreamWriter(os)

    bufferesreader  将InputstreamReader(is)送入缓冲区使用readline()方法读取信息

    bufferedwriter  将OutputStreamWriter(os)送入缓冲区

    注意缓冲区都需要使用flush()方法,否则可能缓冲区的内容没有完全写出来

    BufferedReader in = new BufferedReader(new FileReader("foo.in"));

    多个输入流使用SequenceInputStream()方法,具体方法自己查api文档,

    注意使用SequenceInputStream()方法时可能会有NULLPOINTEXCEPTION可能原因是其中有一个流是null,我写的这个方法就是有问题的,如果有一个输入流是null,所以事先需要进行判断输入流是否为空

    while((a = inputStreamReader.read(bytes)) != -1)
    
        osr.write(bytes,0,a);

    阻塞ioAPI文档:read方法到流结尾时会传递-1  socket并没有将-1传递过去,所以使用socket.outputstream()方法并没有拿到数据流的结尾,自然会造成while循环退不出去
    需要client端发送socket.shutdown或者关闭流那么server端才能收到结束标志,才能退出while循环
    package july.wild.IO_Learning.tcp02;
    
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.Enumeration;
    
    /**
     * @author 郭赛
     * @Company Huawei
     * 文件的判断,先判断路径是否存在,再判断文件是否存在,如果存在,那么作为输入流,如果不存在创建一个文件
     * boolean    mkdir()    Creates the directory named by this abstract pathname.创建一级目录
     * boolean    mkdirs()    Creates the directory named by this abstract pathname.创建多级目录
     *
     */
    public class MultiInputstream {
        public static void main(String[] args) {
            int s;
            ArrayList<FileInputStream> multi = new ArrayList<>();
            File path = new File("C:\Users\郭赛\Desktop\aaa.txt");
            File path01 = new File("C:\Users\郭赛\Desktop\demo02.txt");
            File path02 = new File("C:\Users\郭赛\Desktop\demo01.txt");
            boolean exists = path.exists();
           /* System.out.println(exists);
            System.out.println(path.getName());//获取文件名称
            System.out.println(path.isDirectory());
            System.out.println(path.isFile());*/
            FileInputStream fis01 = judgeFile(path);
            FileInputStream fis02 = judgeFile(path01);
            FileInputStream fis03 = judgeFile(path02);
            //使用集合封装FileInputStream
            multi.add(fis01);
            multi.add(fis02);
            multi.add(fis03);
            /**
             * 使用SequenceInputStream()方法将多个输入流合并成一个,计算机可以没有输入,但是一定要有输出
             * SequenceInputStream​(InputStream s1, InputStream s2)
             *          * Initializes a newly Screated SequenceInputStream by remembering the two arguments,
             * which will be read in order, first s1 and then s2, to provide the bytes to be read from this SequenceInputStream.
             *
             *
             * SequenceInputStream​(Enumeration<? extends InputStream> e)
             * Initializes a newly created SequenceInputStream by remembering the argument,
             * which must be an Enumeration that produces objects whose run-time type is InputStream.
             *
             *
             * collections.enumeration​(Collection<T> c)
             * static <T> Enumeration<T>    enumeration​(Collection<T> c)    Returns an enumeration over the specified collection.
             *
             */
            Enumeration<FileInputStream> e = Collections.enumeration(multi);
            //返回多个数据流的合并
            SequenceInputStream sequenceInputStream = new SequenceInputStream(e);
            File savePath = new File("C:\Users\郭赛\Desktop\demo04.txt");
            OutputStream os = null;
            if(savePath.exists())
                if(savePath.isFile()) {
                    try {
                        os = new FileOutputStream(savePath);
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } finally {
                        byte[] bytes = new byte[1024];
                        try {
                            while ((s = sequenceInputStream.read(bytes)) != -1) {
                                os.write(bytes, 0, s);
                            }
                            os.flush();
                            os.close();
                            sequenceInputStream.close();
                            fis01.close();
                            fis02.close();
                            fis03.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            else {
                    try {
                        System.out.println("创建新的文件");
                        boolean newFile = savePath.createNewFile();
                        if(newFile)
                            System.out.println("文件创建成功");
                         os = new FileOutputStream(savePath);
    
    
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }finally {
                        byte[] bytes = new byte[1024];
                        try {
    
    
                            while((s = sequenceInputStream.read(bytes)) != -1){
                                os.write(bytes,0,s);
                            }
                            os.flush();
                            os.close();
                            sequenceInputStream.close();
                            fis01.close();
                            fis02.close();
                            fis03.close();
    
    
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
        }
    
        public static FileInputStream judgeFile( File file){
            if(file.exists())
                if(file.isFile()){
                    try {
                        FileInputStream fis = new FileInputStream(file);
                        return fis;
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
    
                }
                return null;
        }
    }

    非阻塞io:

  • 相关阅读:
    数据库高级链表查询,重点可以多看看
    数据库多表查询,一对一关系,一对多关系,多对多关系
    Django基础
    前端之JavaScript
    前端之Jquery
    CF888E Maximum Subsequence-折半搜索
    HNOI2010 平面图判定
    CEOI 2011Traffic
    LuoguP1710 地铁涨价
    Luogu2059 卡牌游戏-概率DP
  • 原文地址:https://www.cnblogs.com/guosai1500581464/p/13033445.html
Copyright © 2011-2022 走看看