IO流(三)__字节流 标准输入输出流 转换流

一、字节流:FileInputStream 和FileOutputStream

基本操作和字符流类相同,没有flush,但是close还是要的

复制一个字节流文件

private static void copy_1() throws IOException {
        FileInputStream fis=new FileInputStream("d://17-网络编程(TCP协议-练习-上传图片客户端).avi");
        BufferedInputStream bufis=new BufferedInputStream(fis);        
        FileOutputStream fos=new FileOutputStream("d://2.avi");
        BufferedOutputStream bufos=new BufferedOutputStream(fos);
        
        /*byte[] buf =new byte[1024];
        int len=0;

      while((len=fis.read(buf))!=-1){
      fos.write(buf);*/

        int ch=0;
        while((ch=bufis.read())!=-1){
            bufos.write(ch);          
        }
        fos.close();
        fis.close();
    }

int len=fis.available() 获得可读取的字节数

private static void demo_read() throws IOException {
    FileInputStream fis=new FileInputStream("demo.txt");
        byte[] buf=new byte[fis.available()];//获取文件大小,但是如果文件过大就会出现溢出
        fis.read(buf);
        System.out.println(new String(buf) );
    }

 

二、system.insystem.out标准输入输出流InputStream OutputStream

阻塞的意思就是说: 程序一直停在read()方法这里,等待数据。没有数据就不继续往下执行,至到得到数据。

何谓标准输入输出的呢?就是我们最常见的,一般都是命令行窗口,System.in负责从健盘输入,System.out负责从命令窗口输出 

private static void readKey() throws IOException {
        InputStream in=System.in;
        int ch=in.read();//阻塞式方法
        System.out.println(ch);
        int ch1=in.read();//阻塞式方法
        System.out.println(ch1);
        int ch2=in.read();//阻塞式方法
        System.out.println(ch2);
        
        //in.close();//系统流不要关闭,关闭之后就不能再打开
        
        /*InputStream in2=System.in;  
        int ch3=in2.read();
        System.out.println(ch3);*/
        
    }

练习:获取用户键盘录入的数据,并将数据变成大写显示在控制台上,如果用户输入的是over,结束键盘录入。

思路:
*1.因为键盘录入只读取一个字节,判断是否是over
*需要将读取到的字节拼成字符串,
*2.那就需要一个容器。StringBuilder
*3.在用户回车之前将录入的数据变成字符串判断即可

private static void readKey2() throws IOException {       
        //1.创建容器
        StringBuilder sb=new StringBuilder();
        //2.获取键盘读取流
        InputStream in=System.in;
        //3.定义变量记录读取到的字节,并循环获取
        
        int ch=0;
        while((ch=in.read())!=-1){
            //在存储之前需要判断是否是换行标记,因为换行标记不存储
            if(ch=='\r')
                continue;
            if(ch=='\n'){
                String temp=sb.toString();
                if("over".equals(temp))
                    break;
                System.out.println(temp.toUpperCase());
                sb.delete(0,sb.length());
            }
            else
                sb.append((char)ch);
        
        }
    }

注意:\r \n在windows下都是换行控制符。在此程序中交换\r和\n没有影响。

 

三、转换流

什么时候使用转换流?
1.源或者目的对应的设备是字节流。但是操作的确实文本数据,可以使用转换作为桥梁提高对文本操作的便捷
2.一旦操作文本设计具体的指定编码表时,必须使用转换流。

public class TransStreamDemo {

    public static void main(String[] args) throws IOException {
        //字节流
        InputStream in= System.in;
        //将字节流转成字符的桥梁。转换流
        InputStreamReader isr=new InputStreamReader(in);
        //缓冲字符流
        BufferedReader buff=new BufferedReader(isr);   
String line
=null; while((line=buff.readLine())!=null){ if("over".equals(line)) break; System.out.println(line.toUpperCase()); } } }

OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("gbk_1.txt"),"GBK");
FileWriter fw=new FileWriter("gbk_1.txt"); 
* 这两句代码的功能是等同的
* FileWriter:其实就是转换流指定了本机默认码表的体现。而且这个转换流的子类对象,可以方便操作文本文件
* 简单说:操作文件的字节流+本机默认的编码表
* 这就是按照默认码表来操作文件的便捷类。
* 如果文本文件需要明确具体的码表,FileWriter就不行了。必须用转换流。

 

练习:

* 1.需求:将键盘录入的数据写入到一个文件当中
* 2.需求:讲一个文本文件内容显示在控制台上
* 3.需求:将一个文本文件中的内容复制到另一个文件中

public class TransStreamDemo2 {
    public static void main(String[] args) throws IOException {
        //1.需求:将键盘录入的数据写入到一个文件当中
        /*BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a.txt")));
        */
        //2.需求:讲一个文本文件内容显示在控制台上
        /*BufferedReader bufr=new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
        BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(System.out));*/
        //3.需求:将一个文本文件中的内容复制到另一个文件中
        BufferedReader bufr=new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
        BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("b.txt")));
        
        String line=null;
        while((line=bufr.readLine())!=null){
            if("over".equals(line))
                break;
            bufw.write(line.toUpperCase());
            bufw.newLine();
            bufw.flush();
        }
        
    }

}
posted @ 2016-10-07 12:42  开拖拉机的蜡笔小新  阅读(700)  评论(0编辑  收藏  举报