當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.io.FilterOutputStream用法及代碼示例


Java 中的 java.io.FilterInputStream 類

FilterInputStream and FilterOutputStream Class

Java.io.FilterOutputStream 類是所有過濾輸出流的類的超類。 FilterOutputStream 類的 write() 方法過濾數據並將其寫入底層流,過濾的完成取決於流。

宣言:

public class FilterOutputStream
   extends OutputStream

構造函數:

  • FilterOutputStream(輸出流極客):創建輸出流過濾器。

方法:

  • 寫(int arg):java.io.FilterOutputStream.write(int arg)將指定字節寫入輸出流。
    句法:
public void write(int arg)
Parameters : 
arg : Source Bytes
Return  :
void
Exception : 
In case any I/O error occurs.
  • Implementation :

Java


// Java program illustrating the working of work(int arg)
// method
import java.io.*;
import java.lang.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        // OutputStream, FileInputStream & FilterOutputStream
        // initially null
        OutputStream geek_out = null;
        FilterOutputStream geek_filter = null;
        // FileInputStream used here
        FileInputStream geekinput = null;
        char c;
        int a;
        try
        {
            // create output streams
            geek_out = new FileOutputStream("GEEKS.txt");
            geek_filter = new FilterOutputStream(geek_out);
            // write(int arg) : Used to write 'M' in the file
            // - "ABC.txt"
            geek_filter.write(77);
            // Flushes the Output Stream
            geek_filter.flush();
            // Creating Input Stream
            geekinput = new FileInputStream("GEEKS.txt");
            // read() method of FileInputStream :
            // reading the bytes and converting next bytes to int
            a = geekinput.read();
            /* Since, read() converts bytes to int, so we
               convert int to char for our program output*/
            c = (char)a;
            // print character
            System.out.println("Character written by" +
                              " FilterOutputStream : " + c);
        }
        catch(IOException except)
        {
            // if any I/O error occurs
            System.out.print("Write Not working properly");
        }
        finally{
            // releases any system resources associated with
            // the stream
            if (geek_out != null)
                geek_out.close();
            if (geek_filter != null)
                geek_filter.close();
        }
    }
}
  • 筆記:
    在我使用過的程序中GEEKS.txt文件時,程序將創建一個具有代碼中給定名稱的新文件並寫入其中。
    輸出:
Character written by FilterOutputStream : M
  • write(byte[] buffer): java.io.FilterOutputStream.write(byte[] buffer)‘arg.length’字節到輸出流。
    句法:
public void write(byte[] arg)
Parameters : 
buffer : Source Buffer to be written to the Output Stream
Return  :
void
Exception : 
In case any I/O error occurs.
  • Implementation :

Java


// Java program illustrating the working of work(byte
// buffer) method
import java.io.*;
import java.lang.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        // OutputStream, FileInputStream & FilterOutputStream
        // initially null
        OutputStream geek_out = null;
        FilterOutputStream geek_filter = null;
        // FileInputStream used here
        FileInputStream geekinput = null;
        byte[] buffer = {77, 79, 72, 73, 84};
        char c;
        int a;
        try
        {
         // create output streams
         geek_out = new FileOutputStream("ABC.txt");
         geek_filter = new FilterOutputStream(geek_out);
         // writes buffer to the output stream
         geek_filter.write(buffer);
         // forces byte contents to written out to the stream
         geek_filter.flush();
         // create input streams
         geekinput = new FileInputStream("ABC.txt");
         while ((a=geekinput.read())!=-1)
         {
            // converts integer to the character
            c = (char)a;
            // prints
            System.out.print(c);
         }
        }
        catch(IOException except)
        {
            // if any I/O error occurs
            System.out.print("Write Not working properly");
        }
        finally
        {
            // releases any system resources associated
            // with the stream
            if (geek_out != null)
                geek_out.close();
            if (geek_filter != null)
                geek_filter.close();
        }
    }
}
  • 筆記:
    在我使用的程序中GEEKS.txt文件時,程序將創建一個具有代碼中給定名稱的新文件並寫入其中。

輸出:

MOHIT
  • write(byte[] buffer, int offset, int maxlen): java.io.FilterOutputStream.write(byte[] buffer, int offset, int maxlen)將 maxlen 個字節從指定的 Buffer(從偏移位置開始)寫入到輸出流。

句法:

public void write(write(byte[] buffer, int offset, int maxlen)
Parameters : 
buffer : Source Buffer to be written to the Output Stream
Return  :
buffer : Source Buffer to be written
offset : Starting offset 
maxlen : max no. of bytes to be written to the Output Stream
Exception : 
In case any I/O error occurs.
  • flush(): java.io.FilterOutputStream.flush()刷新輸出流,並且不允許將任何數據寫入該流。
    句法:
public void flush()
Parameters : 
------
Return  :
void
Exception : 
In case any I/O error occurs.
  • close(): java.io.FilterOutputStream.close()關閉流並釋放所有分配給流的資源。
    句法:
public void close()
Parameters : 
------
Return  :
void
Exception : 
In case any I/O error occurs.

Java程序說明:write(byte[] buffer, int offset, int maxlen), flush(), close()方法

Java


// Java program illustrating the working of
// write(byte[] buffer, int offset, int maxlen),
// flush(), close() method
import java.io.*;
import java.lang.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        // OutputStream, FileInputStream & FilterOutputStream
        // initially null
        OutputStream geek_out = null;
        FilterOutputStream geek_filter = null;
        // FileInputStream used here
        FileInputStream geekinput = null;
        byte[] buffer = {65, 66, 77, 79, 72, 73, 84};
        char c;
        int a;
        try
        {
            // create output streams
            geek_out = new FileOutputStream("ABC.txt");
            geek_filter = new FilterOutputStream(geek_out);
            // write(byte[] buffer, int offset, int maxlen) :
            // writes buffer to the output stream
            // Here offset = 2, so it won't read first two bytes
            // then maxlen = 5, so it will print max of 5 characters
            geek_filter.write(buffer, 2, 5);
            // forces byte contents to written out to the stream
            geek_filter.flush();
            // create input streams
            geekinput = new FileInputStream("ABC.txt");
            while ((a = geekinput.read())!=-1)
            {
                // converts integer to the character
                c = (char)a;
                // prints
                System.out.print(c);
            }
        }
        catch(IOException except)
        {
            // if any I/O error occurs
            System.out.print("Write Not working properly");
        }
        finally
        {
            // releases any system resources associated
            // with the stream
            if (geek_out != null)
                geek_out.close();
            if (geek_filter != null)
                geek_filter.close();
        }
    }
}

筆記:
在我使用的程序中GEEKS.txt文件時,程序將創建一個具有代碼中給定名稱的新文件並寫入其中。

輸出:

MOHIT


相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.FilterOutputStream Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。