JAVA伊奥。Java中的Writer类

这个抽象类用于编写字符流。子类必须实现的唯一方法是write(char[]、int、int)、flush()和close()。然而,大多数子类将重写此处定义的一些方法,以提供更高的效率和/或附加功能。 建造师

null
  • 受保护的写入程序(): 创建一个新的字符流编写器,其关键部分将在编写器本身上同步。
  • 受保护的写入程序(对象锁): 创建一个新的字符流编写器,其关键部分将在给定对象上同步。

方法:

  • Writer append(字符c): 将指定的字符追加到此写入程序。对这个表单方法的调用。append(c)的行为方式与调用完全相同 出来写(c)
    Syntax :public Writer append(char c)
                  throws IOException
    Parameters:
    c - The 16-bit character to append
    Returns:
    This writer
    Throws:
    IOException
  • 写入程序附加(CharSequence csq): 将指定的字符序列追加到此写入程序。对这个表单方法的调用。append(csq)的行为方式与调用完全相同 出来写入(csq.toString()) 根据字符序列csq的toString规范,可能不会附加整个序列。例如,调用字符缓冲区的toString方法将返回一个子序列,其内容取决于缓冲区的位置和限制。
    Syntax :public Writer append(CharSequence csq)
                  throws IOException
    Parameters:
    csq - The character sequence to append. If csq is null, 
    then the four characters "null" are appended to this writer.
    Returns:
    This writer
    Throws:
    IOException
  • Writer append(字符序列csq,int start,int end): 将指定字符序列的子序列追加到此写入程序。将指定字符序列的子序列追加到此写入程序
    Syntax :public Writer append(CharSequence csq,
                int start,
                int end)
                  throws IOException
    Parameters:
    csq - The character sequence from which a subsequence will be appended. 
    If csq is null, then characters will be appended as 
    if csq contained the four characters "null".
    start - The index of the first character in the subsequence
    end - The index of the character following the last character in the subsequence
    Returns:
    This writer
    Throws:
    IndexOutOfBoundsException
    IOException
  • 抽象void close(): 关闭水流,先冲洗。一旦流被关闭,进一步的write()或flush()调用将导致引发IOException。关闭以前关闭的流没有效果。
    Syntax :public abstract void close()
                        throws IOException
    Throws:
    IOException 
  • 抽象空刷新(): 冲水。如果流已将各种write()方法中的任何字符保存在缓冲区中,请立即将它们写入预期的目标。然后,如果目标是另一个字符或字节流,则刷新它。因此,一次flush()调用将刷新写入程序和输出流链中的所有缓冲区。
    Syntax :public abstract void flush()
                        throws IOException
    Throws:
    IOException
  • 无效写入(char[]cbuf): 写入一个字符数组。
    Syntax :public void write(char[] cbuf)
               throws IOException
    Parameters:
    cbuf - Array of characters to be written
    Throws:
    IOException - If an I/O error occurs
  • 摘要无效冲销(char[]cbuf,int off,int len): 写入字符数组的一部分。
    Syntax :public abstract void write(char[] cbuf,
             int off,
             int len)
                        throws IOException
    Parameters:
    cbuf - Array of characters
    off - Offset from which to start writing characters
    len - Number of characters to write
    Throws:
    IOException
  • 无效写入(int c): 只写一个字符。要写入的字符包含在给定整数值的16个低阶位中;16个高阶位被忽略。 打算支持高效的单字符输出的子类应该重写此方法。
    Syntax :public void write(int c)
               throws IOException
    Parameters:
    c - int specifying a character to be written
    Throws:
    IOException
  • 无效写入(字符串str): 写一个字符串。
    Syntax :public void write(String str)
               throws IOException
    Parameters:
    str - String to be written
    Throws:
    IOException
  • 无效写入(字符串str、int off、int len): 写入字符串的一部分。
    Syntax :public void write(String str,
             int off,
             int len)
               throws IOException
    Parameters:
    str - A String
    off - Offset from which to start writing characters
    len - Number of characters to write
    Throws:
    IndexOutOfBoundsException 

节目:

//Java program demonstrating Writer methods
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
class WriterDemo
{
public static void main(String[] args) throws IOException
{
Writer wr= new PrintWriter(System.out);
char c[] = { 'B' , 'C' , 'D' , 'E' , 'F' };
CharSequence cs = "JKL" ;
String str = "GHI" ;
//illustrating write(int c)
wr.write( 65 );
//flushing the stream
wr.flush();
//illustrating write(char[] c,int off,int len)
wr.write(c);
wr.flush();
//illustrating write(String str,int off,int len)
wr.write(str);
wr.flush();
//illustrating append(Charsequence cs,int start,int end)
wr.append(cs);
wr.flush();
//illustrating append(int ch)
wr.append( 'M' );
wr.flush();
//closing the stream
wr.close();
}
}


输出:

ABCDEFGHIJKLM

本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

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