Java基础:IO编程_java io编程-程序员宅基地

技术标签: java  # Java学习笔记  NIO  IO  AIO  BIO  

IO编程

1、IO流脑图

2、IO流分类

image-20210903163413192

image-20210903163440828

image-20210903171155474

3、File类操作文件

image-20210903171858267

4、访问文件

字节流:FileInputStream & FileOutputStream

/**
     * FileInputStreamTest
     * @throws IOException
     */
    @Test
    public void test01() throws IOException {
    
        String path = "F:\\file2.txt";
        FileInputStream fileInputStream = new FileInputStream(path);
        int readLen = 0;
        byte[] buf = new byte[8];
        while ((readLen = fileInputStream.read(buf)) != -1) {
    
            System.out.println(new String(buf, 0, readLen));
        }
        fileInputStream.close();
    }

    /**
     * FileOutputStreamTest
     * @throws IOException
     */
    @Test
    public void test02() throws IOException {
    
        String path = "F:\\file2.txt";
        FileOutputStream fileOutputStream = new FileOutputStream(path);
        fileOutputStream.write('h');//追加写入,默认写入末尾
        fileOutputStream.write(11);
        fileOutputStream.write("hello,world".getBytes());
    }

    /**
     * 案例:拷贝文件
     * @throws IOException
     */
    @Test
    public void test03() throws IOException {
    
        FileInputStream fileInputStream = new FileInputStream("F:\\png1.png");
        FileOutputStream fileOutputStream = new FileOutputStream("F:\\png2.png");

        byte[] buf = new byte[1024];
        int readLine = 0;

        while ((readLine = fileInputStream.read(buf)) != -1) {
    
            fileOutputStream.write(buf);
        }
        System.out.println("ok~~~");
    }

字符流:FileReader & FileWriter

/**
     * FileReaderTest
     */
    @Test
    public void test04() throws IOException {
    
        String filePath = "F:\\file2.txt";
        FileReader fileReader = new FileReader(filePath);
        int readLen = 0;
        char[] buf = new char[8];
        while ((readLen = fileReader.read(buf)) != -1) {
    
            System.out.println(new String(buf, 0, readLen));
        }
    }

    /**
     * FileWriter
     * @throws IOException
     */
//        fileWriter.close();
//        write(int):写入单个字符
//        write(char[]):写入指定数组
//        write(char[],off,len):写入指定数组的指定部分
//        write(string):弓入整个字符串
//        write(string,off,len):写入字符串的指定部分
    @Test
    public void test05() throws IOException {
    
//        new FileWriter(File/String):覆盖模式,相当于流的指针在首端
//        new FileWriter(File/String,true):追加模式,相当于流的指针在尾端
        FileWriter fileWriter = new FileWriter("F:\\file2.txt", true);
        fileWriter.write(999);
        char[] chars = new char[]{
    'a', 'b', 'c', 4, 5, 6};
        fileWriter.write(chars);
        fileWriter.write("stringstr");
        fileWriter.flush();

    }

5、缓冲流

image-20210903171257771

