IO流 字节输出流

这是数据输出到哪个位置

//创建字节输出流对象
        //构造方法指定文件地址时,若果存在则覆盖
            //如果不存在则创建
        FileOutputStream fos=new FileOutputStream("E:\\Java0322\\d.txt");
    /*    //向文件中写入一个字节write(int b) ASCII码
        //0----48
        //a----97
        //A----65
        fos.write(49);
        fos.write(48);
        fos.write(48);*/
        /*//向文件中写入一个字节数组write(byte[] b)
        byte[] bytes={-66,-67,-68,-69};
        fos.write(bytes);*/
        byte[] bytes={-66,-67,-68,-69};
        fos.write(bytes,2,2);
        //释放资源
        fos.close();

这是开启续写的功能

//创建字节输出流(开启续写 true)
        FileOutputStream fos=new FileOutputStream("E:\\Java0322\\d.txt",true);
        //字符串——>字节数组  getBytes()
        fos.write("abc".getBytes());
        //换行——>\r\n
        fos.write("\r\n换行了".getBytes());
        //释放资源
        fos.close();

 

这是把数据从磁盘中读出来的   FileInputStream

//创建字节输入流(明确从哪个文件读取数据)
        FileInputStream fis=new
                FileInputStream("E:\\Java0322\\d.txt");
        //读取一个字节
        /*int len=fis.read();
        System.out.println((char)len);
        len=fis.read();
        System.out.println((char)len);
        len=fis.read();
        System.out.println((char)len);
        len=fis.read();
        System.out.println((char)len);
        len=fis.read();
        System.out.println((char)len);
        len=fis.read();
        System.out.println(len);*/
        int len=0;
        while((len=fis.read())!=-1){
            System.out.println((char)len);
        }
        //释放资源
        fis.close();
    }

 

posted on 2019-06-15 14:08  默示う梦璃  阅读(127)  评论(0)    收藏  举报