📌  相关文章
📜  Java中的 PrintStream print(String) 方法及示例

📅  最后修改于: 2022-05-13 01:54:38.408000             🧑  作者: Mango

Java中的 PrintStream print(String) 方法及示例

Java中PrintStream类的print(String)方法用于在流上打印指定的 String 值。这个字符串值被当作一个参数。

句法:

public void print(String StringValue)

参数:此方法接受一个强制参数StringValue ,它是要写入流的字符串值。

返回值:此方法不返回任何值。

以下方法说明了 print(String) 方法的工作原理:

方案一:

// Java program to demonstrate
// PrintStream print(String) method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a PrintStream instance
            PrintStream stream
                = new PrintStream(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
            stream.print("GeeksForGeeks");
  
            stream.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
GeeksForGeeks

方案二:

// Java program to demonstrate
// PrintStream print(String) method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a PrintStream instance
            PrintStream stream
                = new PrintStream(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
            stream.print("GFG");
  
            stream.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
GFG