62,628
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
File file = new File("D:\\a.txt");
PrintStream ps = null;
try {
PrintStream pp = System.out;
ps = new PrintStream(file);
System.setOut(ps);
System.out.println("AAAAAAAAAAAAAAAAA");
System.out.println("球");
System.setOut(pp);
System.out.println("PPPPPPPPPPPPPPPP");
ps.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
ps.close();
}
}
public static void main(String[] args) {
File file = new File("D:\\a.txt");
PrintStream ps = null;
try {
ps = new PrintStream(file);
System.setOut(ps);
System.out.println("AAAAAAAAAAAAAAAAA");
System.out.println("球");
PrintStream pp = System.out;
System.setOut(pp);
System.out.println("PPPPPPPPPPPPPPPP");
ps.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
ps.close();
}
}
public static void main(String[] args) {
File file = new File("D:\\a.txt");
PrintStream ps = null;
try {
PrintStream pp = System.out; //这里的System.out; 是系统标准输出(默认是控制台),所以pp指向系统标准输出
ps = new PrintStream(file);
System.setOut(ps); //这里setOut后,System.out=ps,也就是System.out也指向了文件(注意这里pp本身不变,还是指向系统标准输出,就好比 int a=5; int b=a; a=10; b保持5不变,所以同理pp不变)
System.out.println("AAAAAAAAAAAAAAAAA"); //所以这里会输出到文件
System.out.println("球");
System.setOut(pp); //这里setOut后,System.out=pp=系统标准输出
System.out.println("PPPPPPPPPPPPPPPP"); //所以这里就会输出到控制台
ps.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
ps.close();
}
}public static void main(String[] args) {
File file = new File("D:\\a.txt");
PrintStream ps = null;
try {
ps = new PrintStream(file); //ps指向文件
System.setOut(ps); //这里setOut以后,System.out=ps,所以System.out也指向文件
System.out.println("AAAAAAAAAAAAAAAAA"); //所以这里会输出到文件
System.out.println("球");
PrintStream pp = System.out; //这里pp=System.out; 因为System.out=ps,所以相当于 pp=ps; 所以pp也指向文件
System.setOut(pp); //所以这里setOut后,System.out还是指向文件
System.out.println("PPPPPPPPPPPPPPPP"); //所以这里会输出到文件
ps.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
ps.close();
}
}