字节流:BufferedInputStream & BufferedOutputStream

    /**
     * 案例:拷贝文件:BufferedInputStream
     * @throws IOException
     */
    @Test
    public void test12() throws IOException {
    
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("F:\\png1.png"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("F:\\png2.png"));
        int readdata;
        while ((readdata = bufferedInputStream.read()) != -1) {
    
            bufferedOutputStream.write(readdata);
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }

    /**
     * 案例:拷贝文件:BufferedInputStream,byte[]
     */
    @Test
    public void test13() throws IOException {
    
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("F:\\png1.png"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("F:\\png2.png"));
        byte[] buf = new byte[1024];
        int readdata;
        while ((readdata = bufferedInputStream.read(buf)) != -1) {
    
            bufferedOutputStream.write(buf, 0, readdata);
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }

字符流:BufferedReader & BufferedWriter

    /**
     * BufferedReaderTest
     * @throws IOException
     */
    @Test
    public void test08() throws IOException {
    
        String path = "F:\\file2.txt";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
        String readData;
        while ((readData = bufferedReader.readLine()) != null) {
    
            System.out.println(readData);
        }
        bufferedReader.close();
    }

    /**
     * BufferedWriterTest
     * @throws IOException
     */
    @Test
    public void test09() throws IOException {
    
        String path = "F:\\file2.txt";
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path, true));

        bufferedWriter.write("hello");
        bufferedWriter.newLine();
        bufferedWriter.write("world");
        bufferedWriter.newLine();

        bufferedWriter.close();
    }

    /**
     * 案例:文件拷贝,
     *
     * @throws IOException
     */
    @Test
    public void test10() throws IOException {
    
        BufferedReader bufferedReader = new BufferedReader(new FileReader("F:\\hello.txt"));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("F:\\helloworld.txt"));
        String readData;
        while ((readData = bufferedReader.readLine()) != null) {
    
            bufferedWriter.write(readData);
            bufferedWriter.newLine();
        }
        bufferedReader.close();
        bufferedWriter.close();
    }

    /**
     * 案例:文件拷贝,二进制文件(png,pdf,mp4),文件损毁
     * @throws IOException
     */
    @Test
    public void test11() throws IOException {
    
        BufferedReader bufferedReader = new BufferedReader(new FileReader("F:\\png1.png"));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("F:\\png2.png"));
        String readData;
        while ((readData = bufferedReader.readLine()) != null) {
    
            bufferedWriter.write(readData);
            bufferedWriter.newLine();
        }
        bufferedReader.close();
        bufferedWriter.close();
    }

6、对象流

对象流只有字节流

image-20210903172702091

image-20210903172653329

字节流:ObjectOutputStream & ObjectInputStream

 /**
     * 序列化:ObjectOutputStream
     * @throws IOException
     */
    @Test
    public void test14() throws IOException {
    
        //文件后缀名没有意义,
        String path = "F:\\hi.txt";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
//        oos.write(100);//没有类型
        oos.writeInt(100);//int->Integer
        oos.writeBoolean(true);//boolean->Boolean
        oos.writeChar('H');//char->Char
        oos.writeDouble(3.14);//double->Double
        oos.writeUTF("张三&李四");//String
        oos.writeObject(new Dog("王五", 17));//java.io.NotSerializableException

        oos.close();
    }

    /**
     * 反序列化:ObjectInputStream
     *
     * @throws IOException
     */
    @Test
    public void test15() throws IOException, ClassNotFoundException {
    

        String path = "F:\\hi.txt";
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));

        //读取(反序列化)顺序 要和 写入(序列化)顺序 保持一致
        //否则异常:java.io.EOFException
        System.out.println(ois.readInt());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readChar());
        System.out.println(ois.readDouble());
        System.out.println(ois.readUTF());
        Object o = ois.readObject();
        System.out.println((Dog) o);

        //注意细节:
        //1.如果我们希望调用Dog里面的方法,需要向下转型
        //2.需要我们将Dog类的定义,写在可以引用的地方

        //3.如果在序列化之后,Dog类里面的内容进行了修改
        //  那么,必须在 反序列化之前重新 序列化
        System.out.println(((Dog) o).getAge());
        System.out.println(((Dog) o).getName());
    }

7、转换流

image-20210903171355809

字符流:InputStreamReader & OutputStreamWriter

/**
     * 问题引入:中文编码,乱码
     * @throws IOException
     */
    @Test
    public void test17() throws IOException {
    
        //读取文件,默认 utf-8
        String path = "F:\\Hello.txt";
        //如果读取的不是utf-8编码的文件,则会乱码
        BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
        System.out.println(bufferedReader.readLine());//HellJavaWorld�������
        bufferedReader.close();
    }

    /**
     * InputStreamReader 解决中文乱码
     * @throws IOException
     */
    @Test
    public void test18() throws IOException {
    
        //文件是以 ANSI 编码
        String path = "F:\\Hello.txt";
        //希望以 gbk 编码读取
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(path), "gbk");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String s = bufferedReader.readLine();
        System.out.println(s);//HellJavaWorld你好世界
        bufferedReader.close();
    }

    /**
     * OutputStreamWriter:以指定的编码写入文件
     * @throws IOException
     */
    @Test
    public void test19() throws IOException {
    
        String path = "F:\\hello.txt";
        String charSet = "GBK";

        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(path), charSet);

        osw.write("hello");
        osw.write("张三");

        osw.close();
    }

