黑马程序猿——26,基本数据操作流,字节数组操作流,转换流,编码表

-----------android培训java培训、java学习型技术博客、期待与您交流!

------------ 

          

         黑马程序猿——26,

DataInputStream,DataOutputStream,

ByteArrayInputStream,ByteArrayOutputStream,

InputStreamReader,OutputStreamWriter,编码表

/*

DataInputStream和DataOutputStream

能够更加操作基本数据类型的流

注意这里是Data不是Date!

*/

 
import java.io.*;
class  Ioliou31
{
         public  static void  main(String[] args)throws  IOException
         {
          UTFDemo() ;      
         //  method(); 
         }
         public  static void  method()  throws IOException
         {
           File  f=new   File("f:\科学.txt");  //封装目的文件为对象
           FileOutputStream     fos=new  FileOutputStream(f);//关联目的文件的流
           DataOutputStream     dos=new  DataOutputStream(fos);
           //再用专门操作基本类型数据的流的构造函数接收
 
           //下面注意写入的格式
           //不同的写入格式是依照不同位数写入的
           dos.writeInt(56);
           dos.writeBoolean(false);
           dos.writeDouble(15.24);
     /*  */
           FileInputStream    fis=new  FileInputStream(f);
          DataInputStream  dis=new   DataInputStream (fis);      
           //读取的时候注意要依照顺序读取,以及读取的格式
           //不同的读取格式是依照不同的位数来读取的
           int    i   =dis.readInt();
           boolean  bo=dis.readBoolean();
           double   dou=dis.readDouble();
           soc(i+"---"+bo+"---"+dou);
           //特别注意:什么格式写入的就用什么格式读取出来
 
           dos.close();
           dis.close();             
         }
         public   static void  UTFDemo()  throws IOException
         {
           File   f=new   File("f:\科学.txt");  //封装目的文件为对象
 
 
           FileOutputStream     fos=new  FileOutputStream(f);//关联目的文件的流
           DataOutputStream     dos=new  DataOutputStream(fos);
 
           dos.writeUTF("这是");//四个字节组成一个字符,一共八个字节
          
           FileInputStream    fis=new  FileInputStream(f);
           DataInputStream  dis=new   DataInputStream (fis);
           soc(dis.readUTF());
           //readUTF方法返回的是字符串。用什么格式写就要用什么格式读取
         }
         public  static void  soc(Object  obj)
         {
              System.out.println(obj);           
         }
}

————————切割线————————

/*

操作字节数组的流(内部封装长度可变的字节数组)

ByteArrayInputStream

ByteArrayOutputStream

直接把字节数组传给其ByteArrayInputStream的构造函数,

传进来的字节数组就是源

源设备:

    键盘System.in    硬盘FileStream    内存ArrayStream

目的设备

    控制台System.out   硬盘FileStream   内存ArrayStream

这两个流没有调用底层资源,不用关闭close

另外,还有ByteArrayReader和ByteArrayWriter使用方法也是和上面两者类似。

ByteArrayReader构造函数传入的是字符数组。

*/

import java.io.*;
 
class  Ioliou32
{
         public   static void main(String[] args) throws IOException
         {
                   ByteArrayInputStream   bais=new   ByteArrayInputStream("哦奥额".getBytes());
                   ByteArrayOutputStream   baos=new  ByteArrayOutputStream();
                   int  i=0;
                  while((i=bais.read())!=-1)
                  {
                       baos.write(i);       
                   }
                   File   f=new  File("f:\可乐可乐.java");
                   FileOutputStream    fos=new FileOutputStream(f);
                   baos.writeTo(fos);    //直接写入字节输出流中    
                  
 
         }
         public static void  soc(Object  obj)
         {
            System.out.println(obj);        
         }
}

——————切割线——————

