20 Şubat 2020 Perşembe

PrintWriter Sınıfı - Formatlı Writer

Giriş
Text çıktı yazmak içindir.

PrintStream İle Farkı
Açıklaması şöyle. Java 1 ile gelen PrintStream yerine bu sınıf tercih edilmeli.

1. Bu sınıf ile "raw byte" yazılamaz.
Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.
2. flush
Açıklaması şöyle
The PrintWriter class has some differences when compared to the PrintStream class. In the PrintStream class, when automatic flushing is enabled, output will be sent when the newline character is the output.

But, in the PrintWriter class, when automatic flushing is enabled, ouput will be printed when the following methods are invoked: println, printf, etc.
constructor - File
Şöyle yaparız.
File source = new File("notebook.txt");
try (PrintWriter pw = new PrintWriter(source)) {
    out.print("...");
}
constructor - OutputStream
Açıklaması şöyle.
Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.
Şöyle yaparız.
PrintWriter writer=PrintWriter(System.out);  
writer.write("mypassword");   
constructor - Writer
İmzası şöyle.
PrintWriter(Writer wr)
PrintWriter(Writer wr, boolean autoFlush)
Writer olarak BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter kullanılabilir.

append metodu
Örnek ver

format metodu
En çok kullanılan ve işe yarayan metod bence bu
Örnek ver

print metodu - Object
Örnek
Şöyle yaparız.
out.print("...");
println metodu
Örnek
Şöyle yaparız
File file = ...
try (PrintWriter printWriter = new PrintWriter(new FileWriter(file))) {
  printWriter.println(...);
  ...
}

write metodu
İmzası şöyle.
PrintWriter.write(int oneChar)
PrintWriter.write(char[] buf)
PrintWriter.write(char[] buf, int offset, int count)
PrintWriter.write(String str)
PrintWriter.write(String str, int offset, int count)

Hiç yorum yok:

Yorum Gönder