最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java的DataInputStream和DataOutputStream數(shù)據(jù)輸入輸出流

 更新時間:2016年06月27日 08:59:32   作者:skywangkw  
這里我們來看一下Java的DataInputStream和DataOutputStream數(shù)據(jù)輸入輸出流的使用示例,兩個類分別繼承于FilterInputStream和FilterOutputStream:

DataInputStream 
DataInputStream 是數(shù)據(jù)輸入流。它繼承于FilterInputStream。
DataInputStream 是用來裝飾其它輸入流,它“允許應(yīng)用程序以與機器無關(guān)方式從底層輸入流中讀取基本 Java 數(shù)據(jù)類型”。應(yīng)用程序可以使用DataOutputStream(數(shù)據(jù)輸出流)寫入由DataInputStream(數(shù)據(jù)輸入流)讀取的數(shù)據(jù)。
DataInputStream 函數(shù)列表:

DataInputStream(InputStream in)
final int  read(byte[] buffer, int offset, int length)
final int  read(byte[] buffer)
final boolean  readBoolean()
final byte  readByte()
final char  readChar()
final double  readDouble()
final float  readFloat()
final void  readFully(byte[] dst)
final void  readFully(byte[] dst, int offset, int byteCount)
final int  readInt()
final String  readLine()
final long  readLong()
final short  readShort()
final static String  readUTF(DataInput in)
final String  readUTF()
final int  readUnsignedByte()
final int  readUnsignedShort()
final int  skipBytes(int count)

示例代碼:
關(guān)于DataInputStream中API的詳細(xì)用法:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.SecurityException;

/**
 * DataInputStream 和 DataOutputStream測試程序
 *
 * @author skywang
 */
public class DataInputStreamTest {

 private static final int LEN = 5;

 public static void main(String[] args) {
  // 測試DataOutputStream,將數(shù)據(jù)寫入到輸出流中。
  testDataOutputStream() ;
  // 測試DataInputStream,從上面的輸出流結(jié)果中讀取數(shù)據(jù)。
  testDataInputStream() ;
 }

