进阶day10-打印流的基本使用(PrintStream)(了解)

This commit is contained in:
2026-03-25 14:49:58 +08:00
parent c59f70cb24
commit ce94c2636e

View File

@@ -0,0 +1,32 @@
package com.inmind.printstream_04;
import java.io.FileNotFoundException;
import java.io.PrintStream;
/*
打印流的基本使用(PrintStream)(了解)
构造方法
PrintStream(String fileName) 使用指定的文件名创建新的打印流,无需自动换行
常用方法:
print():直接输出内容,但不换行
println():直接输出内容,但换行
*/
public class Demo14 {
//修改掉sout的打印位置
public static void main(String[] args) throws FileNotFoundException {
PrintStream ps = new PrintStream("dest.txt");
System.setOut(ps);//修改了系统级别的打印位置
System.out.println("这是修改了系统级别的打印位置后的sout");
}
//打印流的基本使用
public static void method(String[] args) throws FileNotFoundException {
//往相对路径dest.txt中打印字符串
PrintStream ps = new PrintStream("dest.txt");
ps.println("你好java");
ps.print("helloworld");
System.out.println("你好java");
ps.close();
}
}