void print(Object obj)
描述 (Description)
java.io.PrintWriter.print()方法打印一个对象。 String.valueOf(Object)方法生成的字符串根据平台的默认字符编码转换为字节,这些字节的写入方式与write(int)方法完全相同。
声明 (Declaration)
以下是java.io.PrintWriter.print()方法的声明。
public void print(Object obj)
参数 (Parameters)
obj - 要打印的对象。
返回值 (Return Value)
此方法不返回值。
异常 (Exception)
NA
例子 (Example)
以下示例显示了java.io.PrintWriter.print()方法的用法。
package com.iowiki;
import java.io.*;
import java.util.Date;
public class PrintWriterDemo {
public static void main(String[] args) {
Object obj1 = "Object";
Object obj2 = 2;
Date date = new Date(112, 2, 21);
try {
// create a new writer
PrintWriter pw = new PrintWriter(System.out);
// print object
pw.print(obj1);
// change the line
pw.println();
// print another object
pw.print(obj2);
// change the line
pw.println();
// print a date (it is an object)
pw.print(date);
// flush the writer
pw.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Object
2
Wed Mar 21 00:00:00 EET 2012