JavaのPrintWriter紹介_動力ノードJava学院の整理
18827 ワード
PrintWriterの紹介
PrintWriterは文字タイプのプリント出力ストリームであり、Writerに引き継がれています。
PrintStreamは、テキストにストリーム印刷オブジェクトを出力するフォーマット表示形式である。これはPrintStreamにおけるすべてのprint方法を実現する。元のバイトを書き込む方法は含まれていません。これらのバイトに対しては、プログラムは符号化されていないバイトストリームを使って書き込みを行うべきです。
PrintWriter関数一覧
PrintWriterにおけるAPIの詳細な使い方については、例示コード(PrintWriter Test.java)を参照する。
file.txtの内容は以下の通りです。
PrintWriterは文字タイプのプリント出力ストリームであり、Writerに引き継がれています。
PrintStreamは、テキストにストリーム印刷オブジェクトを出力するフォーマット表示形式である。これはPrintStreamにおけるすべてのprint方法を実現する。元のバイトを書き込む方法は含まれていません。これらのバイトに対しては、プログラムは符号化されていないバイトストリームを使って書き込みを行うべきです。
PrintWriter関数一覧
PrintWriter(OutputStream out)
PrintWriter(OutputStream out, boolean autoFlush)
PrintWriter(Writer wr)
PrintWriter(Writer wr, boolean autoFlush)
PrintWriter(File file)
PrintWriter(File file, String csn)
PrintWriter(String fileName)
PrintWriter(String fileName, String csn)
PrintWriter append(char c)
PrintWriter append(CharSequence csq, int start, int end)
PrintWriter append(CharSequence csq)
boolean checkError()
void close()
void flush()
PrintWriter format(Locale l, String format, Object... args)
PrintWriter format(String format, Object... args)
void print(float fnum)
void print(double dnum)
void print(String str)
void print(Object obj)
void print(char ch)
void print(char[] charArray)
void print(long lnum)
void print(int inum)
void print(boolean bool)
PrintWriter printf(Locale l, String format, Object... args)
PrintWriter printf(String format, Object... args)
void println()
void println(float f)
void println(int i)
void println(long l)
void println(Object obj)
void println(char[] chars)
void println(String str)
void println(char c)
void println(double d)
void println(boolean b)
void write(char[] buf, int offset, int count)
void write(int oneChar)
void write(char[] buf)
void write(String str, int offset, int count)
void write(String str)
PrintWriter
package java.io;
import java.util.Objects;
import java.util.Formatter;
import java.util.Locale;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
public class PrintWriter extends Writer {
protected Writer out;
// flush
// “ flush”, print(), println(), write() , flush() ;
// “ flush”, flush() 。
private final boolean autoFlush;
// PrintWriter 。 PrintWriter , , trouble true
private boolean trouble = false;
//
private Formatter formatter;
private PrintStream psOut = null;
//
private final String lineSeparator;
// csn( ) Chaset
private static Charset toCharset(String csn)
throws UnsupportedEncodingException
{
Objects.requireNonNull(csn, "charsetName");
try {
return Charset.forName(csn);
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
// UnsupportedEncodingException should be thrown
throw new UnsupportedEncodingException(csn);
}
}
// “Writer out” PrintWriter , flush, 。
public PrintWriter (Writer out) {
this(out, false);
}
// “Writer out” PrintWriter ,autoFlush flush , 。
public PrintWriter(Writer out, boolean autoFlush) {
super(out);
this.out = out;
this.autoFlush = autoFlush;
lineSeparator = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
}
// “ out” PrintWriter , flush, 。
public PrintWriter(OutputStream out) {
this(out, false);
}
// “ out” PrintWriter ,autoFlush flush , 。
public PrintWriter(OutputStream out, boolean autoFlush) {
// new OutputStreamWriter(out): “ ” “ ”
// new BufferedWriter(...): 。
this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
// save print stream for error propagation
if (out instanceof java.io.PrintStream) {
psOut = (PrintStream) out;
}
}
// fileName OutputStreamWriter, BufferedWriter ; BufferedWriter PrintWriter , flush, 。
public PrintWriter(String fileName) throws FileNotFoundException {
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
false);
}
// fileName OutputStreamWriter, BufferedWriter ; BufferedWriter PrintWriter , flush, charset。
private PrintWriter(Charset charset, File file)
throws FileNotFoundException
{
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)),
false);
}
// fileName OutputStreamWriter, BufferedWriter ; BufferedWriter PrintWriter , flush, csn 。
public PrintWriter(String fileName, String csn)
throws FileNotFoundException, UnsupportedEncodingException
{
this(toCharset(csn), new File(fileName));
}
// file OutputStreamWriter, BufferedWriter ; BufferedWriter PrintWriter , flush, 。
public PrintWriter(File file) throws FileNotFoundException {
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
false);
}
// file OutputStreamWriter, BufferedWriter ; BufferedWriter PrintWriter , flush, csn 。
public PrintWriter(File file, String csn)
throws FileNotFoundException, UnsupportedEncodingException
{
this(toCharset(csn), file);
}
private void ensureOpen() throws IOException {
if (out == null)
throw new IOException("Stream closed");
}
// flush“PrintWriter ”。
public void flush() {
try {
synchronized (lock) {
ensureOpen();
out.flush();
}
}
catch (IOException x) {
trouble = true;
}
}
public void close() {
try {
synchronized (lock) {
if (out == null)
return;
out.close();
out = null;
}
}
catch (IOException x) {
trouble = true;
}
}
// flush“PrintWriter ”,
public boolean checkError() {
if (out != null) {
flush();
}
if (out instanceof java.io.PrintWriter) {
PrintWriter pw = (PrintWriter) out;
return pw.checkError();
} else if (psOut != null) {
return psOut.checkError();
}
return trouble;
}
protected void setError() {
trouble = true;
}
protected void clearError() {
trouble = false;
}
// c “PrintWriter ” 。c int ,
public void write(int c) {
try {
synchronized (lock) {
ensureOpen();
out.write(c);
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
// “buf off len ” “PrintWriter ” 。
public void write(char buf[], int off, int len) {
try {
synchronized (lock) {
ensureOpen();
out.write(buf, off, len);
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
// “buf ” “PrintWriter ” 。
public void write(char buf[]) {
write(buf, , buf.length);
}
// “ s off len ” “PrintWriter ” 。
public void write(String s, int off, int len) {
try {
synchronized (lock) {
ensureOpen();
out.write(s, off, len);
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
// “ s” “PrintWriter ” 。
public void write(String s) {
write(s, , s.length());
}
// “ ” “PrintWriter ” 。
private void newLine() {
try {
synchronized (lock) {
ensureOpen();
out.write(lineSeparator);
if (autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
// “boolean ” “PrintWriter ” ,print write
public void print(boolean b) {
write(b ? "true" : "false");
}
// “ c ” “PrintWriter ” ,print write
public void print(char c) {
write(c);
}
// “int i ” “PrintWriter ” ,print write
public void print(int i) {
write(String.valueOf(i));
}
// “long l ” “PrintWriter ” ,print write
public void print(long l) {
write(String.valueOf(l));
}
// “float f ” “PrintWriter ” ,print write
public void print(float f) {
write(String.valueOf(f));
}
// “double d ” “PrintWriter ” ,print write
public void print(double d) {
write(String.valueOf(d));
}
// “ s” “PrintWriter ” ,print write
public void print(char s[]) {
write(s);
}
// “ s” “PrintWriter ” ,print write
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
// “ obj ” “PrintWriter ” ,print write
public void print(Object obj) {
write(String.valueOf(obj));
}
// “ ” “PrintWriter ” ,println write
public void println() {
newLine();
}
// “boolean + ” “PrintWriter ” ,println write
public void println(boolean x) {
synchronized (lock) {
print(x);
println();
}
}
// “ x + ” “PrintWriter ” ,println write
public void println(char x) {
synchronized (lock) {
print(x);
println();
}
}
// “int + ” “PrintWriter ” ,println write
public void println(int x) {
synchronized (lock) {
print(x);
println();
}
}
// “long + ” “PrintWriter ” ,println write
public void println(long x) {
synchronized (lock) {
print(x);
println();
}
}
// “float + ” “PrintWriter ” ,println write
public void println(float x) {
synchronized (lock) {
print(x);
println();
}
}
// “double + ” “PrintWriter ” ,println write
public void println(double x) {
synchronized (lock) {
print(x);
println();
}
}
// “ x+ ” “PrintWriter ” ,println write
public void println(char x[]) {
synchronized (lock) {
print(x);
println();
}
}
// “ x+ ” “PrintWriter ” ,println write
public void println(String x) {
synchronized (lock) {
print(x);
println();
}
}
// “ o + ” “PrintWriter ” ,println write
public void println(Object x) {
String s = String.valueOf(x);
synchronized (lock) {
print(s);
println();
}
}
// “ args” “ Locale ( )” format , “PrintWriter ”
public PrintWriter printf(String format, Object ... args) {
return format(format, args);
}
// “ args” “Locale ( )” format , “PrintWriter ”
public PrintWriter printf(Locale l, String format, Object ... args) {
return format(l, format, args);
}
// “ Locale ( )”
public PrintWriter format(String format, Object ... args) {
try {
synchronized (lock) {
ensureOpen();
if ((formatter == null)
|| (formatter.locale() != Locale.getDefault()))
formatter = new Formatter(this);
formatter.format(Locale.getDefault(), format, args);
if (autoFlush)
out.flush();
}
} catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
} catch (IOException x) {
trouble = true;
}
return this;
}
// “Locale ( )”
public PrintWriter format(Locale l, String format, Object ... args) {
try {
synchronized (lock) {
ensureOpen();
if ((formatter == null) || (formatter.locale() != l))
formatter = new Formatter(this, l);
formatter.format(l, format, args);
if (autoFlush)
out.flush();
}
} catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
} catch (IOException x) {
trouble = true;
}
return this;
}
// “ ” “PrintWriter ”
public PrintWriter append(CharSequence csq) {
if (csq == null)
write("null");
else
write(csq.toString());
return this;
}
// “ start( ) end( ) ” “PrintWriter ”
public PrintWriter append(CharSequence csq, int start, int end) {
CharSequence cs = (csq == null ? "null" : csq);
write(cs.subSequence(start, end).toString());
return this;
}
// “ c” “PrintWriter ”
public PrintWriter append(char c) {
write(c);
return this;
}
}
サンプルコードPrintWriterにおけるAPIの詳細な使い方については、例示コード(PrintWriter Test.java)を参照する。
import java.io.PrintWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* PrintWriter
*
*
*/
public class PrintWriterTest {
public static void main(String[] args) {
// : “abcde” “file.txt” 。
// !
testPrintWriterConstrutor() ;
//testPrintWriterConstrutor() ;
//testPrintWriterConstrutor() ;
// write(), print(), println(), printf() 。
testPrintWriterAPIS() ;
}
/**
* PrintWriter(OutputStream out)
*
* , “abcde” “file.txt”
*/
private static void testPrintWriterConstrutor() {
final char[] arr={'a', 'b', 'c', 'd', 'e' };
try {
// “file.txt” File
File file = new File("file.txt");
// FileOutputStream
PrintWriter out = new PrintWriter(
new FileOutputStream(file));
// “ arr”
out.write(arr);
//
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* PrintWriter(File file)
*
* , “abcde” “file.txt”
*/
private static void testPrintWriterConstrutor() {
final char[] arr={'a', 'b', 'c', 'd', 'e' };
try {
File file = new File("file.txt");
PrintWriter out = new PrintWriter(file);
out.write(arr);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* PrintWriter(String fileName)
*
* , “abcde” “file.txt”
*/
private static void testPrintWriterConstrutor() {
final char[] arr={'a', 'b', 'c', 'd', 'e' };
try {
PrintWriter out = new PrintWriter("file.txt");
out.write(arr);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* write(), print(), println(), printf() 。
*/
private static void testPrintWriterAPIS() {
final char[] arr={'a', 'b', 'c', 'd', 'e' };
try {
// FileOutputStream
PrintWriter out = new PrintWriter("other.txt");
// “hello PrintWriter”+ ,
out.println("hello PrintWriter");
// x
// x ASCII 'A', 'A'
out.write(x);
// "" 。
// out.print(x); out.write(String.valueOf(x));
out.print(x);
// 'B'
out.append('B').append("CDEF");
// "CDE is " +
String str = "GHI";
int num = ;
out.printf("%s is %d
", str, num);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
上のコードを実行すると、ソースディレクトリで2つのファイル「file.txt」と「other.txt」が生成されます。file.txtの内容は以下の通りです。
abcde
other.txtの内容は以下の通りです。
hello PrintWriter
A65BCDEFGHI is 5
以上は小编が皆さんに绍介したJavaのPrintWriter知识です。皆さんのために役に立つことを望んでいます。ここでも私たちのサイトを応援してくれてありがとうございます。