首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

DataOutputStream 种 和DatainputStream类 的主要方法简单介绍,及代码演示

2012-12-24 
DataOutputStream 类 和DatainputStream类的主要方法简单介绍,及代码演示。DataOutputStream数据输出流 将j

DataOutputStream 类 和DatainputStream类 的主要方法简单介绍,及代码演示。

DataOutputStream数据输出流 将java基本数据类型写入数据输出流中。并可以通过数据输入流DataInputStream将数据读入。

DataOutputStream类

构造函数:

DataOutputStream(OutputStream out);//创建一个将数据写入指定输出流out的数据输出流。

字段摘要:

int written;//到目前为止写入数据流的字节数。

主要方法:

void write(byte[] b,int off,int len);//将byte数组off角标开始的len个字节写到OutputStream 输出流对象中。

void write(int b);//将指定字节的最低8位写入基础输出流。

void writeBoolean(boolean b);//将一个boolean值以1-byte形式写入基本输出流。

void writeByte(int v);//将一个byte值以1-byte值形式写入到基本输出流中。

void writeBytes(String s);//将字符串按字节顺序写入到基本输出流中。

void writeChar(int v);//将一个char值以2-byte形式写入到基本输出流中。先写入高字节。

void writeInt(int v);//将一个int值以4-byte值形式写入到输出流中先写高字节。

void writeUTF(String str);//以机器无关的的方式用UTF-8修改版将一个字符串写到基本输出流。该方法先用writeShort写入两个字节表示后面的字节数。

int size();//返回written的当前值。

DataInputStream类

构造方法:DataInputStream(InputStream in);

主要方法:

int read(byte[] b);//从输入流中读取一定的字节,存放到缓冲数组b中。返回缓冲区中的总字节数。

int read(byte[] buf,int off,int len);//从输入流中一次读入len个字节存放在字节数组中的偏移off个字节及后面位置。

String readUTF();//读入一个已使用UTF-8修改版格式编码的字符串

String readLine();

boolean readBoolean;

int readInt();

byte readByte();

char readChar();

 

package PipedDemo;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class DataOutputStreamDemo {/** * @param args * @throws IOException  *  * DataOutputStream & DataInputStream 用于操作基本数据类型 *   * ByteArrayInputStream   &  ByteArrayOutputStream 用于操作字节数组。 *   * CharArrayReader  &   CharArrayWriter  用于操作字符数组 *   * StringReader  &  StringWriter  用于操作字符串。 *  */public static void main(String[] args) throws IOException {writeDemo();readDemo();}public static void readDemo() throws IOException {DataInputStream dos = new DataInputStream(new FileInputStream("data.txt"));String s = dos.readUTF();System.out.println(s);}public static void writeDemo() throws IOException {DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));dos.writeUTF("你好啊");//UTF-8修改版}}

 

热点排行