/*

转换流与编码表:

转换流InputStreamReader和OutputStreamWriter

编码表:

   ASCII美国标准信息交换码,用一个字节的7位表示

         ISO8859-1  欧洲码表。拉丁码表不识别中文字符,用一个字节8位表示

         GB2312  中国中文编码表

         GBK     中国中文编码表升级版,两个字节表示一个字符

         Unicode   国际标准码,java使用的编码表

         UTF-8     最多三个字节表示一个字符

*/

import java.io.*;
class  Ioliou33
{
         publicstatic void main(String[] args) throws IOException
         {
       // write();
          read();               
         }
         public  static void  write()  throws IOException
         {
                   File   f=new  File("f:\楼宇.txt");
                   FileOutputStream   fos=new  FileOutputStream(f);
                   OutputStreamWriter   osw=new  OutputStreamWriter(fos,"UTF-8");//要加上编码表版本号
                   osw.write("动手打人了"); 
                   osw.close();
       /*
                   尽管写入之后我们代开f:\楼宇.java文件看到的依然是字符。
                   可是本质上文件存储的是编码表上相应的数字,仅仅只是依照指定编码表兑换成字符而已。
                   */    
         }
         public  static void  read()  throws IOException
         {
                   File   f=new  File("f:\楼宇.txt");
                   FileInputStream   fis=new FileInputStream(f);
                   InputStreamReader   isr=new InputStreamReader(fis,"UTF-8");//编码表版本号要相应上
                   /*
                   读取的时候也是依照存储的数字再依照指定的编码表找出相应的字符
                   */
                   char[]    ch=new  char[20];
                   int  i=0;
                      while ((i=isr.read(ch))!=-1)
                      {
                         soc(new  String(ch,0,i));       
                      }
     
                   isr.close();                      
         }
         public  static void  soc(Object  obj)
         {
             System.out.println(obj);    
         }
 
 
}

——————切割线——————

/*

编码与解码:

经常使用编码表gbk,utf-8,ISO8859-1

*/

import java.io.*;
import java.util.*;
class  Ioliou34
 
{
         public  static void  main(String[] args)throws  Exception
         {
 
                 method2();
         }
         public  static void  method()  throws Exception
         {
                   String   s="開始游戏";
                   byte[]  by=  s.getBytes("utf-8");//依照utf-8编码表进行编码
                   soc(Arrays.toString(by));
                   String     s2=new  String(by,"ISo8859-1");//依照ISo8859-1编码表进行解码
                  soc("s2="+s2);
                   //假设依照不同的编码表进行解码的话会导致错误结果
       //假设遇到这样的情况就须要再一次进行编码解码
                   byte[]    by2=s2.getBytes("ISo8859-1");
                   String   s3=new String(by2,"utf-8");
                   soc("s3="+s3);                        
         }
         public  static void  method2()  throws Exception
         {
                   String   s="開始游戏";
                   byte[]  by=  s.getBytes("utf-8");//依照utf-8编码表进行编码
                   soc(Arrays.toString(by));
                   String   s2=new String(by,"gbk");
                   soc("s2="+s2);
                   //假设依照不同的编码表进行解码的话会导致错误结果
       //假设遇到这样的情况就须要再一次进行编码解码
                   byte[]    by2= s2.getBytes("gbk");
                   soc(Arrays.toString(by2));
                   String    s3=new String(by2,"utf-8");
                   soc("s3="+s3);
                   /*
                   由于gbk和utf-8都是中文编码表,所以,此时的情况与method方法中的不同。
                   method方法中解码过程出错的是由于使用了IOS8895-1拉丁码表,里面不知别中文字符,
                   而这里method2的情况则是解码过程中使用了能够识别中文的gbk码表。
                   解码中遇到对不上号的字符就会去gbk码表的乱码区查找,接着再用gbk码表编码就
                   不是原来的号吗了,所以,这里method2方法的s3就会显示乱码,而method方法的s3则是显示正确。
 
 
       */
         }
         public  static void  soc(Object  obj)
         {
             System.out.println(obj);            
         }
}

-----------android培训java培训、java学习型技术博客、期待与您交流!

------------