黑马程序员-IO字节流的基础应用。。整个IO的分析(字符流和字节流的选择,转换)

来源:互联网 发布:app软件色影 编辑:程序博客网 时间:2024/05/02 00:44
--------------------------------Android培训            Java培训  期待与您的交流!----------------------
//字符流与字节流在需求时分析与选择基本原理/*    1.    源:键盘录入。    目的:控制台。        2.需求:想把键盘录入的数据存储到一个文件中,    源:文件。    目的:控制台。    3,需求:想要讲一个文件的数据打印在控制台上。    源:文件。    目的:控制台。    流操作的基本规律    最难的一个问题是:            流对象有很多,不知道该用哪一个。    通过三个明确来完成。        1.明确源和目的:            源:输入流:InputStream Reader            目的:输出流 OutputStream Writer        2.操作的数据是否是纯文本,            是:字符流。            不是:字节流。        3,当体系明确后,在明确要使用哪个具体的对象,            通过设备进行区分:            源设备:内存,硬盘,键盘            目的设备:内存,硬盘,控制台。分析案例    1.将一个文本文件中数据存储到另一个文件中,复制文件。        源:因为是源,所以使用到读取流。InputStream Reader        是不是操作文本文件。        是!这时就可以选择Reader        这样体系就明确了。                接下来明确要使用该体系中的哪个对象。        明确色胚:硬盘。上一个文件。        Redaer体系中可以操作文件对象的是FileReader        是否提高效率 是:加入Reader体系中缓存区BufferedReader                目的:OutputStream Writer        是否是存文本        是!Writer        设备:硬盘,一个文件。        Writer体系中可以操作文件的对象FileWriter        是否需要提高效率:是!。加入Writer体系中缓存区 BufferedWriter                *****************************************************************        存储时需要将录入的数据按照指定的编码表(UTF-8),将数据存到文件中。        目的:OutputStream Writer        是否是存文本?是!Writer.        设备:硬盘。一个文件。使用FileWriter.        但是FileWriter是使用的默认编码表。GBK。                但是存储时,需要加入指定编码表(utf-8)。而指定的编码表只有转换流可以指定。        所以要使用的对象是OutputStreamWriter.        而该转换流对象要接受一个字节输出流。而且还可以操作的文件的字节输出流。FileOutputStream        OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("XXX.xxx"),"utf-8");        需要高效             需要:BufferedWriter bufw=new BufferedWriter(osw);                所以,转换流什么时候使用。字符和字节之间的桥梁,通常涉及到字符编码转换时,        需要使用到转换流。*//*字节流InputStreamOutputStreamFileInputStreamFileOutputStreamBufferedInputStreamBufferedOutputStream需求:不仅操作文本文件,可能图片,视频,音乐*/import java.io.*;class FileStream {    public static void main(String[] args) throws IOException    {        test4();    }    public static void test4() throws IOException    {        FileInputStream fis= new FileInputStream("demo.txt");        byte[] buf=new byte[fis.available()];        //刚刚好的字节数数组大小        fis.read(buf);        System.out.println(new String(buf));        fis.close();    }    public static void test3() throws IOException    {        FileInputStream fis=new FileInputStream("demo.txt");        byte [] buf=new byte[1024];                    //通过数组缓存1024的大小        int num=0;        while ((num=fis.read(buf))!=-1)        {            System.out.println(new String(buf,0,num));//将数组里面从O到num个字节生成字符串        }        fis.close();    }    public static void test2() throws IOException    {        FileInputStream fis=new FileInputStream("demo.txt");        int ch=0;        while ((ch=fis.read())!=-1)                    //将demo.txt的字节一个一个读取出来        {            System.out.print((char)ch);        }            fis.close();    }    public static  void test1() throws IOException{        FileOutputStream fout=new FileOutputStream("demo.txt");        fout.write("hello java world".getBytes());    //将字符串写入到demo.txt        fout.close();                                //没有刷新,操作对象为最基本的字节,直接写入,但要关闭    }}/*复制一个图片思路:1.用字节读取流对象和图片相关联。2.用字节写入流对象创建一个图片文件,用于存储获取到的图片数据。3.通过循环读写,完成数据的存储。4.关闭资源。自己偷偷的写了一下异常处理*/import java.io.*;class CopyPic {    public static void main(String[] args)     {            FileInputStream fis=null;            FileOutputStream fos=null;        try        {            fis=new FileInputStream("F:\\梦幻.jpg");            fos=new FileOutputStream("F:\\梦幻_copy.jpg");            byte [] buf =new byte[1024];            int len=0;            while ((len=fis.read(buf))!=-1)            {                fos.write(buf,0,len);            }        }        catch (IOException e)        {            System.out.println(e.toString());        }        finally        {            try            {                if(fis!=null)                {                    fis.close();                }            }            catch (IOException e)            {                System.out.println(e.toString());            }                        try            {                if(fos!=null)                {                    fos.close();                }            }            catch (IOException e)            {                System.out.println(e.toString());            }        }    }}/*通过缓存区复制一首MP3的歌曲*/import java.io.*;class BufferedCopy {    public static void main(String[] args) throws IOException    {        copy2();    }    static void copy1() throws IOException    {        //开发中一般都是这种方式创建对象,避免过多的引用对象        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("f:\\古巨基匆匆那年Live.mp3"));        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("f:\\古巨基匆匆那年Live.mp3_copy.mp3"));            int bl=0;        while ((bl=bis.read())!=-1)        {            bos.write(bl);        }        bis.close();        bos.close();       }    static void copy2() throws IOException    {    //通过自己领悟的Buffer原理,        InputStream is=new FileInputStream("f:\\古巨基匆匆那年Live.mp3");        MyBufferedInputStream bis=new MyBufferedInputStream(is);        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("f:\\古巨基匆匆那年Live_copy.mp3"));            int bl=0;        while ((bl=bis.myread())!=-1)        {            bos.write(bl);        }        bis.myclose();        bos.close();        }}/*    读取键盘录入    System.out:对应的标准输出设备,控制台    System.in:对应的编著输入设备,键盘需求:通过键盘录入数据当录入一行数据后,就将该行数据进行打印如果录入的数据是over,那么停止录入。*/import java.io.*;class  ReadIn{    public static void main(String[] args) throws IOException    {        InputStream in=System.in;        StringBuilder sb=new StringBuilder();        int ch=0;        while (true)        {            ch=in.read();                        if (ch=='\r')            {                continue;            }            if(ch=='\n'){                if("over".equals(sb.toString()))                    break;                System.out.println(sb.toString().toUpperCase());                sb.delete(0,sb.length());                            }else{                sb.append((char)ch);            }        }    }}/*    字节流变成字符流输出    字符流变成字节流写入到文件中*/import java.io.*;class TransStream1{    public static void main(String[] args) throws IOException    {    /*    InputStream in=System.in;//创建一个键盘设备输入字节流        InputStreamReader isr=new InputStreamReader(in);//将字节流提升为字符流        BufferedReader br=new BufferedReader(isr);        OutputStream out=System.out;//创建一个输出字节流        OutputStreamWriter osw =new OutputStreamWriter(out);        BufferedWriter bw=new BufferedWriter(osw);*/        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//键盘录入最常见的语法        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));        String line=null;        while ((line=br.readLine())!=null)        {            if("over".equals(line))                                           //添加结束标识                break;            //System.out.println(line.toUpperCase());            bw.write(line.toUpperCase());            bw.newLine();                                                    //跨平台的换行方法            bw.flush();        }        br.close();        bw.close();    }}/*    通过刚才的键盘录入一行并打印其大写,发现其实就是读写一行数据的原理    即BufferedReader对象的readLine方法    思考:    能否直接使用readLine方法来完成键盘录入一行数据的读取ne    键盘录入是字节流InputStream的read方法---->BufferedReader是字符流        是否存在将字节流提升为字符流缓存的类或者方法呢*/import java.io.*;class TransStreamDemo {    public static void main(String[] args) throws IOException    {        InputStream in=System.in;//定义一个键盘录入的字节流        InputStreamReader isr=new InputStreamReader(in);//将字节流提升到字符流        BufferedReader br=new BufferedReader(isr);//将字符流提升至高效的缓存区        String line=null;        while ((line=br.readLine())!=null)        {                if("over".equals(line))                break;            System.out.println(line.toUpperCase());        }        br.close();    }}

0 0
原创粉丝点击