📜  Java中的Java类

📅  最后修改于: 2022-05-13 01:55:10.210000             🧑  作者: Mango

Java中的Java类

Java中的FilterInputStream类

过滤流过滤数据,因为它们在输入流中读取和写入数据,过滤它并将其传递给底层流。过滤流是

  • 过滤输入流
  • 过滤输出流

FilterInputStream : Java.io.FilterInputStream类的工作方式与Java中的 InputStream 类几乎相同,但它所做的只是覆盖 InputStream 类方法,将请求传递给 InputStream。 FilterInputStream 类的 read() 方法过滤数据并读取数据并将数据传递给底层流过滤,这取决于 Streams 完成。

宣言:

public class FilterInputStream
   extends InputStream

构造函数:

  • protected FilterInputStream(InputStream in):通过将参数in分配给字段this.in来创建一个FilterInputStream,以便记住它以供以后使用。

方法:

  • read(byte[] buffer) : Java.io.FilterInputStream.read(byte[] buffer)从过滤器输入流中读取 buffer.length 的字节数到缓冲区数组。
    句法 :
    public int read(byte[] buffer)
    Parameters :
    buffer : buffer to be read
    Return : 
    reads number of bytes of buffer.length to the buffer 
    else, -1 i.e. when end of file is reached.
    Exception :
    ->  IOException : If I/O error occurs.

    执行 :

    // Java program illustrating the working of read(byte[] buffer) method
      
    import java.io.*;
      
    public class NewClass
    {
        public static void main(String[] args) throws IOException
        {
            // LineNumberInputStream & FileInputStream initailly null
            FilterInputStream geek_input = null;
            InputStream geek = null;
      
            try{
                char c;
                int a;
                byte[] buffer = new byte[6];
      
                // New InputStream : 'GEEKS' is created
                geek = new FileInputStream("GEEKS.txt");
                geek_input = new BufferedInputStream(geek);
      
                a = geek.read(buffer);
                // read() method returning Bytes of Input Stream as integer
                // '-1' indicating to read till end Of Input Stream
                int length = 1 ;
                  
                for(byte g : buffer)
                {
                    // Since read() method returns Integer value
                    // So, we convert each integer value to char
                    c = (char)g;
      
                    System.out.println("At position " + length  +  " : "  + c);
                    length++;
                }
            }
            catch(Exception e)
            {
                // In case of error
                e.printStackTrace();
                System.out.println("ERROR Occurs ");
            }
            finally
            {
                // Closing the streams, Once the End of Input Stream is reached
                if(geek != null)
                    geek.close();
      
                if(geek_input != null)
                    geek_input.close();
            }
        }
    }
    

    笔记 :
    以下Java代码不会在此处运行,因为我们无法访问在线 IDE 上的任何文件。
    因此,将程序复制到您的系统并在那里运行它。

    程序中使用的GEEKS.txt文件包含:

    HelloGeeks

    在给定的代码中 buffer.length = 6 ,所以只有 HelloG 会被 read(byte[] buffer) 方法读取
    输出 :

    At position 1 : H
    At position 2 : e
    At position 3 : l
    At position 4 : l
    At position 5 : o
    At position 6 : G
  • read(byte[] buffer, int offset, int maxlen) : Java.io.FilterInputStream.read(byte[] buffer, int offset, int maxlen)从 FilterInputStream 读取最多 maxlen 个数据到缓冲区中。
    句法 :
    public int read(byte[] buffer, int offset, int maxlen)
    Parameters :
    buffer : Destination buffer
    offset : start position to read
    maxlen : max. length of bytes to be read
    Return : 
    total no. of bytes to be written else, -1 i.e. when end of Stream is reached.
    Exception :
    ->  IOException : If I/O error occurs.

    执行 :

    // Java program illustrating the working of
    // read(byte[] buffer, int offset, int maxlen) method
      
    import java.io.*;
      
    public class NewClass
    {
        public static void main(String[] args) throws IOException
        {
            // LineNumberInputStream & FileInputStream initailly null
            FilterInputStream geek_input = null;
            InputStream geek = null;
      
            try{
                char c;
                int a;
                byte[] buffer = new byte[4];
      
                // New InputStream : 'ABC' is created
                geek = new FileInputStream("ABC.txt");
                geek_input = new BufferedInputStream(geek);
      
                // Offset = 1(*), Maxlen = 3 (MOH)
                a = geek.read(buffer, 1, 3);
                // read() method returning Bytes of Input Stream as integer
                // '-1' indicating to read till end Of Input Stream
                 
                for(byte g : buffer)
                {
                    // Since read() method returns Integer value
                    // So, we convert each integer value to char
                    c = (char)g;
      
                    if(g == 0)
                        c = '*';
      
                    System.out.print(c);
                }
            }
            catch(Exception e)
            {
                // In case of error
                e.printStackTrace();
                System.out.println("ERROR Occurs ");
            }
            finally
            {
                // Closing the streams, Once the End of Input Stream is reached
                if(geek != null)
                    geek.close();
      
                if(geek_input != null)
                    geek_input.close();
            }
        }
    }
    

    笔记 :
    以下Java代码不会在此处运行,因为我们无法访问在线 IDE 上的任何文件。
    因此,将程序复制到您的系统并在那里运行它。

    程序中使用的 ABC.txt 文件包含:

    MOHIT

    偏移量 = 1 即 * 和 Maxlen = 3 即 MOH
    输出 :

    *MOH
  • available() : Java.io.FilterInputStream.available()返回编号。可以从输入流中读取的字节数。
    句法 :
    public int available()
    Parameters : 
    -------
    Return : 
    returns the no. of bytes that an be read from the FilterInputStream.
    Exception: 
    IOException : in case I/O error occurs

    执行 :

    // Java program illustrating the working of available() method
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args) throws IOException
        {
            // FilterInputStream & FileInputStream initailly null
            FilterInputStream geek_input = null;
      
            InputStream geek = null;
      
            try{
                char c;
                int a, b;
      
                // New InputStream : 'ABC' is created
                geek = new FileInputStream("ABC.txt");
                geek_input = new BufferedInputStream(geek);
      
                while((a = geek_input.read()) != -1)
                {
                    // So, we convert each integer value to char
                    c = (char)a;
      
                    // Use of available method : return no. of bytes that can be read
                    a = geek_input.available();
                    System.out.println(c + " Bytes available : " + a);
      
                }
            }
            catch(Exception e)
            {
                // In case of error
                e.printStackTrace();
                System.out.println("ERROR Occurs ");
            }
            finally
            {
                // Closing the streams, Once the End of FilterInputStream is reached
                if(geek != null)
                    geek.close();
      
                if(geek_input != null)
                    geek_input.close();
            }
        }
    }
    

    笔记 :
    以下Java代码不会在此处运行,因为我们无法访问在线 IDE 上的任何文件。
    因此,将程序复制到您的系统并在那里运行它。

    程序中使用的 ABC.txt 文件包含:

    MOHIT

    输出 :

    M Bytes available : 4
    O Bytes available : 3
    H Bytes available : 2
    I Bytes available : 1
    T Bytes available : 0
  • read() : Java.io.FilterInputStream.read()从过滤器输入流中读取下一个字节的数据。返回值字节在 0 到 255 的范围内。如果由于已到达流的末尾而没有可用的字节,则返回值 -1。
    句法 :
    public int read()
    Parameters :
    ------
    Return : 
    Reads next data else, -1 i.e. when end of Stream is reached.
    Exception :
    ->  IOException : If I/O error occurs.
  • close() : Java.io.FilterInputStream.close()关闭过滤器输入流并将与此流相关的系统资源释放到垃圾收集器。
    句法 :
    public void close()
    Parameters :
    ------
    Return : 
    void
    Exception :
    ->  IOException : If I/O error occurs.
  • mark(int arg) : Java.io.FilterInputStream.mark(int arg)标记FilterInputStream的当前位置。它设置readlimit,即在标记位置无效之前可以读取的最大字节数。
    句法 :
    public void mark(int arg)
    Parameters :
    arg : integer specifying the read limit of the input Stream
    Return : 
    void
  • skip() : Java.io.FilterInputStream.skip(long arg)从 FilterInputStream 数据中跳过并丢弃“arg”字节。
    句法 :
    public long skip(long arg)
    Parameters : 
    arg : no. of bytes of FilterInputStream data to skip.
    Return : 
    no. of bytes to be skipped
    Exception: 
    IOException : in case I/O error occurs
  • reset() : Java.io.FilterInputStream.reset()由 mark() 方法调用。它将 FilterInputStream 重新定位到标记的位置。
    句法 :
    public void reset()
    Parameters :
    ----
    Return : 
    void
    Exception :
    ->  IOException : If I/O error occurs.
  • markSupported() : Java.io.FilterInputStream.markSupported()方法测试此输入流是否支持标记和重置方法。 InputStream 的 markSupported 方法默认返回 false。
    句法 :
    public boolean markSupported()
    Parameters :
    -------
    Return : 
    true if input stream supports the mark() and reset() method  else,false

    Java程序解释:markSupported()、close()、reset()、mark()、read()、skip() 方法

    // Java program illustrating the working of FilterInputStream method
    // mark(), read(), skip()
    // markSupported(), close(), reset()
      
    import java.io.*;
      
    public class NewClass
    {
        public static void main(String[] args) throws Exception
        {
            InputStream geek = null;
            // FilterInputStream initialised to null here
            FilterInputStream geek_input = null;
            try {
      
                geek = new FileInputStream("GEEKS.txt");
                  
            geek_input = new BufferedInputStream(geek);
      
                // read() method : reading and printing Characters
                // one by one
                System.out.println("Char : " + (char)geek_input.read());
                System.out.println("Char : " + (char)geek_input.read());
                System.out.println("Char : " + (char)geek_input.read());
      
                // mark() : read limiing the 'geek' input stream
                geek_input.mark(0);
      
                // skip() : it results in redaing of 'e' in G'e'eeks
                geek_input.skip(1);
                System.out.println("skip() method comes to play");
                System.out.println("mark() method comes to play");
                System.out.println("Char : " + (char)geek_input.read());
                System.out.println("Char : " + (char)geek_input.read());
                System.out.println("Char : " + (char)geek_input.read());
      
                boolean check = geek_input.markSupported();
                if (geek_input.markSupported())
                {
                    // reset() method : repositioning the stream to
                    // marked positions.
                    geek_input.reset();
                    System.out.println("reset() invoked");
                    System.out.println("Char : " + (char)geek_input.read());
                    System.out.println("Char : " + (char)geek_input.read());
                }
                else
                    System.out.println("reset() method not supported.");
                    System.out.println("geek_input.markSupported() supported" 
                                                    + " reset() : " + check);
      
            }
            catch(Exception excpt)
            {
                // in case of I/O error
                excpt.printStackTrace();
            }
            finally
            {
                // releasing the resources back to the
                // GarbageCollector when closes
                if (geek != null)
                { // Use of close() : closing the file
                    // and releasing resources
                    geek.close();
                }
      
                if(geek_input != null)
                    geek_input.close();
            }
        }
    }
    

    笔记 :
    此代码不会在在线 IDE 上运行,因为此处不存在此类文件。
    您可以在您的系统上运行此代码以检查工作情况。
    代码中使用的GEEKS.txt文件有

    HelloGeeks

    输出 :

    Char : H
    Char : e
    Char : l
    skip() method comes to play
    mark() method comes to play
    Char : o
    Char : G
    Char : e
    reset() invoked
    Char : l
    Char : o
    geek_input.markSupported() supported reset() : true