8、打印流

image-20210903171455625

字节流:PrintStream

/**
     * PrintStream
     * @throws IOException
     */
    @Test
    public void test20() throws IOException {
    

        PrintStream out = System.out;
        out.println("hello,world"); //打印到控制台
        /*  println = print + newline
            print 底层 也就是 write
            public void print(String s) {
            if (s == null) {
                s = "null";
             }
             write(s);
             }
         */
        out.write("hello,张三".getBytes());
        out.close();

        //可以修改打印流输出的位置
        //修改输出到 文件:
        System.setOut(new PrintStream("F:\\f1.txt"));
        //会打印到文件中,不再是控制台
        System.out.println("hello,李四");
        /* 源码:
         public static void setOut(PrintStream out) {
            checkIO();
            setOut0(out); //native方法
         }

         private static native void setOut0(PrintStream out);
         */

    }

字符流:PrintWriter

    /**
     * PrintWriter 控制台输出
     * @throws IOException
     */
    @Test
    public void test21() throws IOException {
    
        PrintWriter printWriter = new PrintWriter(System.out);
        printWriter.print("hello,田七");
        printWriter.close();
    }

    /**
     * printWriter,输出到文件
     * @throws IOException
     */
    @Test
    public void test22() throws IOException {
    
        PrintWriter printWriter = new PrintWriter(new FileWriter("F:\\f2.txt"));
        printWriter.print("hello,田七");
        printWriter.close();
    }

9、标准输入输出流

image-20210903171435773

    /**
     * 标准输入,输出流
     * System.in & System.out
     */
    @Test
    public void test16() {
    

        //System.in源码:public final static InputStream in = null;
        //编译类型:InputStream
        //运行类型:BufferedInputStream
        System.out.println(System.in.getClass());//class java.io.BufferedInputStream

        //System.out源码:public final static PrintStream out = null;
        //编译类型:PrintStream
        //运行类型:PrintStream
        System.out.println(System.out.getClass());//class java.io.PrintStream

    }

10、Properties类

/**
     * 使用 Properties 创建文件
     * @throws IOException
     */
    @Test
    public void test23() throws IOException {
    

        Properties properties = new Properties();
        //添加数据
        properties.setProperty("id", "127.0.0.1");
        properties.setProperty("user", "zhangsan张三");//中文将会以 unicode码 的方式保存在文件中
        properties.setProperty("pwd", "66666666");
        //保存
        //第二个参数,用于设置 配置文件 的注解,
        //如果 填写null,会自动为你添加当前时间 例:#Tue Aug 31 19:51:16 CST 2021
        //如果 不是非null,则会在 文件首行添加  # 你填写的内容(中文为unicode码) + [换行] + 当前时间
        properties.store(new FileOutputStream("F:\\mysql.properties"), "这是一个注释");

        System.out.println("保存成功~~");

    }

    /**
     * 读取 properties 文件数据
     * @throws IOException
     */
    @Test
    public void test24() throws IOException {
    
        Properties properties = new Properties();
        //加载文件
        properties.load(new FileReader("F:\\mysql.properties"));
        //获取整个数据列表,并输出在 控制台上
        //properties底层是 hashtable,所以 添加的顺序 != 存放的顺序
        //class Properties extends Hashtable<Object,Object>
        properties.list(System.out);

        String user = properties.getProperty("user");
        String pwd = properties.getProperty("pwd");
        System.out.println(user);
        System.out.println(pwd);
    }

    /**
     * 修改 properties 值
     *
     * @throws IOException
     */
    @Test
    public void test25() throws IOException {
    
        Properties properties = new Properties();
        //添加数据
        properties.setProperty("id", "127.0.0.1");
        properties.setProperty("user", "zhangsan张三");//中文将会以 unicode码 的方式保存在文件中
        properties.setProperty("pwd", "66666666");

        //获取修改前的数据
        properties.list(System.out);

        //修改数据
        //如果 key存在,再次添加 就是修改
        properties.setProperty("user", "lisi李四");

        //获取修改后的数据
        properties.list(System.out);
    }

