📜  Java I/O-DataOutputStream

📅  最后修改于: 2020-09-27 02:34:58             🧑  作者: Mango

Java DataOutputStream类

Java DataOutputStream类允许应用程序以与机器无关的方式将原始Java数据类型写入输出流。

Java应用程序通常使用数据输出流来写入数据,以后可以由数据输入流读取。

Java DataOutputStream类声明

让我们看一下java.io.DataOutputStream类的声明:

public class DataOutputStream extends FilterOutputStream implements DataOutput

Java DataOutputStream类方法

Method Description
int size() It is used to return the number of bytes written to the data output stream.
void write(int b) It is used to write the specified byte to the underlying output stream.
void write(byte[] b, int off, int len) It is used to write len bytes of data to the output stream.
void writeBoolean(boolean v) It is used to write Boolean to the output stream as a 1-byte value.
void writeChar(int v) It is used to write char to the output stream as a 2-byte value.
void writeChars(String s) It is used to write string to the output stream as a sequence of characters.
void writeByte(int v) It is used to write a byte to the output stream as a 1-byte value.
void writeBytes(String s) It is used to write string to the output stream as a sequence of bytes.
void writeInt(int v) It is used to write an int to the output stream
void writeShort(int v) It is used to write a short to the output stream.
void writeShort(int v) It is used to write a short to the output stream.
void writeLong(long v) It is used to write a long to the output stream.
void writeUTF(String str) It is used to write a string to the output stream using UTF-8 encoding in portable manner.
void flush() It is used to flushes the data output stream.

DataOutputStream类的示例

在此示例中,我们使用DataOutputStream类将数据写入文本文件testout.txt。

package com.javatpoint;

import java.io.*;
public class OutputExample {
public static void main(String[] args) throws IOException {
FileOutputStream file = new FileOutputStream(D:\\testout.txt);
DataOutputStream data = new DataOutputStream(file);
data.writeInt(65);
data.flush();
data.close();
System.out.println("Succcess...");
}
}

输出:

Succcess...

testout.txt:

A