JAVA伊奥。Java中的Writer类

Writer class in JAVA

null

JAVA伊奥。作家班 是一个抽象类。它用于写入字符流。 宣言:

public abstract class Writer  extends Object    implements Appendable, Closeable, Flushable

施工人员:

  • 受保护的写入程序(): 创建一个新的字符流,该字符流本身可以在编写器上同步。
  • 受保护的写入程序(对象obj): 创建一个新的字符流,该字符流本身可以在给定对象“obj”上同步。

方法:

  • write(int char):java。伊奥。作家写入(int char) 将单个字符写入字符流。正在写入的字符包含在“char”整数值的16个低位中,其余16个高位被该方法忽略。 语法:
public void write(int char)Parameters : char : int value of the character to be written.Return  :voidException :-> IOException : if in case I/O error occurs.
  • write(String str):java。伊奥。作家写入(字符串str) 将字符串写入字符流。 语法:
public void write(String str)Parameters : str : string to be written to the character stream.Return  :voidException :-> IOException : if in case I/O error occurs.
  • write(String str,int offset,int maxlen):java。伊奥。作家写入(字符串str、int offset、int maxlen) 将字符串的某些部分写入字符流。 语法:
public void write(String str, int offset, int maxlen)Parameters : str : string to be written to the character stream.offset : start position of the Stringmaxlen : maximum length upto which string has to writtenReturn  :voidException :-> IOException : if in case I/O error occurs.-> IndexOutOfBoundsException : if offset is -ve or offset + maxlen = -ve || > maxlen
  • 写(char[]carray):java。伊奥。作家写(char[]carray) 将字符数组写入字符流。 语法:
public void write(char[] carray)Parameters : carray : character array to be written to the character streamReturn  :voidException :-> IOException : if in case I/O error occurs.
  • 写(char[]carray,int offset,int maxlen):java。伊奥。作家写入(字符[]carray,整数偏移,整数maxlen) 将字符数组的某些部分写入字符流。 语法:
public abstract void write(char[] carray, int offset, int maxlen)Parameters : carray : character to be written to the character streamoffset : start position of the character arraymaxlen : maximum no. of the character of the carray has to writtenReturn  :voidException :-> IOException : if in case I/O error occurs.
  • close():java。伊奥。作家关闭() 关闭字符流,首先刷新它。 语法:
public abstract void close()Parameters : -----------Return  :voidException :-> IOException : if in case I/O error occurs.
  • flush():java。伊奥。作家同花顺 刷新Writer流。刷新一个流调用将刷新链中的所有其他缓冲区。 语法:
public void flush()Parameters : -----Return  :voidException :-> IOException : if in case I/O error occurs.
  • 演示Writer类方法使用的Java程序:

JAVA

// Java program illustrating the working of Writer class methods
// write(int char), write(String str), close()
// write(String str, int offset, int maxlen), flush()
// write(char[] carray, int offset, int maxlen), write(char[] carray)
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
char [] carray = { 'G' , 'E' , 'E' , 'K' , 'S' };
// Initializing Writer
Writer geek_writer1 = new PrintWriter(System.out);
Writer geek_writer2 = new PrintWriter(System.out);
Writer geek_writer3 = new PrintWriter(System.out);
Writer geek_writer4 = new PrintWriter(System.out);
Writer geek_writer5 = new PrintWriter(System.out);
// Use of write(int char) : to write a character
geek_writer1.write( 71 );
geek_writer1.write( 70 );
geek_writer1.write( 71 );
// Use of flush() method
System.out.print("Using write( int char []) : ");
geek_writer1.flush();
String str = "Hello Geeks";
// Use of write(String str) : to write string
geek_writer2.write(str);
// Value written by write(String str)
System.out.print("Using write(String str) : ");
geek_writer2.flush();
// Use of write(String str, int offset, int maxlen)
//: to write part of string
geek_writer3.write(str, 2 , 4 );
geek_writer3.write(str, 5 , 6 );
// Value written by write(String str, int offset, int maxlen)
System.out.print("Using write(str, offset, maxlen) : ");
geek_writer3.flush();
geek_writer4.write(carray);
System.out.print("Using write( char [] carray) : ");
geek_writer4.flush();
// Use of write(char[] carray, int offset, int maxlen):
// to write part of char array
geek_writer5.write(carray, 1 , 3 );
// Value written by write(String str, int offset, int maxlen)
System.out.print("Using write(carray, offset, maxlen) : ");
geek_writer5.flush();
// Use of close() method
geek_writer1.close();
geek_writer2.close();
geek_writer3.close();
geek_writer4.close();
geek_writer5.close();
}
}


  • 输出:
Using write(int char[]) : GFGUsing write(String str) : Hello GeeksUsing write(str, offset, maxlen) : llo  GeeksUsing write(char[] carray) : GEEKSUsing write(carray, offset, maxlen) : EEK
  • append(char Sw):java。伊奥。作家附加(字符Sw) 将单个字符附加到作者。 语法:
public Writer append(char Sw)Parameters : Sw : character to be appendReturn  :WriterException :-> IOException : if in case I/O error occurs.
  • append(CharSequence char_sq):java。伊奥。作家追加(CharSequence char_sq) 将指定的字符序列追加到写入程序。 语法:
public Writer append(CharSequence char_sq)Parameters : char_sq : Character sequence to append. Return  :Writer, if char sequence is null, then NULL appends to the Writer.Exception :-> IOException : if in case I/O error occurs.
  • append(CharSequence char_sq,int start,int end):java。伊奥。作家追加(字符序列char_sq,int start,int end) 将字符序列的指定部分追加到写入程序。 语法:
public Writer append(CharSequence char_sq, int start, int end)Parameters : char_sq : Character sequence to append.start : start of character in the Char Sequenceend : end of character in the Char SequenceReturn  :voidException :-> IOException : if in case I/O error occurs.-> IndexOutOfBoundsException : if start or end are -ve or start > end or                                   end > char_sq.length
  • 演示Writer类方法使用的Java程序:

JAVA

// Java program illustrating the working of Writer class methods
// append(CharSequence char_sq), append(char Sw)
// append(CharSequence char_sq, int start,int end)
// flush()
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// Initializing String Writer
Writer geek_writer1 = new PrintWriter(System.out);
Writer geek_writer2 = new PrintWriter(System.out);
Writer geek_writer3 = new PrintWriter(System.out);
// Use of write(int char) : to write a character
geek_writer1.append( 'G' );
geek_writer1.append( 'G' );
geek_writer1.append( 'G' );
geek_writer1.append( 'G' );
geek_writer1.append( 'G' );
// Use of append(char Sw)
System.out.print("append( char Sw) : ");
geek_writer1.flush();
// Initializing Character Sequence
CharSequence char_sq1 = " 1 Hello 1 ";
CharSequence char_sq2 = " : 2 Geeks 2 ";
// Use of append(CharSequence char_sq)
geek_writer2.append(char_sq1);
geek_writer2.append(char_sq2);
System.out.print("append(char_sq) : ");
geek_writer2.flush();
// Use of append(CharSequence char_sq,int start,int end)
geek_writer3.append(char_sq1, 0 , 3 );
geek_writer3.append(char_sq2, 3 , 6 );
System.out.print("append(char_sq,start,end) : ");
geek_writer3.flush();
}
}


  • 输出:
Using write(int char) : GFGappend(char Sw) : GGGGGappend(char_sq) : 1 Hello 1 : 2 Geeks 2append(char_sq,start,end) : 1 H2 G

本文由 莫希特·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享