zoukankan      html  css  js  c++  java
  • Java的输入与输出流InputStream/OutputStream

    先看InputStream和FileInputStream的结构

    操作输入流的步骤:

    1. 创建源
    2. 选择流
    3. 操作
    4. 释放源

    代码示例:

    import org.testng.annotations.Test;
    import java.io.*;
    
    public class FileDemo {
    
        @Test
        public void fileTest() {
    
            //1.创建源
            File file = new File("jerry.txt");
    
            //2.选择流
            InputStream in = null;
    
            //3.操作
            try {
                in = new FileInputStream(file);
                int temp;
                while (true) {
    
                    if (!((temp = in.read()) != -1)) break;
                    //read()返回是字符的编码,我们要读取的字符是英文,所以转义成char
                    System.out.print((char) temp);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                //4.关闭流
                //注意:close()要卸载finally里面。
                //如果不写在finally里面,而是写在前面catch里面,当出现异常时,close就不会被执行
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    用public int read(byte[] b)方法

    这个方法每次会读取指定长度的字符。

    长度有字符数组byte[]b决定。

    import org.testng.annotations.Test;
    
    import java.io.*;
    
    public class FileDemo {
    
        @Test
        public void fileTest() {
    
            //1.创建源
            File file = new File("jerry.txt");
    
            //2.选择流
            InputStream in = null;
    
            //3.操作
            try {
                in = new FileInputStream(file);
                int temp;
                byte[] b=new byte[1024];
                int len=-1;//len代表每次读取到的字符的长度,也就是上面定义的2014,读到末尾返回-1
                while ((len=in.read(b))!=-1){
                    //需要用String类来把读取到的字符数组解码
                    String str=new String(b,0,len);
                    System.out.println(len);
                    System.out.println(str);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    //4.关闭流
                    //注意:close()要卸载finally里面。
                    //如果不写在finally里面,而是写在前面catch里面,当出现异常时,close就不会被执行
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    FileInputStram/FileoutputStream:

    是字节流,通过字节的方式读取/输出所有的文件类型,比如:图像、视频、文字等等。

    FileReader/FileWriter:

    全字符请考虑FileReader/FileWriter

    FileOutputStream

    import org.testng.annotations.Test;
    
    import java.io.*;
    
    public class FileDemo {
    
        @Test
        public void fileTest() {
    
            //1.创建源
            File file = new File("jerry.txt");
    
            //2.选择流
            OutputStream out = null;
    
            //3.操作
            try {
                out = new FileOutputStream(file);
                String msg = "天生丽质难自弃";
             //因为write方法的参数是一个字节数组,所以需要把字符串转换成字节数组
                byte[] b = msg.getBytes();
                out.write(b);//把整个字节数组全部写进文件
                out.write(b,3,6);//从字节数组第三个开始,写6个字节到文件
                out.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    //4.关闭流
                    //注意:close()要卸载finally里面。
                    //如果不写在finally里面,而是写在前面catch里面,当出现异常时,close就不会被执行
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    FileOutputStream(File file),这个构造方法创建的输出流,每写一次就会把原有内容覆盖

    FileOutputStream(File file, boolean append),这个构造方法创建的输出流,不会覆盖原有内容,而是在原有内容后面追加。

  • 相关阅读:
    解决javaScript在不同时区new Date()显示值不同问题
    页面返回上一页浏览位置
    如何disabled禁用所有表单input输入框元素
    js根据json数组多个字段排序
    No identifier specified for entity
    Android resource compilation failed
    android 系统dialog的应用
    android消息处理源码分析
    Linux下常用命令
    ContentProvider和ContentResolver的使用
  • 原文地址:https://www.cnblogs.com/majestyking/p/12416051.html
Copyright © 2011-2022 走看看