📜  Java Java类

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

Java Java类

JAVA中的作家类

Java.io.Writer 类是一个抽象类。它用于写入字符流。
宣言 :

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

构造函数:

  • protected Writer() :创建一个新的字符流,它本身可以在编写器上同步。
  • protected Writer(Object obj) :创建一个新的字符流,它本身可以在给定的对象上同步 - 'obj'。

方法:

  • write(int char) : Java.io.Writer.write(int char)将单个字符写入字符流。正在写入的字符包含在“char”整数值的 16 个低位中,其余 16 个高位被该方法忽略。
    句法:
public void write(int char)
Parameters : 
char : int value of the character to be written.
Return  :
void
Exception :
-> IOException : if in case I/O error occurs.
  • write(String str) : Java.io.Writer.write(String str)将字符串写入字符流。
    句法:
public void write(String str)
Parameters : 
str : string to be written to the character stream.
Return  :
void
Exception :
-> IOException : if in case I/O error occurs.
  • write(String str, int offset, int maxlen) : Java.io.Writer.write(String 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 String
maxlen : maximum length upto which string has to written
Return  :
void
Exception :
-> IOException : if in case I/O error occurs.
-> IndexOutOfBoundsException : if offset is -ve or offset + maxlen = -ve || > maxlen
  • write(char[] carray) : Java.io.Writer.write(char[] carray)将字符数组写入字符流。
    句法:
public void write(char[] carray)
Parameters : 
carray : character array to be written to the character stream
Return  :
void
Exception :
-> IOException : if in case I/O error occurs.
  • write(char[] carray, int offset, int maxlen) : Java.io.Writer.write(char[] carray, int offset, int maxlen)将字符数组的一部分写入字符流。
    句法:
public abstract void write(char[] carray, int offset, int maxlen)
Parameters : 
carray : character to be written to the character stream
offset : start position of the character array
maxlen : maximum no. of the character of the carray has to written
Return  :
void
Exception :
-> IOException : if in case I/O error occurs.
  • close() : Java.io.Writer.close()关闭字符流,首先刷新它。
    句法:
public abstract void close()
Parameters : 
-----------
Return  :
void
Exception :
-> IOException : if in case I/O error occurs.
  • flush() : Java.io.Writer.flush()刷新 Writer 流。刷新一个流调用将刷新链中的所有其他缓冲区。
    句法:
public void flush()
Parameters : 
-----
Return  :
void
Exception :
-> 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("\nUsing 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("\nUsing write(str, offset, maxlen) : ");
        geek_writer3.flush();
 
        geek_writer4.write(carray);
        System.out.print("\nUsing 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("\nUsing 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();
                 
    }
}


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("\nappend(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("\nappend(char_sq,start,end) : ");
        geek_writer3.flush();
 
    }
}


  • 输出 :
Using write(int char[]) : GFG
Using write(String str) : Hello Geeks
Using write(str, offset, maxlen) : llo  Geeks
Using write(char[] carray) : GEEKS
Using write(carray, offset, maxlen) : EEK
  • append(char Sw) : Java.io.Writer.append(char Sw)将单个字符附加到 Writer。
    句法:
public Writer append(char Sw)
Parameters : 
Sw : character to be append
Return  :
Writer
Exception :
-> IOException : if in case I/O error occurs.
  • append(CharSequence char_sq) : Java.io.Writer.append(CharSequence char_sq)将指定的字符序列附加到 Writer。
    句法:
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.io.Writer.append(CharSequence char_sq, int start, int end)将字符序列的指定部分附加到 Writer。
    句法:
public Writer append(CharSequence char_sq, int start, int end)
Parameters : 
char_sq : Character sequence to append.
start : start of character in the Char Sequence
end : end of character in the Char Sequence
Return  :
void
Exception :
-> 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("\nappend(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("\nappend(char_sq,start,end) : ");
        geek_writer3.flush();
 
    }
}
  • 输出 :
Using write(int char) : GFG
append(char Sw) : GGGGG
append(char_sq) : 1 Hello 1 : 2 Geeks 2
append(char_sq,start,end) : 1 H2 G