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)
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,将数据写入到输出流中。
testDataOutputStream() ;
// 测试DataInputStream,从上面的输出流结果中读取数据。
testDataInputStream() ;
}
/**
* DataOutputStream的API测试函数
*/
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严12");
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* DataInputStream的API测试函数
*/
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对应的16进制的字符串
private static String byteToHexString(byte val) {
return Integer.toHexString(val & 0xff);
}
// 打印char对应的16进制的字符串
private static String charToHexString(char val) {
return Integer.toHexString(val);
}
// 打印short对应的16进制的字符串
private static String shortToHexString(short val) {
return Integer.toHexString(val & 0xffff);
}
}
byteToHexString(0x8F):0x8f charToHexString(0x8FCF):0x8fcf readBoolean():true readByte():0x41 readChar():0x4243 readShort():0x4445 readInt():0x12345678 readLong():0xfedcba987654321 readUTF():abcdefghijklmnopqrstuvwxyz严12
private static String byteToHexString(byte val) {
return Integer.toHexString(val & 0xff);
}
return Integer.toHexString(val & 0xff);
return Integer.toHexString(val);
private static String charToHexString(char val) {
return Integer.toHexString(val);
}
private static String shortToHexString(short val) {
return Integer.toHexString(val & 0xffff);
}
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,将数据写入到输出流中。
testDataOutputStream() ;
// 测试DataInputStream,从上面的输出流结果中读取数据。
testDataInputStream() ;
}
/**
* DataOutputStream的API测试函数
*/
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严12");
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* DataInputStream的API测试函数
*/
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对应的16进制的字符串
private static String byteToHexString(byte val) {
return Integer.toHexString(val & 0xff);
}
// 打印char对应的16进制的字符串
private static String charToHexString(char val) {
return Integer.toHexString(val);
}
// 打印short对应的16进制的字符串
private static String shortToHexString(short val) {
return Integer.toHexString(val & 0xffff);
}
}
byteToHexString(0x8F):0x8f charToHexString(0x8FCF):0x8fcf readBoolean():true readByte():0x41 readChar():0x4243 readShort():0x4445 readInt():0x12345678 readLong():0xfedcba987654321 readUTF():abcdefghijklmnopqrstuvwxyz严12
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有