Java读写二进制数据以及byte[]与各种数据类型互相转换示例

1.将数据写入二进制文件

[java] view plain copy
  1. public void wirteBinary() {  
  2.         try {  
  3.             DataOutputStream os = new DataOutputStream(  
  4.                     new BufferedOutputStream(new FileOutputStream(  
  5.                             "E:\\data.dat")));  
  6.             os.writeInt(1001);  
  7.             os.writeByte(520);  
  8.             os.writeBoolean(true);  
  9.             os.writeFloat(10.0f);  
  10.             os.writeLong(100l);  
  11.             os.writeUTF("读写二进制文件");  
  12.               
  13.             os.flush();  
  14.             os.close();  
  15.               
  16.         } catch (IOException e) {  
  17.   
  18.         }  
  19.     }  

2.从文件中读取二进制数据
[java] view plain copy
  1. public void readBinary() {  
  2.         try {  
  3.             DataInputStream is = new DataInputStream(  
  4.                     new BufferedInputStream(new FileInputStream(  
  5.                             "E:\\data.dat")));  
  6.             System.out.println(is.readInt());  
  7.             System.out.println(is.readByte());  
  8.             System.out.println(is.readBoolean());  
  9.             System.out.println(is.readFloat());  
  10.             System.out.println(is.readLong());  
  11.             System.out.println(is.readUTF());  
  12.               
  13.             is.close();  
  14.         } catch (IOException e) {  
  15.   
  16.         }  
  17.     }  

看一下程序执行的结果:


byte[]与各种数据类型互相转换示例

在socket开发过程中,通常需要将一些具体的值(这些值可能是各种JAVA类型)转化为byte[]类型,为此我总结了如下这个示例,贴出来,以便经常翻看

 

