public static void main(String[] args) throws IOException {
PrintStream printStream = System.out;
//在默认情况下,PrintStream 输出数据的位置 标准输出,即显示器
printStream.print("Tom,hello");
/*
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
*/
//因为print底层使用的是write,所以我们可以直接调用write进行打印/输出
printStream.write("你好,元宇宙".getBytes());//write是字节流
//我们可以修改打印流的位置/设备
//1.输出修改成到"e:\\f1.txt"
//2."Tom,hello" 就会输出到 "e:\\f1.txt"
System.setOut(new PrintStream("E:\\f1.txt"));
printStream.print("Tom,hello");
System.out.println("打印完毕~");//已经打印到文件中
printStream.close();
}
便捷方式:
public static void main(String[] args) throws IOException {
PrintStream printStream = new PrintStream("E:\\f1.txt");
printStream.println("Tom,hello");
printStream.write("你好,元宇宙".getBytes());
System.out.println("已经打印到文件中~");
printStream.close();
}
public static void main(String[] args) throws IOException {
// PrintWriter printWriter = new PrintWriter(System.out);
PrintWriter printWriter = new PrintWriter(new FileWriter("E:\\f2.txt"));
printWriter.print("hi,铜陵你好~");
System.out.println("打印完成!");
printWriter.close();
}
更改输出的位置~