import java.io.*; public class IO2File { public static void main(String[] args) throws IOException { File f=new File("out.txt"); f.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(f); PrintStream printStream = new PrintStream(fileOutputStream); System.setOut(printStream); System.out.println("默认输出到控制台的这一句,输出到了文件 out.txt"); } }