Java中的 PrintWriter print(String) 方法及示例
Java中PrintWriter类的print(String)方法用于在流上打印指定的 String 值。这个字符串值被当作一个参数。
句法:
public void print(String StringValue)
参数:此方法接受一个强制参数StringValue ,它是要写入流的字符串值。
返回值:此方法不返回任何值。
以下方法说明了 print(String) 方法的工作原理:
方案一:
// Java program to demonstrate
// PrintWriter print(String) method
import java.io.*;
class GFG {
public static void main(String[] args)
{
try {
// Create a PrintWriter instance
PrintWriter writer
= new PrintWriter(System.out);
// Print the String value 'GeeksForGeeks'
// to this stream using print() method
// This will put the StringValue in the
// stream till it is printed on the console
writer.print("GeeksForGeeks");
writer.flush();
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
GeeksForGeeks
方案二:
// Java program to demonstrate
// PrintWriter print(String) method
import java.io.*;
class GFG {
public static void main(String[] args)
{
try {
// Create a PrintWriter instance
PrintWriter writer
= new PrintWriter(System.out);
// Print the String value 'GFG'
// to this stream using print() method
// This will put the StringValue in the
// stream till it is printed on the console
writer.print("GFG");
writer.flush();
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
GFG