11、随机文件存取流

image-20210903172519681

image-20210903172501588

RandomAccessFile

/**
 * @Author: 
 * @Date: 2021/9/2 10:21
 */
public class demo03 {
    

    /**
     * RandomAccessFile 复制文件
     */
       @Test
    public void test01() throws IOException {
    

        RandomAccessFile raf1 = null;
        RandomAccessFile raf2 = null;
        raf1 = new RandomAccessFile(new File("png1.png"), "r");
        raf2 = new RandomAccessFile(new File("png2.png"), "rw");

        byte[] buff = new byte[1024];
        int len;
        while ((len = raf1.read(buff)) != -1) {
    
            raf2.write(buff, 0, len);
        }
        raf1.close();
        raf2.close();

    }

    /**
     * RandomAccessFile
     * 没有文件时,自动创建
     * 有文件时,从头覆盖,覆盖几个算几个
     * @throws IOException
     */
    @Test
    public void test02() throws IOException {
    
        RandomAccessFile raf=new RandomAccessFile(new File("hello.txt"),"rw");
        raf.write("hihihihi".getBytes());
        //原文件内容:hello,everyone大家好
        //修改后文件:hihihihieryone大家好
        raf.close();
    }


    /**
     * RandomAccessFile 追加写入
     * @throws IOException
     */
    @Test
    public void test03() throws IOException {
    
        File file = new File("hello.txt");
        RandomAccessFile raf=new RandomAccessFile(file,"rw");
        //seek,定位到文件最后面
        raf.seek(file.length());
        raf.write("hihihihi".getBytes());
        //原文件内容:hello,everyone大家好
        //修改后文件:hello,everyone大家好 <换行> hihihihi
        raf.close();
    }


    /**
     * RandomAccessFile seek定位实现插入
     * @throws IOException
     */
    @Test
    public void test04() throws IOException{
    

        File file=new File("hello.txt");
        RandomAccessFile raf=new RandomAccessFile(file,"rw");

        raf.seek(6);

        //保存指针5后面的所有数据
        StringBuilder builder=new StringBuilder((int) file.length());
        byte[] buff=new byte[1024];
        int len;
        while ((len=raf.read(buff))!=-1){
    
           builder.append(new String(buff,0,len));
        }

        //调回指针
        raf.seek(6);
        //写入 要插入的数据
        raf.write("hihiihi".getBytes());

        //写回
        raf.write(builder.toString().getBytes());

        raf.close();

        //原文件内容:hello,everyone大家好
        //修改后文件:hello,hihiihieveryone大家好
    }
}

12、第三方JAR包

FileUtils

推荐阅读:https://www.cnblogs.com/deityjian/p/11106981.html

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
1.写 文件/文件夹
/* 写文件 
 * 1.这里只列出3种方式全参数形式,api提供部分参数的方法重载 
 * 2.最后一个布尔参数都是是否是追加模式 
 * 3.如果目标文件不存在,FileUtils会自动创建 
 * */  
//static void:write(File file, CharSequence data, String encoding, boolean append)   
FileUtils.write(new File("D:/a/b/cxyapi.txt"), "程序换api","UTF-8",true);  
  
//static void:writeLines(File file, Collection<?> lines, boolean append)   
List<String> lines=new ArrayList<String>();  
lines.add("欢迎访问:");lines.add("www.cxyapi.com");  
FileUtils.writeLines(new File("D:/a/b/cxyapi.txt"),lines,true);  
  
//static void:writeStringToFile(File file, String data, String encoding, boolean append)   
FileUtils.writeStringToFile(new File("D:/a/b/cxyapi.txt"), "作者:cxy", "UTF-8",true);  
 

2.读 文件/文件夹
//读文件  
//static String:readFileToString(File file, String encoding)   
System.out.println(FileUtils.readFileToString(new File("D:/a/b/cxyapi.txt"), "UTF-8"));  
  
//static List<String>:readLines(File file, String encoding)   
System.out.println(FileUtils.readLines(new File("D:/a/b/cxyapi.txt"), "UTF-8")); //返回一个list  
 

