欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java需要关注的知识点---标准I0流

程序员文章站 2022-04-09 13:09:40
...
[color=red][b]System.in[/b][/color]

public class SystemInTest {
public static void main(String[] args) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String s;
while((s = stdin.readLine())!= null && s.length()!= 0) {
System.out.println(s);
}
}
}

将System.out转换成PrintWriter:

public class SystemOutTest {
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out,true);
out.println("Hello,World");
}
}


标准I0的重定向:

public class Redirecting {
public static void main(String[] args) throws IOException, InterruptedException {
PrintStream console = System.out;
BufferedInputStream in = new BufferedInputStream(new FileInputStream("D:\\RSA75WP\\test\\src\\com\\io\\Redirecting.java"));
PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out")));
System.setIn(in);
System.setOut(out);
System.setErr(out);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Thread.sleep(4000);
String s;
while((s = br.readLine())!= null) {
System.out.println(s);
}
out.close();
System.setOut(console);
}
}
相关标签: IO