output redirectionOut:
public class RedirectOut {
public static void main(String[] args) throws FileNotFoundException {
try {
var ps = new PrintStream(new FileOutputStream("out.txt"));
System.setOut(ps);
System.out.println("普通字符串");
System.out.println(new RedirectOut()); } catch (FileNotFoundException e) {
e.printStackTrace();
} }
}
input redirectionIn:
public class RedirectionIn {
public static void main(String[] args) {
try {
var fis = new FileInputStream("./src/com/company/RedirectionIn.java");
System.setIn(fis);
var sc = new Scanner(System.in);
sc.useDelimiter("\n");
while (sc.hasNext()){
System.out.println("键盘输入的内容是:" + sc.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}