3.删除 文件/文件夹
//删除目录  
//static void:deleteDirectory(File directory)   
FileUtils.deleteDirectory(new File("D:/not/cxyapi"));  
  
//static boolean:deleteQuietly(File file)   
FileUtils.deleteQuietly(new File("D:/not/cxyapi")); //文件夹不是空任然可以被删除,永远不会抛出异常  
 

4.移动 文件/文件夹
//移动文件 或 文件夹  
//static void:moveDirectory(File srcDir, File destDir)   
FileUtils.moveDirectory(new File("D:/cxyapi1"), new File("D:/cxyapi2")); //注意这里 第二个参数文件不存在会引发异常  
//static void:moveDirectoryToDirectory(File src, File destDir, boolean createDestDir)   
FileUtils.moveDirectoryToDirectory(new File("D:/cxyapi2"), new File("D:/cxyapi3"), true);  
/* 上面两个方法的不同是: 
 * moveDirectory:D:/cxyapi2里的内容是D:/cxyapi1的内容。 
 * moveDirectoryToDirectory:D:/cxyapi2文件夹移动到到D:/cxyapi3里 
 *  
 * 下面的3个都比较简单没提供示例,只提供了api 
 * 其中moveToDirectory和其他的区别是 它能自动识别操作文件还是文件夹 
 */  
//static void:moveFileToDirectory(srcFile, destDir, createDestDir)  
//static void:moveFile(File srcFile, File destFile)   
//static void:moveToDirectory(File src, File destDir, boolean createDestDir)  
 

5.copy
//结果是cxyapi和cxyapi1在同一目录  
FileUtils.copyDirectory(new File("D:/cxyapi"), new File("D:/cxyapi1"));   
//结果是将cxyapi拷贝到cxyapi2下  
FileUtils.copyDirectoryToDirectory(new File("D:/cxyapi"), new File("D:/cxyapi2"));  
  
//拷贝文件  
FileUtils.copyFile(new File("d:/cxyapi.xml"), new File("d:/cxyapi.xml.bak"));  
//拷贝文件到目录中  
FileUtils.copyFileToDirectory(new File("d:/cxyapi.xml"), new File("d:/cxyapi"));  
//拷贝url到文件  
FileUtils.copyURLToFile(new URL("http://www.cxyapi.com/rss/cxyapi.xml"), new File("d:/cxyapi.xml"));  
 

6.其他
//判断是否包含文件或者文件夹  
boolean b=FileUtils.directoryContains(new File("D:/cxyapi"), new File("D:/cxyapi/cxyapi.txt"));  
System.out.println(b);  
  
//获得临时目录 和 用户目录  
System.out.println(FileUtils.getTempDirectoryPath());  
System.out.println(FileUtils.getUserDirectoryPath());  
  
//打开流,如果不存在创建文件及其目录结构  
//第二个参数表示 文件流是否是追加方式  
FileOutputStream fos=FileUtils.openOutputStream(new File("D:/cxyapi/cxyapi.txt"),true);  
fos.write(new String("欢迎访问:www.cxyapi.com\r\n").getBytes());  
fos.close();  
  
//文件 或 文件夹大小  
System.out.println(FileUtils.sizeOf(new File("D:/cxyapi")));  
System.out.println(FileUtils.sizeOfDirectory(new File("D:/cxyapi")));

13、数据流

image-20210903171544593

    /**
     * DataOutputStream
     * @throws IOException
     */
    @Test
    public void test06() throws IOException{
    
        DataOutputStream dos=new DataOutputStream(new FileOutputStream("data.txt"));
        dos.writeUTF("大家好");
        dos.flush();
        dos.writeInt(33);
        dos.flush();
        dos.writeBoolean(false);
        dos.flush();
    }


    /**
     * DataInputStream
     * @throws IOException
     */
    @Test
    public void test07() throws IOException{
    
        DataInputStream dis=new DataInputStream(new FileInputStream("data.txt"));
        System.out.println(dis.readUTF());
        System.out.println(dis.readInt());
        System.out.println(dis.readBoolean());
    }

14、访问数组

字节流:ByteArrayInputStream & ByteArrayInputStream

