logo

FileOutputStream を使用したファイルの作成

FileOutputStream クラスはバイト ストリームに属し、データを個々のバイトの形式で格納します。テキストファイルの作成に使用できます。ファイルは、ハードディスクや CD などの 2 番目の記憶メディア上のデータの記憶を表します。ファイルが利用可能かどうか、またはファイルを作成できるかどうかは、基盤となるプラットフォームによって異なります。特に一部のプラットフォームでは、一度に 1 つの FileOutputStream (または他のファイル書き込みオブジェクト) のみによる書き込み用にファイルを開くことができます。このような状況で、関連するファイルがすでに開いていると、このクラスのコンストラクターは失敗します。 FileOutputStream は、画像データなどの生のバイトのストリームを書き込むためのものです。文字のストリームを書き込む場合は、FileWriter の使用を検討してください。 重要な方法:
    void close() : このファイル出力ストリームを閉じ、このストリームに関連付けられているシステム リソースを解放します。 保護された void Finalize() : ファイルへの接続をクリーンアップし、このストリームへの参照がなくなったときに、このファイル出力ストリームの close メソッドが確実に呼び出されるようにします。 void write(byte[] b) : 指定されたバイト配列から b.length バイトをこのファイル出力ストリームに書き込みます。 void write(byte[] b int off int len) : オフセット off から始まる指定されたバイト配列から len バイトをこのファイル出力ストリームに書き込みます。 void write(int b) : 指定されたバイトをこのファイル出力ストリームに書き込みます。
いくつかの文字 (またはテキスト) を保存するテキスト ファイルを作成するには、次の手順に従います。
    データの読み取り: First of all data should be read from the keyboard. For this purpose associate the keyboard to some input stream class. The code for using DataInputSream class for reading data from the keyboard is as:
    DataInputStream dis =new DataInputStream(System.in);
    Here System.in represent the keyboard which is linked with DataInputStream object データを OutputStream に送信します。 Now associate a file where the data is to be stored to some output stream. For this take the help of FileOutputStream which can send data to the file. Attaching the file.txt to FileOutputStream can be done as:
    FileOutputStream fout=new FileOutputStream(file.txt);
    DataInputStream からデータを読み取る: The next step is to read data from DataInputStream and write it into FileOutputStream . It means read data from dis object and write it into fout object as shown here:
    ch=(char)dis.read(); fout.write(ch);
    ファイルを閉じます。最後に、ファイルに対して入力または出力操作を実行した後は、ファイルを閉じる必要があります。そうしないと、ファイルのデータが破損する可能性があります。ファイルを閉じるには、関連するストリームを閉じます。たとえば、 fout.close(): は FileOutputStream を閉じるため、ファイルにデータを書き込む方法はありません。
実装: Java
//Java program to demonstrate creating a text file using FileOutputStream import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.IOException; class Create_File {  public static void main(String[] args) throws IOException   {  //attach keyboard to DataInputStream  DataInputStream dis=new DataInputStream(System.in);  // attach file to FileOutputStream  FileOutputStream fout=new FileOutputStream('file.txt');  //attach FileOutputStream to BufferedOutputStream  BufferedOutputStream bout=new BufferedOutputStream(fout1024);  System.out.println('Enter text (@ at the end):');  char ch;  //read characters from dis into ch. Then write them into bout.  //repeat this as long as the read character is not @  while((ch=(char)dis.read())!='@')  {  bout.write(ch);  }  //close the file  bout.close();  } } 
If the Program is executed again the old data of file.txt will be lost and any recent data is only stored in the file. If we don’t want to lose the previous data of the file and just append the new data to the end of already existing data and this can be done by writing true along with file name.
FileOutputStream fout=new FileOutputStream(file.txttrue); 

BufferedOutputStream を使用した効率の向上

Normally whenever we write data to a file using FileOutputStream as:
fout.write(ch);
Here the FileOutputStream is invoked to write the characters into the file. Let us estimate the time it takes to read 100 characters from the keyboard and write all of them into a file.
  • DataInputStream を使用してデータがキーボードからメモリに読み取られ、1 文字をメモリに読み取るのに 1 秒かかり、この文字が FileOutputStream によってさらに 1 秒かけてファイルに書き込まれると仮定します。
  • したがって、ファイルの読み取りと書き込みには 200 秒かかります。これは多くの時間を無駄にしています。一方、Buffered クラスが使用される場合は、最初にバッファーからの文字で満たされるバッファーが提供され、一度にファイルに書き込むことができます。バッファリングされたクラスは、他のストリーム クラスと接続して使用する必要があります。
  • First the DataInputStream reads data from the keyboard by spending 1 sec for each character. This character is written into the buffer. Thus to read 100 characters into a buffer it will take 100 second time. Now FileOutputStream will write the entire buffer in a single step. So reading and writing 100 characters took 101 sec only. In the same way reading classes are used for improving the speed of reading operation.  Attaching FileOutputStream to BufferedOutputStream as:
    BufferedOutputStream bout=new BufferedOutputStream(fout1024);
    Here the buffer size is declared as 1024 bytes. If the buffer size is not specified then a default size of 512 bytes is used
BufferedOutputStream クラスの重要なメソッド:
    ボイドフラッシュ() : このバッファリングされた出力ストリームをフラッシュします。 void write(byte[] b int off int len) : 指定されたバイト配列のオフセット off から始まる len バイトを、このバッファリングされた出力ストリームに書き込みます。 void write(int b) : 指定されたバイトをこのバッファリングされた出力ストリームに書き込みます。
出力:
C:> javac Create_File.java C:> java Create_File Enter text (@ at the end): This is a program to create a file @ C:/> type file.txt This is a program to create a file 
関連記事:
  • キャラクターストリームとバイトストリーム
  • Javaのファイルクラス
  • FileWriter と FileReader を使用した Java でのファイル処理
クイズの作成