Java代码   收藏代码
  1. public class TestCase {  
  2.       
  3.     /** 
  4.      * short到字节数组的转换. 
  5.      */  
  6.     public static byte[] shortToByte(short number) {  
  7.         int temp = number;  
  8.         byte[] b = new byte[2];  
  9.         for (int i = 0; i < b.length; i++) {  
  10.             b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位  
  11.             temp = temp >> 8;// 向右移8位  
  12.         }  
  13.         return b;  
  14.     }  
  15.   
  16.     /** 
  17.      * 字节数组到short的转换. 
  18.      */  
  19.     public static short byteToShort(byte[] b) {  
  20.         short s = 0;  
  21.         short s0 = (short) (b[0] & 0xff);// 最低位  
  22.         short s1 = (short) (b[1] & 0xff);  
  23.         s1 <<= 8;  
  24.         s = (short) (s0 | s1);  
  25.         return s;  
  26.     }  
  27.       
  28.       
  29.     /** 
  30.      * int到字节数组的转换. 
  31.      */  
  32.     public static byte[] intToByte(int number) {  
  33.         int temp = number;  
  34.         byte[] b = new byte[4];  
  35.         for (int i = 0; i < b.length; i++) {  
  36.             b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位  
  37.             temp = temp >> 8;// 向右移8位  
  38.         }  
  39.         return b;  
  40.     }  
  41.   
  42.     /** 
  43.      * 字节数组到int的转换. 
  44.      */  
  45.     public static int byteToInt(byte[] b) {  
  46.         int s = 0;  
  47.         int s0 = b[0] & 0xff;// 最低位  
  48.         int s1 = b[1] & 0xff;  
  49.         int s2 = b[2] & 0xff;  
  50.         int s3 = b[3] & 0xff;  
  51.         s3 <<= 24;  
  52.         s2 <<= 16;  
  53.         s1 <<= 8;  
  54.         s = s0 | s1 | s2 | s3;  
  55.         return s;  
  56.     }  
  57.       
  58.       
  59.     /** 
  60.      * long类型转成byte数组 
  61.      */  
  62.     public static byte[] longToByte(long number) {  
  63.         long temp = number;  
  64.         byte[] b = new byte[8];  
  65.         for (int i = 0; i < b.length; i++) {  
  66.             b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位 temp = temp  
  67.                                                         // >> 8;// 向右移8位  
  68.         }  
  69.         return b;  
  70.     }  
  71.   
  72.     /** 
  73.      * 字节数组到long的转换. 
  74.      */  
  75.     public static long byteToLong(byte[] b) {  
  76.         long s = 0;  
  77.         long s0 = b[0] & 0xff;// 最低位  
  78.         long s1 = b[1] & 0xff;  
  79.         long s2 = b[2] & 0xff;  
  80.         long s3 = b[3] & 0xff;  
  81.         long s4 = b[4] & 0xff;// 最低位  
  82.         long s5 = b[5] & 0xff;  
  83.         long s6 = b[6] & 0xff;  
  84.         long s7 = b[7] & 0xff;  
  85.   
  86.         // s0不变  
  87.         s1 <<= 8;  
  88.         s2 <<= 16;  
  89.         s3 <<= 24;  
  90.         s4 <<= 8 * 4;  
  91.         s5 <<= 8 * 5;  
  92.         s6 <<= 8 * 6;  
  93.         s7 <<= 8 * 7;  
  94.         s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;  
  95.         return s;  
  96.     }  
  97.       
  98.     /** 
  99.      * double到字节数组的转换. 
  100.      */  
  101.     public static byte[] doubleToByte(double num) {    
  102.         byte[] b = new byte[8];    
  103.         long l = Double.doubleToLongBits(num);    
  104.         for (int i = 0; i < 8; i++) {    
  105.             b[i] = new Long(l).byteValue();    
  106.             l = l >> 8;    
  107.         }  
  108.         return b;  
  109.     }  
  110.       
  111.     /** 
  112.      * 字节数组到double的转换. 
  113.      */  
  114.     public static double getDouble(byte[] b) {    
  115.         long m;    
  116.         m = b[0];    
  117.         m &= 0xff;    
  118.         m |= ((long) b[1] << 8);    
  119.         m &= 0xffff;    
  120.         m |= ((long) b[2] << 16);    
  121.         m &= 0xffffff;    
  122.         m |= ((long) b[3] << 24);    
  123.         m &= 0xffffffffl;    
  124.         m |= ((long) b[4] << 32);    
  125.         m &= 0xffffffffffl;    
  126.         m |= ((long) b[5] << 40);    
  127.         m &= 0xffffffffffffl;    
  128.         m |= ((long) b[6] << 48);    
  129.         m &= 0xffffffffffffffl;    
  130.         m |= ((long) b[7] << 56);    
  131.         return Double.longBitsToDouble(m);    
  132.     }  
  133.       
  134.       
  135.     /** 
  136.      * float到字节数组的转换. 
  137.      */  
  138.     public static void floatToByte(float x) {  
  139.         //先用 Float.floatToIntBits(f)转换成int  
  140.     }  
  141.       
  142.     /** 
  143.      * 字节数组到float的转换. 
  144.      */  
  145.     public static float getFloat(byte[] b) {    
  146.         // 4 bytes    
  147.         int accum = 0;    
  148.         for ( int shiftBy = 0; shiftBy < 4; shiftBy++ ) {    
  149.                 accum |= (b[shiftBy] & 0xff) << shiftBy * 8;    
  150.         }    
  151.         return Float.intBitsToFloat(accum);    
  152.     }    
  153.   
  154.      /** 
  155.       * char到字节数组的转换. 
  156.       */  
  157.      public static byte[] charToByte(char c){  
  158.         byte[] b = new byte[2];  
  159.         b[0] = (byte) ((c & 0xFF00) >> 8);  
  160.         b[1] = (byte) (c & 0xFF);  
  161.         return b;  
  162.      }  
  163.        
  164.      /** 
  165.       * 字节数组到char的转换. 
  166.       */  
  167.      public static char byteToChar(byte[] b){  
  168.         char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));  
  169.         return c;  
  170.      }  
  171.       
  172.     /** 
  173.      * string到字节数组的转换. 
  174.      */  
  175.     public static byte[] stringToByte(String str) throws UnsupportedEncodingException{  
  176.         return str.getBytes("GBK");  
  177.     }  
  178.       
  179.     /** 
  180.      * 字节数组到String的转换. 
  181.      */  
  182.     public static String bytesToString(byte[] str) {  
  183.         String keyword = null;  
  184.         try {  
  185.             keyword = new String(str,"GBK");  
  186.         } catch (UnsupportedEncodingException e) {  
  187.             e.printStackTrace();  
  188.         }  
  189.         return keyword;  
  190.     }  
  191.       
  192.       
  193.     /** 
  194.      * object到字节数组的转换 
  195.      */  
  196.     @Test  
  197.     public void testObject2ByteArray() throws IOException,  
  198.             ClassNotFoundException {  
  199.         // Object obj = "";  
  200.         Integer[] obj = { 1, 3, 4 };  
  201.   
  202.         // // object to bytearray  
  203.         ByteArrayOutputStream bo = new ByteArrayOutputStream();  
  204.         ObjectOutputStream oo = new ObjectOutputStream(bo);  
  205.         oo.writeObject(obj);  
  206.         byte[] bytes = bo.toByteArray();  
  207.         bo.close();  
  208.         oo.close();  
  209.         System.out.println(Arrays.toString(bytes));  
  210.   
  211.         Integer[] intArr = (Integer[]) testByteArray2Object(bytes);  
  212.         System.out.println(Arrays.asList(intArr));  
  213.   
  214.   
  215.         byte[] b2 = intToByte(123);  
  216.         System.out.println(Arrays.toString(b2));  
  217.   
  218.         int a = byteToInt(b2);  
  219.         System.out.println(a);  
  220.   
  221.     }  
  222.   
  223.     /** 
  224.      * 字节数组到object的转换. 
  225.      */  
  226.     private Object testByteArray2Object(byte[] bytes) throws IOException,  
  227.             ClassNotFoundException {  
  228.         // byte[] bytes = null;  
  229.         Object obj;  
  230.         // bytearray to object  
  231.         ByteArrayInputStream bi = new ByteArrayInputStream(bytes);  
  232.         ObjectInputStream oi = new ObjectInputStream(bi);  
  233.         obj = oi.readObject();  
  234.         bi.close();  
  235.         oi.close();  
  236.         System.out.println(obj);  
  237.         return obj;  
  238.     }  
  239.   


  • 8
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值