使用字节流,从字节数组中读取数据,以及向字节数组中写数据。

java.io.ByteArrayInputStream 负责从字节数组中读取数据

java.io.ByteArrayOutputStream 负责把数据写入到字节数组中

    @Test
    public void test01() throws IOException {
    

        byte[] arr="hello,everyone".getBytes();
        //1.创建流
        InputStream in = new ByteArrayInputStream(arr);
        OutputStream out = new ByteArrayInputStream();
        //2.使用流
        int len=0;
        byte[] buff=new byte[1024];
        while ((len= in.read(buff))!=-1){
    
            System.out.println(new String(buff,0,len));
            //将数据写入out对象中的属性里面
            out.write(buff,0,len);
            out.flush();
        }

        //ByteArrayOutputStream中的toByteArray()方法,可以将out对象中的数据返回
        byte[] toByteArray = ((ByteArrayOutputStream) out).toByteArray();
        System.out.println(Arrays.toString(toByteArray));
        
    }

字符流:CharArrayReader & CharArrayWriter

使用字符流,从字符数组中读取数据,以及向字符数组中写数据。

java.io.CharArrayReader 负责从字符数组中读取数据

java.io.CharArrayWriter 负责把数据写入到字符数组中

    @Test
    public void test02() throws IOException {
    

        char[] chars = "大家好,我是zhangsan".toCharArray();

        //1.创建流
        Reader reader = new CharArrayReader(chars);
        Writer writer = new CharArrayWriter();
        //2.使用流
        int len=0;
        char[] buff=new char[10];
        while ((len= reader.read(buff))!=-1){
    
            System.out.println(new String(buff,0,len));
            //将数据写入out对象中的属性里面
            writer.write(buff,0,len);
            writer.flush();
        }

        //CharArrayWriter中的toByteArray()方法,可以将out对象中的数据返回
        char[] array = ((CharArrayWriter) writer).toCharArray();
        System.out.println(Arrays.toString(array));//[大, 家, 好, ,, 我, 是, z, h, a, n, g, s, a, n]

    }

15、访问管道

字节流:PipedInputStream & PipedOutputStream

使用字节流,可以从管道中读取数据,以及向管道中写数据。

java.io.PipedInputStream 负责从管道中读取数据

java.io.PipedOutputStream 负责将数据写入到管道中

注意,一般可以在一个线程中,使用管道输出流,将数据写入到管道中,在另一个线程中,读取管道中 的数据。

image-20210906170505273


   /**
     * 访问管道:字节流 PipedInputStream,PipedOutputStream
     * @throws IOException
     */
    @Test
    public void test03() throws IOException, InterruptedException {
    
        PipedInputStream in = new PipedInputStream();
        PipedOutputStream out = new PipedOutputStream();

        //管道对接
        in.connect(out);

        Thread t1=new Thread(new Runnable() {
    
            @Override
            public void run() {
    
                byte[] arr="hello,world,zhangsan".getBytes();
                try {
    
                    for (int i = 0; i <arr.length ; i++) {
    
                        out.write(arr[i]);
                        out.flush();
                        Thread.sleep(10);
                    }
                } catch (IOException e) {
    
                    e.printStackTrace();
                } catch (InterruptedException e) {
    
                    e.printStackTrace();
                } finally {
    
                    try {
    
                        out.close();
                    } catch (IOException e) {
    
                        e.printStackTrace();
                    }
                }
            }
        },"写线程");

        Thread t2=new Thread(new Runnable() {
    
            @Override
            public void run() {
    
                try {
    
                    int data=0;
                    while ((data=in.read())!=-1){
    
                        System.out.println(data);//h101
                        System.out.write(data);//
                        System.out.flush();
                    }
                    System.out.flush();
                } catch (IOException e) {
    
                    e.printStackTrace();
                } finally {
    
                    try {
    
                        in.close();
                    } catch (IOException e) {
    
                        e.printStackTrace();
                    }
                }
            }
        },"读线程");

        t1.start();
        t2.start();

        t1.join();
        t2.join();

    }
}

字符流:PipedReader & PipedWriter

使用字符流,可以从管道中读取数据,以及向管道中写数据。

java.io.PipedReader 负责从管道中读取数据

