Java PrintWriter print(String)方法及实例
Java中PrintWriter类的print(String)方法用于在流中打印指定的String值。这个字符串值被作为一个参数。
语法。
public void print(String StringValue)
参数。该方法接受一个强制性参数StringValue,它是要写入流的字符串值。
返回值。该方法不返回任何值。
下面的方法说明了print(String)方法的工作。
程序1:
// 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
程序2。
// 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