• 字节流与字符流


    字节输出流:

        OutputStream此抽象类,是表示输出字节流的所有类的超类。以Stream结尾的都是输出字节流。

        成员方法:

            

    FileOutputStream类:

        OutputStream有很多子类,其中子类FileOutputStream可用来写入数据到文件。FileOutputStream类,即文件输出流,是用于将数据写入 File的输出流。

        构造方法:

            

         

    public class Demo01 {
    
        public static void main(String[] args) throws IOException {
            //明确目的地
            //如果目的地文件不存在,则会创建。
            //如果目的地为文件存在,则会覆盖。
            //new FileOutputStream("D:\io0803\demo10.txt",true);
            //(目的地地址,是否续写)
            FileOutputStream fos=
                    new FileOutputStream("D:\io0803\demo10.txt",true);
            //写一个字节
            fos.write(49);
            fos.write(48);
            fos.write(48);
            //写一个字节数组
            byte[] bytes={-65,-66,-67,-68};
            fos.write(bytes);
            //写字节数组中指定字节
            fos.write(bytes, 1, 2);
            //换行
            fos.write("你好吗
    我很号".getBytes());
            //释放资源
            fos.close();
        }
    
    }

    处理异常:

        

    public class Demo02 {
    
        public static void main(String[] args) {
            //明确目的地
            //如果目的地文件不存在,则会创建。
            //如果目的地为文件存在,则会覆盖。
            //new FileOutputStream("D:\io0803\demo10.txt",true);
            //(目的地地址,是否续写)
    
            //写一个字节
            FileOutputStream fos=null;
            try {
            fos=
                        new FileOutputStream("D:\io0803\demo10.txt",true);
                fos.write(49);
                fos.write(48);
                fos.write(48);
                //写一个字节数组
                byte[] bytes={-65,-66,-67,-68};
                fos.write(bytes);
                //写字节数组中指定字节
                fos.write(bytes, 1, 2);
                //换行
                fos.write("你好吗
    我很号".getBytes());
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                //释放资源
                if(fos!=null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                
            }
        
        }
    
    }

     

     字节输入流:

          InputStream此抽象类,是表示字节输入流的所有类的超类。

          成员方法:

              

                int read():读取一个字节并返回,没有字节返回-1.

               int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。

     FileInputStream类:

        InputStream有很多子类,其中子类FileInputStream可用来读取文件内容。FileInputStream 从文件系统中的某个文件中获得输入字节。

        构造方法:

            

         成员方法:

            

             

          

    public class Demo03 {
    
        public static void main(String[] args) throws IOException {
            //创建字节输入流对象,明确数据源(从哪读);
            FileInputStream fis=
                    new FileInputStream("D:\io0803\demo10.txt");
            //读一个字节
            int len=0;
            while((len=fis.read())!=-1){
                System.out.println((char)len);
            }
            //释放资源
            fis.close();
        }
    
    }
        public static void main(String[] args) throws IOException {
            //创建字节输入流对象,明确数据源
            FileInputStream fis=
                    new FileInputStream("D:\io0803\demo10.txt");
            //一个字节数组一个字节数组读
            byte[] bytes=new byte[2];
            int len=0;
            while((len=fis.read(bytes))!=-1){
                System.out.println(new String(bytes,0,len));
            }
            //释放资源
            fis.close();
        }
    
    }

    一个字节一个字节的复制文件代码:

      

    public class Demo05 {
    
        public static void main(String[] args) throws IOException {
            //明确数据源
            FileInputStream fis=
                    new FileInputStream("D:\io0803\demo10.txt");
            //明确目的地
            FileOutputStream fos=
                    new FileOutputStream("D:\io0803\a\demo10.txt");
            //开始复制(一个字节)
            int len=0;
            while((len=fis.read())!=-1){
                //写一个字节
                fos.write(len);
            }
            //释放资源
            fis.close();
            fos.close();
        }
    
    }

    一个数组一个数组的复制文件:

      

    public class Demo06 {
    
        public static void main(String[] args) throws IOException {
            //明确数据源
            FileInputStream fis=
                    new FileInputStream("D:\io0803\demo10.txt");
            //明确目的地
            FileOutputStream fos=
                    new FileOutputStream("D:\io0803\a\b\demo09.txt");
            //开始复制(一个数组)
            byte bytes[]=new byte[1024];
            int len=0;
            while((len=fis.read(bytes))!=-1){
                fos.write(bytes,0,len);
            }
            //释放资源
            fis.close();
            fos.close();
        }
    
    }

     字符流:

      字符编码表:

          1:ascil码表:ASCII 码使用指定的 7 位或 8 位二进制数组合来表示 128 或 256 种可能的字符。标准 ASCII 码也叫基础ASCII码,使用 7 位二进制数来表示所有的大写和小写字母,数字 0 到 9、标点符号, 以及在美式英语中使用的特殊控制字符。其中:0~31及127(共33个)是控制字符或通信专用字符(其余为可显示字符),如控制符:LF(换行)、CR(回车)、FF(换页)、DEL(删除)、BS(退格)、BEL(振铃)等;通信专用字符:SOH(文头)、EOT(文尾)、ACK(确认)等;ASCII值为 8、9、10 和 13 分别转换为退格、制表、换行和回车字符。它们并没有特定的图形显示,但会依不同的应用程序,而对文本显示有不同的影响。32~126(共95个)是字符(32sp是空格),其中48~57为0到9十个阿拉伯数字; 65~90为26个大写英文字母,97~122号为26个小写英文字母,其余为一些标点符号、运算符号等。

          2:iso-8859-1码表(拉代码表):是国际标准化组织内ISO/IEC 8859的第一个8位字符集

          3:GBK码表:目前最常用的中文码表,2万的中文和符号。用两个字节表示,其中的一部分文字,第一个字节开头是1,第二字节开头是0。

          4:unicode码表:国际标准码表:无论是什么文字,都用两个字节存储。Java中的char类型用的就是这个码表。char c = 'a';占两个字节。 Java中的字符串是按照系统默认码表来解析的。简体中文版 字符串默认的码表是GBK。

          5::基于unicode,一个字节就可以存储数据,不要用两个字节存储,而且这个码表更加的标准化,在每一个字节头加入了编码信息.

            文字--->(数字) :编码。 “abc”.getBytes()  byte[]

            (数字)--->文字  : 解码。 byte[] b={97,98,99}  new String(b,0,len)

    字符输入流Reader:

          

           read():读取单个字符并返回

          read(char[]):将数据读取到数组中,并返回读取的个数。

    FileReader类:

          构造方法:

              

           

    public class Demo01 {
    
        public static void main(String[] args) throws IOException {
            //创建字符输入流对象,明确数据源
        FileReader fr=new FileReader("D:\io0803\demo03.txt");
        //读一个字符
        int len=0;
        while((len=fr.read())!=-1 ){
            System.out.println((char)len);
        }
        fr.close();
        }
    
    }
    public class Demo02 {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
    //创建字符输入流对象,明确数据源
            FileReader fr=new FileReader("D:\io0803\demo03.txt");
            char[] ch=new char[1024];
            int len=0;
            while((len=fr.read(ch))!=-1){
                System.out.println(new String(ch,0,len));
            }
            fr.close();
        }
    
    }

    字符输出流Writer类:

        

    FileWriter类:

        构造方法:

            

         

    public class Demo03 {
    
        public static void main(String[] args) throws IOException {
            //创建字符输出流,明确目的地
            FileWriter fw=new FileWriter("D:\io0803\demo04.txt");
    //写入一个字符
            fw.write(100);
            //字符需要写一下,刷一下
            fw.flush();
            //写入一个字符数组
            char[] ch={'0','好','r'};
            fw.write(ch);
            fw.write(ch,2,1);
            //写入字符串
            fw.write("
    熊大");
            //释放资源
            fw.close();
            
        }
    
    }

     写入字符到文件中,先进行流的刷新,再进行流的关闭。

    flush()和close()区别:

        

         flush():将流中的缓冲区缓冲的数据刷新到目的地中,刷新后,流还可以继续使用。

        close():关闭资源,但在关闭前会将缓冲区中的数据先刷新到目的地,否则丢失数据,然后在关闭流。流不可以使用。如果写入数据多,一定要一边写一边刷新,最后一次可以不刷新,由close完成刷新并关闭。

     复制文本文件:

          

    public class Demo04 {
    
        public static void main(String[] args) throws IOException {
            //明确数据源
            FileReader fr=new FileReader("D:\io0803\demo03.txt");
            //明确目的地
            FileWriter fw=new FileWriter("D:\io0803\a\demo04.txt");
            //开始复制,一个字符一个字符复制
            int len=0;
            while((len=fr.read())!=-1){
                fw.write(len);
            }
            //释放资源
            fr.close();
            fw.close();
        }
    
    }
    public class Demo05 {
    
        public static void main(String[] args) throws IOException {
            //明确数据源
            FileReader fr=new FileReader("D:\io0803\demo03.txt");
            //明确目的地
            FileWriter fw=new FileWriter("D:\io0803\a\demo05.txt");
            //开始复制,一个字符数组一个字符数组复制
            int len=0;
            char[] chars=new char[1024];
            while((len=fr.read(chars))!=-1){
                fw.write(chars,0,len);
                fw.flush();
            }
            //释放资源
            fr.close();
            fw.close();
    
        }
    
    }

       

  • 相关阅读:
    WPF treeview 多层次绑定问题 HierarchicalDataTemplate 和 CompositeCollection
    Difference between Visibility.Collapsed and Visibility.Hidden
    WPF style的继承
    Asp.net个性化服务《系列03_购物车》
    SQLServer2005中的uniqueidentifier数据类型与NEWID()函数
    扉页语
    Asp.net个性化服务《系列01_概述》
    Asp.net个性化服务《系列02_快速的匿名用户个性化》
    2010年java课程体系
    easyui修改提示窗
  • 原文地址:https://www.cnblogs.com/maxuefeng/p/13901185.html
Copyright © 2020-2023  润新知