zoukankan      html  css  js  c++  java
  • 字节流、字符流

    一、IO

         1.IO的分类

    • 输入流 :把数据从其他设备上读取到内存中的流。

    • 输出流 :把数据从内存 中写出到其他设备上的流。

          格局数据的类型分为:字节流字符流

    • 字节流 :以字节为单位,读写数据的流。

    • 字符流 :以字符为单位,读写数据的流。

          2.顶级父类

             

    二、字节流

          1.字节输出流【OutputStream】      

    • public void close() :关闭此输出流并释放与此流相关联的任何系统资源。当完成流的操作时,必须调用此方法,释放系统资源。

    • public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。

    • public void write(byte[] b):将 b.length字节从指定的字节数组写入此输出流。

    • public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流。

    • public abstract void write(int b) :将指定的字节输出流。

          2.FileOutputStream类     

    public class FOSWrite {
        public static void main(String[] args) throws IOException {
            // 使用文件名称创建流对象
            FileOutputStream fos = new FileOutputStream("fos.txt",true);  //true,表示可追加
               String str ="fsfsfsfs
    yeyeye";        //   
    表示换行
           byte[] a =str.getBytes();//编码 字符串-》》字节
           //写出
            os.write(a,0,a.length);
             os.flush();
              // 关闭资源
            fos.close();
        }
    }

         3.FileInputStream类 

    public class FISRead {
        public static void main(String[] args) throws IOException{
              // 使用文件名称创建流对象.
               FileInputStream in = new FileInputStream("read.txt"); // 文件中为abcde
              // 定义变量,作为有效个数
            int len =0// 定义字节数组,作为装字节数据的容器   
            byte[] b = new byte[2];
            // 循环读取
            while (( len= in.read(b))!=-1) {
                   // 每次读取后,把数组的有效字节部分,变成字符串打印
                System.out.println(new String(b,0,len));//  len 每次读取的有效字节个数
            }
            // 关闭资源
            fis.close();
        }
    }

    综合实例:图片拷贝

        

    public class Copy {
        public static void main(String[] args) throws IOException {
            // 1.创建流对象
            // 1.1 指定数据源
            FileInputStream fis = new FileInputStream("D:\test.jpg");
            // 1.2 指定目的地
            FileOutputStream fos = new FileOutputStream("test_copy.jpg");
    
            // 2.读写数据
            // 2.1 定义数组
            byte[] b = new byte[1024];
            // 2.2 定义长度
            int len;
            // 2.3 循环读取
            while ((len = fis.read(b))!=-1) {
                // 2.4 写出数据
                fos.write(b, 0 , len);
            }
    
            // 3.关闭资源
            fos.close();
            fis.close();
        }
    }

    三、字符流

          1.字节输入流【Reader】

            功能方法:

    • public void close() :关闭此流并释放与此流相关联的任何系统资源。

    • public int read(): 从输入流读取一个字符。

    • public int read(char[] cbuf): 从输入流中读取一些字符,并将它们存储到字符数组 cbuf中 。

        1.1.FileReader类

           构造方法

    • FileReader(File file): 创建一个新的 FileReader ,给定要读取的File对象。

    • FileReader(String fileName): 创建一个新的 FileReader ,给定要读取的文件的名称。

    例:

     

    public class FISRead {
        public static void main(String[] args) throws IOException {
              // 使用文件名称创建流对象
               FileReader fr = new FileReader("read.txt");
              // 定义变量,保存有效字符个数
            int len ;
            // 定义字符数组,作为装字符数据的容器
            char[] cbuf = new char[2];
             
            // 循环读取
            /*while ((len = fr.read(cbuf))!=-1) {
                System.out.println(new String(cbuf,0,len));
            }*/
            String line=null;
            while((line=fr.readLine())!=-1){  //逐行读取
                System.out.println(line);      
    }
            // 关闭资源
            fr.close();
        }
    }

         2.字节输出流【Writer】

           方法:

    • oid write(int c) 写入单个字符。

    • void write(char[] cbuf)写入字符数组。

    • abstract void write(char[] cbuf, int off, int len)写入字符数组的某一部分,off数组的开始索引,len写的字符个数。

    • void write(String str)写入字符串。

    • void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。

    • void flush()刷新该流的缓冲。

    • void close() 关闭此流,但要先刷新它。

         2.1.FileWriter类

           构造方法:

             

    • FileWriter(File file): 创建一个新的 FileWriter,给定要读取的File对象。

    • FileWriter(String fileName): 创建一个新的 FileWriter,给定要读取的文件的名称。

    例:

    public class FWWrite {
        public static void main(String[] args) throws IOException {
            // 使用文件名称创建流对象,可以续写数据
            FileWriter fw = new FileWriter("fw.txt",true);     
            String str1="你好,我詹姆斯"
             char[] aa =str1.toCharArray();  
              fw.writer(aa,0,aa.length);
              // 关闭资源
            fw.close();
        }
    }

    四、properties类 

          基本的存储方法

    • public Object setProperty(String key, String value) : 保存一对属性。

    • public String getProperty(String key) :使用此属性列表中指定的键搜索属性值。

    • public Set<String> stringPropertyNames() :所有键的名称的集合。

            

    public class ProDemo {
        public static void main(String[] args) throws FileNotFoundException {
            // 创建属性集对象
            Properties properties = new Properties();
            // 添加键值对元素
            properties.setProperty("filename", "a.txt");
            properties.setProperty("length", "209385038");
            properties.setProperty("location", "D:\a.txt");
            // 打印属性集对象
            System.out.println(properties);
            // 通过键,获取属性值
            System.out.println(properties.getProperty("filename"));
            System.out.println(properties.getProperty("length"));
            System.out.println(properties.getProperty("location"));
    
            // 遍历属性集,获取所有键的集合
            Set<String> strings = properties.stringPropertyNames();
            // 打印键值对
            for (String key : strings ) {
                  System.out.println(key+" -- "+properties.getProperty(key));
            }
        }
    }

    与流相关的方法

    • public void load(InputStream inStream): 从字节输入流中读取键值对。

  • 相关阅读:
    leetcode : Valid Sudoku
    leetcode : Longest Increasing Subsequence
    leetcode : Search for a Range
    leetcode : Search Insert Position
    leetcode : next permutation
    leetcode : Implement strStr()
    leetcode : Remove Element
    框架:Spring MVC
    笔试:在线编程相关
    J2EE:关系(一对多、多对一、多对多关系)
  • 原文地址:https://www.cnblogs.com/cqyp/p/12421461.html
Copyright © 2011-2022 走看看