• IO字节流与字符流的操作


    共性方法:
    public void close();//关闭该流,释放资源
    public void flush();//刷新缓冲区(对于字节输出流来说没用!!)
        
    public void write(int b);//写一个字节
    public void write(byte[] bs);//写一堆字节
    public void write(byte[] bs,int startIndex,int length);//写一堆字节的一部分
    public int read();//读取一个字节
    public int read(byte[] bs);//读取一堆字节,返回值代表实际读取的字节个数

    字节流:

     FileInputStream读取

     读取txt

        //输入
        public static void input() throws IOException {
            FileInputStream fileInputStream = new FileInputStream("day04/src/day20_file/fileTest/1.txt");
            int b = 0;
            byte[] arr = new byte[4];
    //        while ((b = fileInputStream.read()) != -1) {
    //            System.out.print((char) b);
    //        }
    //        for (int i = fileInputStream.read(); i != -1; i = fileInputStream.read()) {
    //            System.out.print((char) i);
    //        }
            while ((b = fileInputStream.read(arr)) != -1) {
                System.out.print(new String(arr, 0, b));
            }
            fileInputStream.close();
        }

    FileOutputStream输出

     写入到txt

        public static void output() throws IOException {
            //输出流
            FileOutputStream fileOutputStream = new FileOutputStream(new File("day04/src/day20_file/fileTest/1.txt"));
            //fileOutputStream.write(97);
            byte[] bytearr = {106, 97, 118, 97};
            bytearr = "张##".getBytes();
            System.out.println(Arrays.toString(bytearr));
            fileOutputStream.write(bytearr);
            fileOutputStream.write("a
    b".getBytes());
            fileOutputStream.write(bytearr);
            fileOutputStream.write(bytearr);
            fileOutputStream.close();
        }

    字节流使用数组缓冲区复制文件,最后得出所使用的时间

        //复制文件
        public static void copy() throws IOException {
            long startTime = System.currentTimeMillis();
            FileOutputStream fileOutputStream = new FileOutputStream(new File("222.txt"));
            FileInputStream fileInputStream = new FileInputStream("day04/src/day20_file/fileTest/1.txt");
            int lengths = 0;
            //效率高于单字节
            byte[] byteArr = new byte[4];
            while ((lengths = fileInputStream.read(byteArr)) != -1) {
                fileOutputStream.write(byteArr, 0, lengths);
            }
            //单字节
    //        while ((lengths = fileInputStream.read()) != -1) {
    //            fileOutputStream.write(lengths);
    //        }
            fileOutputStream.close();
            fileInputStream.close();
            System.out.println("耗时:(毫秒)" + (System.currentTimeMillis() - startTime));
        }

    字符流:
    FileReader 字节流读取 

        public static void demo2() throws Exception {
            FileReader fileReader = new FileReader(new File("111.txt"));
            int len = 0;
            char[] chars = new char[2];
            while ((len = fileReader.read(chars)) != -1) {
                System.out.print(new String(chars ,0 ,len));
            }
        }

    FileWriter 字节流输出

        public static void demo1() throws Exception {
            FileWriter fileWriter = new FileWriter("333.txt");
            char[] chars = {'爱', '中', '国', '我'};
            for (int j = 0; j < 10; j++) {
                fileWriter.write("嗯呢");
            }
            fileWriter.close();
        }

    字节流使用数组缓冲区复制文件

         public class copy {
             public static void main(String[] args) throws IOException {
                 FileReader fr=new FileReader("D:\test\a.txt");
                 FileWriter fw=new FileWriter("D:\test\d\a.txt");
                 char[] ch=new char[2];
                 int len=0;
                 while((len=fr.read(ch))!=-1)
                 {
                     fw.write(ch,0,len);
                     System.out.println((char)len);
                     fw.flush();
                 }
                 fw.close();
                 fr.close();
             }
         }

    转换流

    public InputStreamReader(InputStream in,String charsetName);//创建一个转换输入流,底层需要一个普通的字节流,charsetName表示指定的字符编码名字
    public InputStreamReader(InputStream in);//创建一个转换输入流,底层需要一个普通的字节流,默认使用idea的默认字符编码!
    public OutputStreamWriter(OutputStream out,String charsetName);//创建转换输出流,底层需要一个字节输出流,参数charsetName表示指定的编码名字
    public OutputStreamWriter(OutputStream out);//创建转换输出流,底层需要一个字节输出流,默认使用idea的默认字符编码!
    借助字节流写入方法,将字节流转换成字符流并且规定编码格式,例如编码格式为utf-8
    FileOutputStream fo=new FileOutputStream("D:\test\d\utf.txt");
    OutputStreamWriter osw=new OutputStreamWriter(fo, "utf-8");
    FileInputStream fi=new FileInputStream("D:\test\utf.txt");
    InputStreamReader isr=new InputStreamReader(fi,"utf-8");

    序列化流

    分类:
        序列化流: 对象的输出流,写对象
        反序列化流: 对象的输入流,读对象   
    public ObjectOutputStream(OutputStream out);
    public ObjectInputStream(InputStream in);
    public static void demo1() throws IOException, ClassNotFoundException {
            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("111.txt"));
            outputStream.writeObject(new Person("1","小张",1,1));
            ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("111.txt"));
            Object o = inputStream.readObject();
            System.out.println(o);
    }
    反序列化操作的两种异常演示
    a.ClassNotFoundException 找不到类异常
        原因: 序列化之后,将被序列化的类删除了,然后进行反序列化即可出现这个异常
            
    b.InvalidClassException  无效类异常 
        原因:序列化之后,将被序列化的类进行修改,然后进行反序列化即可出现这个异常
            
        扩展:反序列化流如何判断类是否有效呢???
            通过serialVersionUID(序列化版本号)来判断,而serialVersionUID通过类的成员内容计算而来的
            也就是只要类的内容没变,那么版本号也不会改变,只要内容改变那么版本号也会跟着改变!

    打印流  PrintStream:打印流,可以方便的输出各种数据类型的数据

    构造方法:
        public PrintStream(String path);
        public PrintStream(File file);
        public PrintStream(OutputStream out); 
    成员方法:
        public void print(各种数据类型均可);//打印数据,但是不带换行
        public void println(各种数据类型均可);//打印数据,末尾自动带换行

    缓冲区

    字节输出流: OutputStream  ---> BufferedOutputStream 缓冲字节输出流
    字节输入流: InputStream   ---> BufferedInputStream 缓冲字节输入流
    字符输出流: Writer        ---> BufferedWriter 缓冲字符输出流
    字符输入流: Reader        ---> BufferedReader 缓冲字符输入流
    public BufferedOutputStream(OutputStream out);//创建缓冲流需要传入普通流
    public BufferedInputStream(InputStream in);//创建缓冲流需要传入普通流
    public BufferedWriter(Writer w);//创建字符的缓冲流需要传入普通的字符流
    public BufferedReader(Reader r);//创建字符的缓冲流需要传入普通的字符流
            //字符流缓冲区
            FileReader fr=new FileReader("D:\test\aaa.txt");
            BufferedReader br=new BufferedReader(fr);
            //明确目的地
            FileWriter fw=new FileWriter("D:\test\d\aaa.txt");
            BufferedWriter bw=new BufferedWriter(fw);
            
    
            //字节流缓冲区
            FileInputStream fi=new FileInputStream("D:\codetool\eclipse.zip");
            BufferedInputStream bi=new BufferedInputStream(fi);
            FileOutputStream fo=new FileOutputStream("D:\test\eclipse.zip");
            BufferedOutputStream bo=new BufferedOutputStream(fo);
    
    
      跨平台换行:.newLine();
      读取一整行:readline();  

    Properties类

    用于操作配置文件,后缀名只能是properties的文件
    
    方法:
        load(input字符流或字节流对象);
        store(output字符流或字节流对象,描述信息);
            
    写入:
        创建对象:Properties prop = new Properties();
        写入prop集合:prop.setProperty("周迅", "张学友");
        创建字符流文件写入:FileWriter out = new FileWriter("prop.properties");
        写入:prop.store(out,"描述");
    
    取出:
        创建集合:Properties prop = new Properties();
            创建流对象:
            FileInputStream in = new FileInputStream("prop.properties");
            //FileReader in = new FileReader("prop.properties");
            把流所对应文件中的数据 读取到集合中:
            prop.load(in);
            4,关闭流:
            in.close();
            5,显示集合中的数据:
            System.out.println(prop);

     

    ResourceBundle类
    ResourceBundle直接继承自java.lang.Object,但是Properties则继承自Hashtable<K,V>

    ResourceBundle resourceBundle2 = ResourceBundle.getBundle("1234567");
    //根据key获取value System.out.println(resourceBundle2.getString(
    "java"));
  • 相关阅读:
    Java实现蓝桥杯突击战
    Java实现蓝桥杯突击战
    Java实现蓝桥杯勇者斗恶龙
    Java实现蓝桥杯勇者斗恶龙
    利用cmake来搭建开发环境
    cmake总结
    cmake手册详解----转
    Boost::filesystem 使用小笔记
    Boost学习之可移植路径操作--filesystem
    C++ 檔案、資料夾、路徑處理函式庫:boost::filesystem
  • 原文地址:https://www.cnblogs.com/xiaozhang666/p/10557242.html
Copyright © 2020-2023  润新知