JAVA伊奥。Java中的DataOutputStream

数据输出流允许应用程序以可移植的方式将原始Java数据类型写入输出流。然后,应用程序可以使用数据输入流将数据读回。在讨论这个类的方法之前,让我们先讨论一下这个类的构造函数。

null

建造师: DataOutputStream(OutputStream out)

创建新的数据输出流,将数据写入指定的底层输出流。现在,让我们在实现 相同的

方法1: 无效刷新()

刷新此数据输出流。

语法:

public void flush() throws IOException

注: 它确实重写了类FilterOutputStream中的flush()方法

引发异常: IOException

方法2: 大小()

返回写入的计数器的当前值,即到目前为止写入此数据输出流的字节数

语法:

public final int size()

返回类型: 写入字段的整数值

方法3: 写()

从offset off开始,将指定字节数组中的len字节写入基础输出流。

语法:

void write (byte[] b, int off, int len)

参数:

  • 字节数组
  • int off
  • 字节数组的长度

返回类型: 无效的

方法4:

语法 :

public void write(byte[] b, int off,int len)

注: 它重写FilterOutputStream类中的write()方法。

参数:

  • 数据
  • 数据中的起始偏移量
  • 要写入的字节数

例外情况: IOException

方法5:无效写入(int b):

将指定字节(参数b的低位8位)写入底层输出流。

语法:

public void write(int b)throws IOException

返回类型: 无效的

参数: 要写入的字节

方法6: writebolean()

将布尔值作为1字节值写入基础输出流。

语法:

void writeBoolean (boolean v) 

返回类型: 无效的

参数: 布尔值

方法7: writebolean()

语法:

public final void writeBoolean(boolean v)throws IOException 

参数: 要写入的布尔值。

抛出: IOException

方法8: writeByte()

将一个字节作为1字节的值写入基础输出流。

语法:

public final void writeByte(int v)throws IOException 

参数: 要写入的字节值

引发异常: IOException

方法9: writeChar()

将字符作为2字节的值写入底层输出流,高字节优先。

语法:

public final void writeChar(int v)throws IOException

参数: 要写入的字符值。

例外 :IOException

方法10: 无效可写双(双v)

使用double类中的doubleToLongBits方法将double参数转换为long,然后将该long值作为8字节的量(先高字节)写入底层输出流。

语法:

public final void writeDouble(double v)throws IOException

参数: 要写入的双值

引发异常: IOException

方法11: 写浮动(浮动v)

使用float类中的floatToIntBits方法将float参数转换为int,然后将该int值作为4字节量(MSB first)写入基础输出流。

语法:

public final void writeFloat(float v)throws IOException

返回类型: 无效的

参数: 要写入的浮点值。

引发异常: IOException

方法12: writeInt()

将int以四个字节的形式写入底层输出流,高字节优先。

语法:

public final void writeInt(int v)throws IOException

参数: 一个要写的int。

返回类型: 无效的

引发异常: 将int以四个字节的形式写入底层输出流,高字节优先。

方法13: writeLong()

以8字节的形式向底层输出流写入长字节,先写入高字节。

语法:

public final void writeLong(long v)throws IOException

参数: 好久没写了。

抛出: IOException

方法14: writeShort():

将短消息以两个字节的形式写入底层输出流,先写入高字节。

返回类型: 无效的

参数: 要写的短文

语法:

public final void writeShort(int v)throws IOException

方法15: writeUTF(字符串str)

以独立于机器的方式使用修改的UTF-8编码将字符串写入底层输出流。

参数 :要写入的字符串

返回类型: 无效的

引发异常: IOException

注: 下面列出了一些需要记住的要点:

  • DataOutputStream和DataInputStream经常一起使用。
  • 当DataOutputStream关闭时(通过调用close()),out指定的底层流也会自动关闭。
  • 不再有任何显式close()方法调用。try-with-resources结构解决了这个问题。

例子:

JAVA

// Java Program to Demonstrate DataOutputStream
// Importing required classes
import java.io.*;
// Main class
// DataOutputStreamDemo
class GFG {
// Main driver method
public static void main(String args[]) throws IOException {
// Try block to check for exceptions
// Writing the data using DataOutputStream
try ( DataOutputStream dout =
new DataOutputStream( new FileOutputStream( "file.dat" )) ) {
dout.writeDouble( 1.1 );
dout.writeInt( 55 );
dout.writeBoolean( true );
dout.writeChar( '4' );
}
catch (FileNotFoundException ex) {
System.out.println( "Cannot Open the Output File" );
return ;
}
// Reading the data back using DataInputStream
try ( DataInputStream din =
new DataInputStream( new FileInputStream( "file.dat" )) ) {
double a = din.readDouble();
int b = din.readInt();
boolean c = din.readBoolean();
char d = din.readChar();
System.out.println( "Values: " + a + " " + b + " " + c + " " + d);
}
// Catch block to handle FileNotFoundException
catch (FileNotFoundException e) {
System.out.println( "Cannot Open the Input File" );
return ;
}
}
}


输出:

Values: 1.1 55 true 4

注: 上面的程序使用try-with资源,所以它需要JDK 7或更高版本。

本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享