DataOutputStream
| 日本語 | 情報出力流れ |
| 英語 | data output stream |
| ふりがな | でーたあうとぷっとすとりーむ |
| フリガナ | データアウトプットストリーム |
J2SEに含まれるクラスのひとつ。パッケージも含めたクラス名はjava.io.DataOutputStream。
ストリームクラス。OutputStreamクラスのサブクラスであり、バイト出力ストリームクラスである。
FilterOutputStreamクラスのサブクラスに当たり、バイト出力ストリームクラスの中では「出力用」に位置する。
対になる入力側のクラスはDataInputStreamクラスである。
ストリーム中のバイトデータに「プリミティブ型」や「文字列」を出力するためのバイト出力ストリームクラス。
通常、バイト出力ストリームは対象を「バイトの並び」と見なしてバイトデータを出力する。
DataOutputStreamクラスは、その「バイトの並び」に「プリミティブ型」や「文字列型」をバイナリー形式で書き込む。
DataOutputStreamクラスで出力したバイトストリームからは、必ずDataInputStreamクラスで取得する必要がある。
つまり、DataInputStreamクラスとDataOutputStreamクラスは対にして使用するものであり、DataOutputStreamクラスは、たとえばテキストファイルといった形式では出力できず、DataInputStreamクラスでしか取得できない「バイナリーデータ」としてのみ出力する。
DataOutputStreamクラスは他のバイト出力ストリームクラスに比べて「プリミティブ型や文字列を直接出力できる」というメリットがありそうに見えるが、そのためには入力時にDataInputStreamクラスを使用しなければいけない。
そのため、アプリケーションの独自フォーマットで保存する場合にのみメリットがある、ということである。
DataOutputStreamクラスのコンストラクタに他のOutputStreamクラスのサブクラスを渡すことで、そのクラスを「出力先」として取得する。
DataOutputStreamクラスのwriteInt()メソッド、writeDouble()メソッド、writeUTF()メソッド等によって、プリミティブ型や文字列を直接出力できる。ただし、必ずDataInputStreamクラスのreadInt()メソッド、readDouble()メソッド、readUTF()メソッド等で取得する必要がある。その際、型と順番を合わせなければ、適切なデータを取得することはできない。
処理終了後はclose()メソッドを呼び終了処理を行う。
ストリームクラス。OutputStreamクラスのサブクラスであり、バイト出力ストリームクラスである。
FilterOutputStreamクラスのサブクラスに当たり、バイト出力ストリームクラスの中では「出力用」に位置する。
対になる入力側のクラスはDataInputStreamクラスである。
ストリーム中のバイトデータに「プリミティブ型」や「文字列」を出力するためのバイト出力ストリームクラス。
通常、バイト出力ストリームは対象を「バイトの並び」と見なしてバイトデータを出力する。
DataOutputStreamクラスは、その「バイトの並び」に「プリミティブ型」や「文字列型」をバイナリー形式で書き込む。
DataOutputStreamクラスで出力したバイトストリームからは、必ずDataInputStreamクラスで取得する必要がある。
つまり、DataInputStreamクラスとDataOutputStreamクラスは対にして使用するものであり、DataOutputStreamクラスは、たとえばテキストファイルといった形式では出力できず、DataInputStreamクラスでしか取得できない「バイナリーデータ」としてのみ出力する。
DataOutputStreamクラスは他のバイト出力ストリームクラスに比べて「プリミティブ型や文字列を直接出力できる」というメリットがありそうに見えるが、そのためには入力時にDataInputStreamクラスを使用しなければいけない。
そのため、アプリケーションの独自フォーマットで保存する場合にのみメリットがある、ということである。
DataOutputStreamクラスのコンストラクタに他のOutputStreamクラスのサブクラスを渡すことで、そのクラスを「出力先」として取得する。
DataOutputStreamクラスのwriteInt()メソッド、writeDouble()メソッド、writeUTF()メソッド等によって、プリミティブ型や文字列を直接出力できる。ただし、必ずDataInputStreamクラスのreadInt()メソッド、readDouble()メソッド、readUTF()メソッド等で取得する必要がある。その際、型と順番を合わせなければ、適切なデータを取得することはできない。
処理終了後はclose()メソッドを呼び終了処理を行う。
// Sample.java
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.EOFException;
public class Sample
{
public static void main( String[] args )
{
ByteArrayOutputStream byteArrayOutputStream = null;
DataOutputStream dataOutputStream = null;
ByteArrayInputStream byteArrayInputStream = null;
DataInputStream dataInputStream = null;
try
{
// まずは出力します。
// 出力先のByteArrayOutputStreamクラスを作ります。
byteArrayOutputStream = new ByteArrayOutputStream();
// これを出力先とするDataOutputStreamクラスを作ります。
dataOutputStream = new DataOutputStream( byteArrayOutputStream );
// ストリームを通して、データを書き込みます。
dataOutputStream.writeInt( 100 );
dataOutputStream.writeUTF( "あいうえお" );
dataOutputStream.writeInt( 200);
// 書き込まれたバイトデータをbyte型配列として取得します。
byte[] bytes = byteArrayOutputStream.toByteArray();
// 参考用に、byte型配列の中身を出力します。
for( int iF1 = 0; iF1 < bytes.length; ++iF1 )
{
System.out.print( Integer.toHexString( 0x000000FF & (int)bytes[iF1] ) + ", " );
}
System.out.println();
// 0, 0, 0, 64, 0, f, e3, 81, 82, e3, 81, 84, e3, 81, 86, e3, 81, 88, e3, 81, 8a, 0, 0, 0, c8,
// ← 100 → ↑ ← あいうえお(UTF-8) → ← 200 →
// ↑「あいうえお」のバイト数
// それを読み込みます。
// 入力元のByteArrayInputStreamクラスを作ります。
byteArrayInputStream = new ByteArrayInputStream( bytes );
// これを入力元とするDataOutputStreamクラスを作ります。
dataInputStream = new DataInputStream( byteArrayInputStream );
try
{
// データを読み込みます。
int i1 = dataInputStream.readInt();
String string = dataInputStream.readUTF();
int i2 = dataInputStream.readInt();
System.out.println( i1 );
System.out.println( string );
System.out.println( i2 );
// 100
// あいうえお
// 200
// 最後まで来てるのに読み込もうとすると、
// EOFException例外が投げられます。
char ch = dataInputStream.readChar();
}
catch( EOFException e )
{
System.out.println( "ストリームの終端まで来ました。" );
// このように、ストリームの最後に来ると
// EOFExceptionが投げられます。
// 他のストリームの場合、戻り値に「ストリームの終端まで来た」
// ことを示す「-1」が返されるのですが、DataInputStreamクラスの
// 場合、プリミティブ型を返すのでそれができません。
// そのため、代わりにEOFException例外を投げるのです。
}
}
catch( IOException e )
{
// 入出力に失敗した場合等に、このIOException例外が投げられます。
e.printStackTrace();
}
finally
{
// ストリームを扱ったら、最後にclose()メソッドを呼んで
// 後処理をします。また、これは必ず行うため、
// finally内で行います。
try
{
if( dataInputStream != null )
{
dataInputStream.close();
}
}
catch( IOException e )
{
// close()メソッドはIOExceptionがthrows指定されているので
// 一応受け取ります。
e.printStackTrace();
}
try
{
if( byteArrayInputStream != null )
{
byteArrayInputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
try
{
if( dataOutputStream != null )
{
dataOutputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
try
{
if( byteArrayOutputStream != null )
{
byteArrayOutputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
}
}
}
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.EOFException;
public class Sample
{
public static void main( String[] args )
{
ByteArrayOutputStream byteArrayOutputStream = null;
DataOutputStream dataOutputStream = null;
ByteArrayInputStream byteArrayInputStream = null;
DataInputStream dataInputStream = null;
try
{
// まずは出力します。
// 出力先のByteArrayOutputStreamクラスを作ります。
byteArrayOutputStream = new ByteArrayOutputStream();
// これを出力先とするDataOutputStreamクラスを作ります。
dataOutputStream = new DataOutputStream( byteArrayOutputStream );
// ストリームを通して、データを書き込みます。
dataOutputStream.writeInt( 100 );
dataOutputStream.writeUTF( "あいうえお" );
dataOutputStream.writeInt( 200);
// 書き込まれたバイトデータをbyte型配列として取得します。
byte[] bytes = byteArrayOutputStream.toByteArray();
// 参考用に、byte型配列の中身を出力します。
for( int iF1 = 0; iF1 < bytes.length; ++iF1 )
{
System.out.print( Integer.toHexString( 0x000000FF & (int)bytes[iF1] ) + ", " );
}
System.out.println();
// 0, 0, 0, 64, 0, f, e3, 81, 82, e3, 81, 84, e3, 81, 86, e3, 81, 88, e3, 81, 8a, 0, 0, 0, c8,
// ← 100 → ↑ ← あいうえお(UTF-8) → ← 200 →
// ↑「あいうえお」のバイト数
// それを読み込みます。
// 入力元のByteArrayInputStreamクラスを作ります。
byteArrayInputStream = new ByteArrayInputStream( bytes );
// これを入力元とするDataOutputStreamクラスを作ります。
dataInputStream = new DataInputStream( byteArrayInputStream );
try
{
// データを読み込みます。
int i1 = dataInputStream.readInt();
String string = dataInputStream.readUTF();
int i2 = dataInputStream.readInt();
System.out.println( i1 );
System.out.println( string );
System.out.println( i2 );
// 100
// あいうえお
// 200
// 最後まで来てるのに読み込もうとすると、
// EOFException例外が投げられます。
char ch = dataInputStream.readChar();
}
catch( EOFException e )
{
System.out.println( "ストリームの終端まで来ました。" );
// このように、ストリームの最後に来ると
// EOFExceptionが投げられます。
// 他のストリームの場合、戻り値に「ストリームの終端まで来た」
// ことを示す「-1」が返されるのですが、DataInputStreamクラスの
// 場合、プリミティブ型を返すのでそれができません。
// そのため、代わりにEOFException例外を投げるのです。
}
}
catch( IOException e )
{
// 入出力に失敗した場合等に、このIOException例外が投げられます。
e.printStackTrace();
}
finally
{
// ストリームを扱ったら、最後にclose()メソッドを呼んで
// 後処理をします。また、これは必ず行うため、
// finally内で行います。
try
{
if( dataInputStream != null )
{
dataInputStream.close();
}
}
catch( IOException e )
{
// close()メソッドはIOExceptionがthrows指定されているので
// 一応受け取ります。
e.printStackTrace();
}
try
{
if( byteArrayInputStream != null )
{
byteArrayInputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
try
{
if( dataOutputStream != null )
{
dataOutputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
try
{
if( byteArrayOutputStream != null )
{
byteArrayOutputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
}
}
}
// Sample.java
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.EOFException;
public class Sample
{
public static void main( String[] args )
{
ByteArrayOutputStream byteArrayOutputStream = null;
DataOutputStream dataOutputStream = null;
ByteArrayInputStream byteArrayInputStream = null;
DataInputStream dataInputStream = null;
try
{
// まずは出力します。
// 出力先のByteArrayOutputStreamクラスを作ります。
byteArrayOutputStream = new ByteArrayOutputStream();
// これを出力先とするDataOutputStreamクラスを作ります。
dataOutputStream = new DataOutputStream( byteArrayOutputStream );
// ストリームを通して、データを書き込みます。
dataOutputStream.writeInt( 100 );
dataOutputStream.writeUTF( "あいうえお" );
dataOutputStream.writeInt( 200);
// 書き込まれたバイトデータをbyte型配列として取得します。
byte[] bytes = byteArrayOutputStream.toByteArray();
// 参考用に、byte型配列の中身を出力します。
for( int iF1 = 0; iF1 < bytes.length; ++iF1 )
{
System.out.print( Integer.toHexString( 0x000000FF & (int)bytes[iF1] ) + ", " );
}
System.out.println();
// 0, 0, 0, 64, 0, f, e3, 81, 82, e3, 81, 84, e3, 81, 86, e3, 81, 88, e3, 81, 8a, 0, 0, 0, c8,
// ← 100 → ↑ ← あいうえお(UTF-8) → ← 200 →
// ↑「あいうえお」のバイト数
// それを読み込みます。
// 入力元のByteArrayInputStreamクラスを作ります。
byteArrayInputStream = new ByteArrayInputStream( bytes );
// これを入力元とするDataOutputStreamクラスを作ります。
dataInputStream = new DataInputStream( byteArrayInputStream );
try
{
// データを読み込みます。
int i1 = dataInputStream.readInt();
String string = dataInputStream.readUTF();
int i2 = dataInputStream.readInt();
System.out.println( i1 );
System.out.println( string );
System.out.println( i2 );
// 100
// あいうえお
// 200
// 最後まで来てるのに読み込もうとすると、
// EOFException例外が投げられます。
char ch = dataInputStream.readChar();
}
catch( EOFException e )
{
System.out.println( "ストリームの終端まで来ました。" );
// このように、ストリームの最後に来ると
// EOFExceptionが投げられます。
// 他のストリームの場合、戻り値に「ストリームの終端まで来た」
// ことを示す「-1」が返されるのですが、DataInputStreamクラスの
// 場合、プリミティブ型を返すのでそれができません。
// そのため、代わりにEOFException例外を投げるのです。
}
}
catch( IOException e )
{
// 入出力に失敗した場合等に、このIOException例外が投げられます。
e.printStackTrace();
}
finally
{
// ストリームを扱ったら、最後にclose()メソッドを呼んで
// 後処理をします。また、これは必ず行うため、
// finally内で行います。
try
{
if( dataInputStream != null )
{
dataInputStream.close();
}
}
catch( IOException e )
{
// close()メソッドはIOExceptionがthrows指定されているので
// 一応受け取ります。
e.printStackTrace();
}
try
{
if( byteArrayInputStream != null )
{
byteArrayInputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
try
{
if( dataOutputStream != null )
{
dataOutputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
try
{
if( byteArrayOutputStream != null )
{
byteArrayOutputStream.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
}
}
}




