这是我在Thinking in java书上抄的一个例子:
import java.io.*;
public class Redirecting
{
public static void main (String[] args) throws IOException
{
PrintStream console=System.out;
BufferedInputStream in =new BufferedInputStream(new FileInputStream ("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));
String s=new String();
while((s=br.readLine())!=null&&s.length())
{
System.out.println(s);
}
out.close();
System.setOut(console);
}
}下面我就想问几个问题,希望大家给我解答一下,小弟是新手。
1,重定向到底是怎么回事?
2,以上程序我用vj++6.0编译,会报一个错误:PrintStream(OutputStream) ha s been deprecated by the author of /'java .io.PrintStream,为什么会报这个错误啊?我查过文档啊,没问题啊?
3,这个程序到底要实现什么功能啊?System.setOut(console); 这个语句的功能是什么啊?可能问得多了 点,不过麻烦大家给我解答一下,谢谢了
import java.io.*;
public class Redirecting
{
public static void main (String[] args) throws IOException
{
PrintStream console=System.out;
BufferedInputStream in =new BufferedInputStream(new FileInputStream ("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));
String s=new String();
while((s=br.readLine())!=null&&s.length())
{
System.out.println(s);
}
out.close();
System.setOut(console);
}
}下面我就想问几个问题,希望大家给我解答一下,小弟是新手。
1,重定向到底是怎么回事?
2,以上程序我用vj++6.0编译,会报一个错误:PrintStream(OutputStream) ha s been deprecated by the author of /'java .io.PrintStream,为什么会报这个错误啊?我查过文档啊,没问题啊?
3,这个程序到底要实现什么功能啊?System.setOut(console); 这个语句的功能是什么啊?可能问得多了 点,不过麻烦大家给我解答一下,谢谢了
System.setIn(in); 标准输入定向为Redirecting.java文件。
System.setOut(out);标准输出定向为:test.out文件。
http://www.blogjava.net/leon/articles/7892.html