JAVA伊奥。Java中的FilterInputStream类
null
JAVA伊奥。过滤器输出流 类是过滤输出流的所有类的超类。FilterOutputStream类的write()方法过滤数据并将其写入底层流,根据流进行过滤。
宣言:
public class FilterOutputStream extends OutputStream
施工人员:
- FilterOutputStream(OutputStream geekout): 创建一个输出流过滤器。
方法:
- write(int-arg):java。伊奥。过滤器输出流。写入(int arg) 将指定字节写入输出流。 语法:
public void write(int arg)Parameters : arg : Source BytesReturn :voidException : In case any I/O error occurs.
- 实施:
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(); } } } |
- 注: 在我使用的程序中 极客。txt 文件,程序将创建代码中给定名称的新文件并写入其中。 输出:
Character written by FilterOutputStream : M
- 写入(字节[]缓冲区):java。伊奥。过滤器输出流。写入(字节[]缓冲区) 写 “啊。长度’ 字节到输出流。 语法:
public void write(byte[] arg)Parameters : buffer : Source Buffer to be written to the Output StreamReturn :voidException : In case any I/O error occurs.
- 实施:
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(); } } } |
- 注: 在我使用的程序中 极客。txt 文件,程序将创建代码中给定名称的新文件并写入其中。
输出:
MOHIT
- 写入(字节[]缓冲区,int偏移量,int maxlen):java。伊奥。过滤器输出流。写入(字节[]缓冲区,整数偏移量,整数最大值) 从偏移位置开始,将指定缓冲区中的maxlen字节写入输出流。
语法:
public void write(write(byte[] buffer, int offset, int maxlen)Parameters : buffer : Source Buffer to be written to the Output StreamReturn :buffer : Source Buffer to be writtenoffset : Starting offset maxlen : max no. of bytes to bewriten to the Output StreamException : In case any I/O error occurs.
- flush():java。伊奥。过滤器输出流。同花顺 刷新输出流,不允许将任何数据写入流。 语法:
public void flush()Parameters : ------Return :voidException : In case any I/O error occurs.
- close():java。伊奥。过滤器输出流。关闭() 关闭流并将所有分配的资源释放到流。 语法:
public void close()Parameters : ------Return :voidException : 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(); } } } |
注: 在我使用的程序中 极客。txt 文件,程序将创建代码中给定名称的新文件并写入其中。
输出:
MOHIT
本文由 莫希特·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END