java.io.PipedWriter 负责将数据写入到管道中

注意,一般可以在一个线程中,使用管道输出流,将数据写入到管道中,在另一个线程中,读取管道中 的数据。

image-20210906171911789

和之前的管道字节流的操作类似,只是把字节改成了字符进行操作

/**
     * 访问管道:字符流 PipedReader,PipedWriter
     * @throws IOException
     */
    @Test
    public void test04() throws IOException, InterruptedException {
    

        PipedReader in = new PipedReader();
        PipedWriter out = new PipedWriter();

        //管道对接
        in.connect(out);

        Thread t1=new Thread(new Runnable() {
    
            @Override
            public void run() {
    
                char[] arr = "hello,world,zhangsan".toCharArray();
                try {
    
                    for (int i = 0; i <arr.length ; i++) {
    
                        out.write(arr[i]);
                        out.flush();
                        Thread.sleep(10);
                    }
                } catch (IOException e) {
    
                    e.printStackTrace();
                } catch (InterruptedException e) {
    
                    e.printStackTrace();
                } finally {
    
                    try {
    
                        out.close();
                    } catch (IOException e) {
    
                        e.printStackTrace();
                    }
                }
            }
        },"写线程");

        Thread t2=new Thread(new Runnable() {
    
            @Override
            public void run() {
    
                try {
    
                    int data=0;
                    while ((data=in.read())!=-1){
    
//                        System.out.println(data);//h101
                        System.out.write(data);//
                        System.out.flush();
                    }
                    System.out.flush();
                } catch (IOException e) {
    
                    e.printStackTrace();
                } finally {
    
                    try {
    
                        in.close();
                    } catch (IOException e) {
    
                        e.printStackTrace();
                    }
                }
            }
        },"读线程");

        t1.start();
        t2.start();

        t1.join();
        t2.join();

    }

16、访问字符串

字符流:StringReader、StringWriter

只有字符流

    @Test
    public void test05() throws IOException{
    

        String str="hello大家好,a吗aa,b好bb,大家";
        StringReader stringReader=new StringReader(str);
        StringWriter stringWriter=new StringWriter();
        int len=0;
        char[] chars=new char[6];
       while ((len=stringReader.read(chars))!=-1){
    
           System.out.println(new String(chars,0,len));
           stringWriter.write(chars);
       }

        StringBuffer buffer = stringWriter.getBuffer();
        System.out.println(buffer);
        
    }
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/A233666/article/details/120152410

智能推荐

Vue插件之fetch-jsonp,Web前端编程基础学习-程序员宅基地

文章浏览阅读320次,点赞3次,收藏4次。userList.add(new User(11, “zhangsan”, ‘女’));userList.add(new User(33, “wanger”, ‘女’));userList.add(new User(22, “lisi”, ‘女’));userList.add(new User(44, “mazi”, ‘男’));请求数据。第二步:App.vue页面。

Qt 中英文翻译_##xqtp43ag9qb1uz##page-程序员宅基地

文章浏览阅读2.9k次。中英文翻译就是这么简单 在pro文件中 $$PWD表示.pro文件所在的目录运行程序会在$$PWD/appInfo/路径下生成Chinese.ts和English.ts文件 打开qt提供的翻译工具,然后同时打开这两个.ts文件自己翻译吧. 翻译完成后保存,不要使用工具中的导出Qt5.6.2已测试导出的.qm文件不能用。 使用下面的方式 把生成的.qm文件拷贝出..._##xqtp43ag9qb1uz##page

解决问题:ModuleNotFoundError: No module named ‘requests‘_traceback (most recent call last): modulenotfounde-程序员宅基地

文章浏览阅读1.8w次,点赞12次,收藏10次。代码片段:#!/usr/bin/python# -*- coding: UTF-8 -*-import requests报错提示:Traceback (most recent call last): File "D:/MyWorkspace/PycharmProjects/pythonProject/httpUtils.py", line 4, in <module> import requestsModuleNotFoundError: No module name..._traceback (most recent call last): modulenotfounderror: no module named 'req

Kernel panic – not syncing: Attempted to kill init 解决_(4) kernel panic – not syncing:attempted to kill i-程序员宅基地