 /**
  * DataOutputStream的API測試函數(shù)
  */
 private static void testDataOutputStream() {

  try {
   File file = new File("file.txt");
   DataOutputStream out =
     new DataOutputStream(
      new FileOutputStream(file));

   out.writeBoolean(true);
   out.writeByte((byte)0x41);
   out.writeChar((char)0x4243);
   out.writeShort((short)0x4445);
   out.writeInt(0x12345678);
   out.writeLong(0x0FEDCBA987654321L);

   out.writeUTF("abcdefghijklmnopqrstuvwxyz嚴(yán)12");

   out.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (SecurityException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 /**
  * DataInputStream的API測試函數(shù)
  */
 private static void testDataInputStream() {

  try {
   File file = new File("file.txt");
   DataInputStream in =
     new DataInputStream(
      new FileInputStream(file));

   System.out.printf("byteToHexString(0x8F):0x%s\n", byteToHexString((byte)0x8F));
   System.out.printf("charToHexString(0x8FCF):0x%s\n", charToHexString((char)0x8FCF));

   System.out.printf("readBoolean():%s\n", in.readBoolean());
   System.out.printf("readByte():0x%s\n", byteToHexString(in.readByte()));
   System.out.printf("readChar():0x%s\n", charToHexString(in.readChar()));
   System.out.printf("readShort():0x%s\n", shortToHexString(in.readShort()));
   System.out.printf("readInt():0x%s\n", Integer.toHexString(in.readInt()));
   System.out.printf("readLong():0x%s\n", Long.toHexString(in.readLong()));
   System.out.printf("readUTF():%s\n", in.readUTF());

   in.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (SecurityException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 // 打印byte對應(yīng)的16進制的字符串
 private static String byteToHexString(byte val) {
  return Integer.toHexString(val & 0xff);
 }

 // 打印char對應(yīng)的16進制的字符串
 private static String charToHexString(char val) {
  return Integer.toHexString(val);
 }

 // 打印short對應(yīng)的16進制的字符串
 private static String shortToHexString(short val) {
  return Integer.toHexString(val & 0xffff);
 }
}

運行結(jié)果:

byteToHexString(0x8F):0x8f
charToHexString(0x8FCF):0x8fcf
readBoolean():true
readByte():0x41
readChar():0x4243
readShort():0x4445
readInt():0x12345678
readLong():0xfedcba987654321
readUTF():abcdefghijklmnopqrstuvwxyz嚴(yán)12

結(jié)果說明:
(1) 查看file.txt文本。16進制的數(shù)據(jù)顯示如下:

201662785355523.jpg (717×79)

001f 對應(yīng)的int值是31。它表示的含義是后面的UTF-8數(shù)據(jù)的長度。字符串“abcdefghijklmnopqrstuvwxyz嚴(yán)12”中字母“ab...xyz”的長度是26,“嚴(yán)”對應(yīng)的UTF-8數(shù)據(jù)長度是3;“12”長度是2??偟拈L度=26+3+2=31。
(2) 返回byte對應(yīng)的16進制的字符串
源碼如下:

private static String byteToHexString(byte val) {
 return Integer.toHexString(val & 0xff);
}

想想為什么代碼是:

return Integer.toHexString(val & 0xff);

而不是

return Integer.toHexString(val);

我們先看看 byteToHexString((byte)0x8F); 在上面兩種情況下的輸出結(jié)果。
return Integer.toHexString(val & 0xff); 對應(yīng)的輸出是“0xffffff8f”
return Integer.toHexString(val); 對應(yīng)的輸出是“0x8f”
為什么會這樣呢?
原因其實很簡單,就是“byte類型轉(zhuǎn)換成int類型”導(dǎo)致的問題。
byte類型的0x8F是一個負(fù)數(shù),它對應(yīng)的2進制是10001111;將一個負(fù)數(shù)的byte轉(zhuǎn)換成int類型時,執(zhí)行的是有符號轉(zhuǎn)型(新增位都填充符號位的數(shù)字)。0x8F的符號位是1,因為將它轉(zhuǎn)換成int時,填充“1”;轉(zhuǎn)型后的結(jié)果(2進制)是11111111 11111111 11111111 10001111,對應(yīng)的16進制為0xffffff8f。
因為當(dāng)我們執(zhí)行Integer.toHexString(val);時,返回的就是0xffffff8f。
在Integer.toHexString(val & 0xff)中,相當(dāng)于0xffffff8f & 0xff,得到的結(jié)果是0x8f。
(3) 返回char和short對應(yīng)的16進制的字符串
“返回char對應(yīng)的16進制的字符串”對應(yīng)的源碼如下:

private static String charToHexString(char val) {
 return Integer.toHexString(val);
}

“返回short對應(yīng)的16進制的字符串”對應(yīng)源碼如下:

private static String shortToHexString(short val) {
 return Integer.toHexString(val & 0xffff);
}

比較上面的兩個函數(shù),為什么一個是 “val” ,而另一個是 “val & 0xffff”?
通過(2)的分析,我們類似的推出為什么 “返回short對應(yīng)的16進制的字符串” 要執(zhí)行“val & 0xffff”。
但是,為什么 “返回char對應(yīng)的16進制的字符串” 要執(zhí)行 “val” 即可。原因也很簡單,java中char是無符號類型,占兩個字節(jié)。將char轉(zhuǎn)換為int類型,執(zhí)行的是無符號轉(zhuǎn)型,新增為都填充0。


DataOutputStream
DataOutputStream 是數(shù)據(jù)輸出流。它繼承于FilterOutputStream。
DataOutputStream 是用來裝飾其它輸出流,將DataOutputStream和DataInputStream輸入流配合使用,“允許應(yīng)用程序以與機器無關(guān)方式從底層輸入流中讀寫基本 Java 數(shù)據(jù)類型”。
示例代碼
關(guān)于DataOutStream中API的詳細(xì)用法:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.SecurityException;

/**
 * DataInputStream 和 DataOutputStream測試程序
 *
 * @author skywang
 */
public class DataInputStreamTest {

 private static final int LEN = 5;

 public static void main(String[] args) {
  // 測試DataOutputStream,將數(shù)據(jù)寫入到輸出流中。
  testDataOutputStream() ;
  // 測試DataInputStream,從上面的輸出流結(jié)果中讀取數(shù)據(jù)。
  testDataInputStream() ;
 }

 /**
  * DataOutputStream的API測試函數(shù)
  */
 private static void testDataOutputStream() {

  try {
   File file = new File("file.txt");
   DataOutputStream out =
     new DataOutputStream(
      new FileOutputStream(file));

   out.writeBoolean(true);
   out.writeByte((byte)0x41);
   out.writeChar((char)0x4243);
   out.writeShort((short)0x4445);
   out.writeInt(0x12345678);
   out.writeLong(0x0FEDCBA987654321L);

   out.writeUTF("abcdefghijklmnopqrstuvwxyz嚴(yán)12");

   out.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (SecurityException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 /**
  * DataInputStream的API測試函數(shù)
  */
 private static void testDataInputStream() {

  try {
   File file = new File("file.txt");
   DataInputStream in =
     new DataInputStream(
      new FileInputStream(file));

   System.out.printf("byteToHexString(0x8F):0x%s\n", byteToHexString((byte)0x8F));
   System.out.printf("charToHexString(0x8FCF):0x%s\n", charToHexString((char)0x8FCF));

   System.out.printf("readBoolean():%s\n", in.readBoolean());
   System.out.printf("readByte():0x%s\n", byteToHexString(in.readByte()));
   System.out.printf("readChar():0x%s\n", charToHexString(in.readChar()));
   System.out.printf("readShort():0x%s\n", shortToHexString(in.readShort()));
   System.out.printf("readInt():0x%s\n", Integer.toHexString(in.readInt()));
   System.out.printf("readLong():0x%s\n", Long.toHexString(in.readLong()));
   System.out.printf("readUTF():%s\n", in.readUTF());

   in.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (SecurityException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 // 打印byte對應(yīng)的16進制的字符串
 private static String byteToHexString(byte val) {
  return Integer.toHexString(val & 0xff);
 }

 // 打印char對應(yīng)的16進制的字符串
 private static String charToHexString(char val) {
  return Integer.toHexString(val);
 }

 // 打印short對應(yīng)的16進制的字符串
 private static String shortToHexString(short val) {
  return Integer.toHexString(val & 0xffff);
 }
}

運行結(jié)果:

byteToHexString(0x8F):0x8f
charToHexString(0x8FCF):0x8fcf
readBoolean():true
readByte():0x41
readChar():0x4243
readShort():0x4445
readInt():0x12345678
readLong():0xfedcba987654321
readUTF():abcdefghijklmnopqrstuvwxyz嚴(yán)12

相關(guān)文章

  • Java實現(xiàn)發(fā)送郵件功能時碰到的坑

    Java實現(xiàn)發(fā)送郵件功能時碰到的坑

    之前用163郵箱發(fā)郵件時明明是成功的,但是使用中國移動自己的郵箱時,無論如何在linux服務(wù)器中都發(fā)送不成功。下面小編給大家說下我是怎么解決的,一起看下吧
    2016-06-06
  • SpringBoot下token短信驗證登入登出權(quán)限操作(token存放redis,ali短信接口)

    SpringBoot下token短信驗證登入登出權(quán)限操作(token存放redis,ali短信接口)

    這篇文章主要介紹了SpringBoot下token短信驗證登入登出權(quán)限操作(token存放redis,ali短信接口),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Java數(shù)據(jù)類型的全面剖析

    Java數(shù)據(jù)類型的全面剖析

    這篇文章主要介紹了Java基本數(shù)據(jù)類型,結(jié)合實例形式詳細(xì)分析了java基本數(shù)據(jù)類型、數(shù)據(jù)類型范圍、易錯題等相關(guān)原理與操作技巧,需要的朋友可以參考下
    2021-10-10
  • Java實現(xiàn)的具有GUI的校園導(dǎo)航系統(tǒng)的完整代碼

    Java實現(xiàn)的具有GUI的校園導(dǎo)航系統(tǒng)的完整代碼

    這篇文章主要介紹了Java實現(xiàn)的具有GUI的校園導(dǎo)航系統(tǒng)的完整代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • Maven倉庫鏡像配置的方法實現(xiàn)

    Maven倉庫鏡像配置的方法實現(xiàn)

    本文介紹了如何使用Maven倉庫鏡像來加速構(gòu)建過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • Spring實現(xiàn)動態(tài)數(shù)據(jù)源切換的方法總結(jié)

    Spring實現(xiàn)動態(tài)數(shù)據(jù)源切換的方法總結(jié)

    這篇文章主要為大家詳細(xì)介紹了一種Spring實現(xiàn)動態(tài)數(shù)據(jù)源切換的方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-06-06
  • Zookeeper全局唯一ID生成方案解析

    Zookeeper全局唯一ID生成方案解析

    這篇文章主要介紹了Zookeeper全局唯一ID生成方案解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-12-12
  • 最新評論

    剑川县| 丽江市| 屏边| 太原市| 武山县| 浠水县| 思茅市| 达孜县| 卢氏县| 永新县| 理塘县| 醴陵市| 大余县| 镇江市| 永兴县| 南昌县| 桐乡市| 佳木斯市| 五河县| 东海县| 河东区| 清涧县| 旬邑县| 崇信县| 石柱| 贵州省| 通许县| 高清| 琼结县| 阳朔县| 明水县| 大姚县| 阜宁县| 三都| 化州市| 宝应县| 华坪县| 海淀区| 四会市| 灵台县| 靖宇县|