zoukankan      html  css  js  c++  java
  • java基础篇---I/O技术(二)

    接着上篇http://www.cnblogs.com/oumyye/p/4314412.html

    java I/O流---内存操作流

    ByteArrayInputStream和ByteArrayOutputStream

    ByteArrayInputStream包含一个内部缓冲区,该缓冲区包含从流中读取的字节,内部计数器跟着read方法要提供的下一个字节。
    FileInputStream是把文件当做数据源。ByteArrayInputStream则是把内存中的某一个数组单做数据源。
    ByteArrayOutputStream类实现了一个输出流。其中的数据被写入一个byte数组。缓冲区会随着数据的不断写入而自动增长。可使用toByteArray()和toString()获取数据源的。ByteArrayOutputStream将一个输出流指向一个Byte数组,但这个Byte数组是ByteArrayOutputStream内部内置的,不需要定义。

    操作步骤


    import java.io.* ;
    public class ByteArrayDemo01{
        public static void main(String args[]){
            String str = "HELLOWORLD" ;        // 定义一个字符串,全部由大写字母组成
            ByteArrayInputStream bis = null ;    // 内存输入流
            ByteArrayOutputStream bos = null ;    // 内存输出流
            bis = new ByteArrayInputStream(str.getBytes()) ;    // 向内存中输出内容
            bos = new ByteArrayOutputStream() ;    // 准备从内存ByteArrayInputStream中读取内容
            int temp = 0 ;
            while((temp=bis.read())!=-1){
                char c = (char) temp ;    // 读取的数字变为字符
                bos.write(Character.toLowerCase(c)) ;    // 将字符变为小写
            }
            // 所有的数据就全部都在ByteArrayOutputStream中
            String newStr = bos.toString() ;    // 取出内容
            try{
                bis.close() ;
                bos.close() ;
            }catch(IOException e){
                e.printStackTrace() ;
            }
            System.out.println(newStr) ;
        }
    };

     Java I/O流---过滤流—缓冲流

    BufferedInputStream和BufferedOutputStream

    类BufferedInputStream和BufferedOutputStream继承FilterInputStream和FilterOutputStream,实现了带缓冲的过滤流,它提供了缓冲机制,把任意的I/O流“捆绑”到缓冲流上,可以提高该I/O流的读取效率,在初始化时,除了要指定所连接的I/O流之外,还可以指定缓冲区的大小。
    在读写的同时对数据缓存,这样避免每次读写数据都要进行实际的物理读写操作,在用BufferdOutputStream输出时,数据先输入缓冲区,当缓冲区满的时再写入连接的输出流,可以调用flush()来清空缓冲区。这两个流是处理流,通过内部缓存数组来提高操作流的效率

    使用BufferedInputStream和BufferedOutputStream实现文件的复制
    void copyFile(String src , String dec){
       FileInputStream fis=null;
       BufferedInuptStream bis=null; 
       FileOutputStream fos=null;
       BufferedOutputStream bos=null;
       try {
           fis=new FileInputStream(src);
           fos=new FileOutputStream(dec);
           bis=new BufferedInputStream(fis);
           bos=new BufferedOutputStream(fos);
           while ((temp=bis.read())!=-1) {
               bos.write(temp);
           }
       } catch (FileNotFoundException e) {
            e.printStackTrace();
       }catch (IOException e) {
            e.printStackTrace();
       }finally {
            //增加处理流后,注意流的关闭顺序!“后打开的先关闭”
            try {
                bos.close();
            } catch (IOException e) {
                 e.printStackTrace();
            }
            try {
                bis.close();
            } catch (IOException e) {
                 e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                 e.printStackTrace();
            }
            try {
                fis.close();
            } catch (IOException e) {
                 e.printStackTrace();
            }
       }
    }

    java I/O---数据操作流

    DataInputStream和DataOutputStream

    数据操作流通常在流中写入或读取一个结果是使用,如 网络数据传递,DataInputStream和DataOutputStream提供了可以存取与机器无关的所有java基础类型数据。

    实例 数据写入

    import java.io.DataOutputStream ;
    import java.io.File ;
    import java.io.FileOutputStream ;
    public class DataOutputStreamDemo{
        public static void main(String args[]) throws Exception{    // 所有异常抛出
            DataOutputStream dos = null ;            // 声明数据输出流对象
            File f = new File("d:" + File.separator + "order.txt") ; // 文件的保存路径
            dos = new DataOutputStream(new FileOutputStream(f)) ;    // 实例化数据输出流对象
            String names[] = {"衬衣","手套","围巾"} ;    // 商品名称
            float prices[] = {98.3f,30.3f,50.5f} ;        // 商品价格
            int nums[] = {3,2,1} ;    // 商品数量
            for(int i=0;i<names.length;i++){    // 循环输出
                dos.writeChars(names[i]) ;    // 写入字符串
                dos.writeChar('	') ;    // 写入分隔符
                dos.writeFloat(prices[i]) ; // 写入价格
                dos.writeChar('	') ;    // 写入分隔符
                dos.writeInt(nums[i]) ; // 写入数量
                dos.writeChar('
    ') ;    // 换行
            }
            dos.close() ;    // 关闭输出流
        }
    };

    使用DataOutputStream写入的数据使用DataInputStream读取进来

    import java.io.DataInputStream ;
    import java.io.File ;
    import java.io.FileInputStream ;
    public class DataInputStreamDemo{
        public static void main(String args[]) throws Exception{    // 所有异常抛出
            DataInputStream dis = null ;        // 声明数据输入流对象
            File f = new File("d:" + File.separator + "order.txt") ; // 文件的保存路径
            dis = new DataInputStream(new FileInputStream(f)) ;    // 实例化数据输入流对象
            String name = null ;    // 接收名称
            float price = 0.0f ;    // 接收价格
            int num = 0 ;    // 接收数量
            char temp[] = null ;    // 接收商品名称
            int len = 0 ;    // 保存读取数据的个数
            char c = 0 ;    // 'u0000'
            try{
                while(true){
                    temp = new char[200] ;    // 开辟空间
                    len = 0 ;
                    while((c=dis.readChar())!='	'){    // 接收内容
                        temp[len] = c ;
                        len ++ ;    // 读取长度加1
                    }
                    name = new String(temp,0,len) ;    // 将字符数组变为String
                    price = dis.readFloat() ;    // 读取价格
                    dis.readChar() ;    // 读取	
                    num = dis.readInt() ;    // 读取int
                    dis.readChar() ;    // 读取
    
                    System.out.printf("名称:%s;价格:%5.2f;数量:%d
    ",name,price,num) ;
                }
            }catch(Exception e){}
            dis.close() ;
        }
    };

    java I/O ---打印流

    在整个IO包中,打印流是输出信息最方便的类,主要包含字节打印流(PrintStream)和字符打印流(PrintWriter).打印流提供了非常方便的打印功能,可以打印任何的数据类型。

    打印输入

    import java.io.* ;
    public class PrintDemo02{
        public static void main(String arg[]) throws Exception{
            PrintStream ps = null ;        // 声明打印流对象
            // 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中
            ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;
            String name = "偶my耶" ;    // 定义字符串
            int age = 20 ;                // 定义整数
            float score = 990.356f ;    // 定义小数
            char sex = 'M' ;            // 定义字符
            ps.printf("姓名:%s;年龄:%d;成绩:%f;性别:%c",name,age,score,sex) ;
            ps.close() ;
        }
    };

    BufferReader

    BufferReader是从缓冲区读取内容,所有的输入的字节数据都将放在缓冲区之中。

    import java.io.* ;
    public class BufferedReaderDemo01{
        public static void main(String args[]){
            BufferedReader buf = null ;        // 声明对象
            buf = new BufferedReader(new InputStreamReader(System.in)) ;    // 将字节流变为字符流
            String str = null ;    // 接收输入内容
            System.out.print("请输入内容:") ;
            try{
                str = buf.readLine() ;    // 读取一行数据
            }catch(IOException e){
                e.printStackTrace() ;    // 输出信息
            }
            System.out.println("输入的内容为:" + str) ;
        }
    };
  • 相关阅读:
    Sign Distance Field 2
    矩阵相乘优化
    Editor GUI 的 Gamma Correction
    GPUSkinning 5
    GPUSkinning 2
    RenderTextureFormat.ShadowMap
    战争迷雾
    Texture2DArray(2)
    软件渲染器 YwSoftRenderer
    将 Unity5.3 的老项目升级到 Unity 2018.3 遇到的些许问题。
  • 原文地址:https://www.cnblogs.com/oumyye/p/4316668.html
Copyright © 2011-2022 走看看