文章浏览阅读1.2k次。因为业务需要关闭了selinux,系统重启后就出现 Kernel panic – not syncing: Attempted to kill ini。解决办法: 系统启动的时候,按下‘e’键进入grub编辑界面,编辑grub菜单,选择“kernel /vmlinuz-2.6.23.1-42.fc8 ro root=/dev/vogroup00/logvol00 rhgb qui_(4) kernel panic – not syncing:attempted to kill init !

[9] ADB 查看设备信息_adb display_id-程序员宅基地

文章浏览阅读2.5k次,点赞6次,收藏5次。查看设备信息型号电池状况屏幕分辨率屏幕密度显示屏参数android_idIMEIAndroid 系统版本IP 地址Mac 地址CPU 信息内存信息更多硬件与系统属性型号adb shell getprop ro.product.model我这使用的是锤子手机做测试:电池状况adb shell dumpsys battery其中 scale 代表最大电量,level 代表当前电量。上面的输出表示还剩下 90% 的电量。屏幕分辨率adb shell wm size屏幕密度adb s_adb display_id

如何使用ChatGPT快速构建一个网站模板,你花了多久弄明白架构设计-程序员宅基地

文章浏览阅读814次,点赞19次,收藏17次。关于ChatGPT这个AI工具,它可以实现的事情,远不止实现一个网站页面模板,它可以做的事情还非常多,ChatGPT的出现,算是AI领域的一个重大突破,为啥这样说,因为,它的语言模型更加接近人类语言,不会像一个机器人那样死板,它会有所变通,如果你也喜欢AI,热爱机器学习的话,可以自行下载安装一个来体验一下,在使用的过程种,用英文语言跟它交流会比用中文更加友好。当然,并不是说不可以用中文,中文也一样可以的,它做了大量的训练,支持很多国家的语言。

随便推点

layui select下拉框实现多选功能(附代可运行源码)_select多选下拉框 源码-程序员宅基地

文章浏览阅读551次。demo在线下载地址(完整代码包含插件地址)http://www.shagua.wiki/project/3?p=125_select多选下拉框 源码

网络指令(ipconfig | netstat | tasklist)-程序员宅基地

文章浏览阅读498次,点赞9次,收藏11次。显示所有适配器的 IPv6 地址、 IPv4 地址、子网掩码、默认网关ipconfig显示本机 TCP/IP 配置的详细信息DHCP 客户端手工释放 IP 地址DHCP 客户端手工向服务器刷新请求显示本地 DNS 内容清除本地 DNS 缓存内容。

Android13 获取双卡信号强度_android 监听双卡信号强度-程序员宅基地

文章浏览阅读524次,点赞9次,收藏7次。android13 获取双卡信号强度_android 监听双卡信号强度

vue base64图片不显示_Vue实战080:SVG图标不显示异常记录-程序员宅基地

文章浏览阅读1.5k次。今天在vue中配置处理svg文件时遇到图标显示不出来,查看开发者工具也没有提示异常和错误。不过查看Element元素的时候可以看到svg标签中出现了#shadow-root (closed),shadow-dom 是浏览器提供的功能,它允许在浏览器渲染文档(document)的时候向Dom结构中插入子DOM元素,但是这个子DOM元素(shadow-dom)并不在主DOM 树中,所以该子DOM元素无..._vue base64 svg图片

Android开发--Activity切换动画(从右侧进入,左侧退出)_slide_in_from_left-程序员宅基地

文章浏览阅读1.9k次,点赞2次,收藏6次。在res中创建anim文件夹: 添加四个文件:文件名slide_in_from_left.xml效果:子页面从左侧进入,用于上级页面进入当前页面的返回效果<?xml version="1.0" encoding="utf-8"?><translate ="http://schemas.android.com/apk/res/android" android:fromXDelta="-100%" android:toXDelta="0" _slide_in_from_left

【bug】解决font-family 仿宋体不生效问题_css font-family不生效-程序员宅基地

文章浏览阅读911次。提示:让仿宋体生效系统环境: MacBook Pro(13英寸,M2,2022年)_css font-family不生效

推荐文章

热门文章

相关标签