Java IO笔记:标准IO
2013-09-07 16:09
218 查看
1.标准输入和输出
标准IO是指程序的输入和标准输出,用户和程序之间、程序和程序之间的交流都可以通过标准IO实现。Java中使用System.in(输入),System.out(输出)和System.err(错误输出)来提供程序的输入和输出。
2.读取输入数据
public class Echo {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = in.readLine()) != null && s.length() > 0) {
System.out.println(s);
}
}
}
3.重定向输入和输出
public class RedirectTest {
public static void main(String[] args) throws IOException {
//保存out,后面使用
PrintStream con = System.out;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
"test.txt"));
PrintStream out = new PrintStream(new BufferedOutputStream(
new FileOutputStream("test.out")));
//重定向输入到文件输入流 in
System.setIn(in);
//重定向输出到文件输出流
System.setOut(out);
System.setErr(out);
BufferedReader input = new BufferedReader(new InputStreamReader(
System.in));
String s;
while((s = input.readLine()) != null){
System.out.println(s);
}
out.close();
//输出重定向默认的
System.setOut(con);
}
}
标准IO是指程序的输入和标准输出,用户和程序之间、程序和程序之间的交流都可以通过标准IO实现。Java中使用System.in(输入),System.out(输出)和System.err(错误输出)来提供程序的输入和输出。
2.读取输入数据
public class Echo {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = in.readLine()) != null && s.length() > 0) {
System.out.println(s);
}
}
}
3.重定向输入和输出
public class RedirectTest {
public static void main(String[] args) throws IOException {
//保存out,后面使用
PrintStream con = System.out;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
"test.txt"));
PrintStream out = new PrintStream(new BufferedOutputStream(
new FileOutputStream("test.out")));
//重定向输入到文件输入流 in
System.setIn(in);
//重定向输出到文件输出流
System.setOut(out);
System.setErr(out);
BufferedReader input = new BufferedReader(new InputStreamReader(
System.in));
String s;
while((s = input.readLine()) != null){
System.out.println(s);
}
out.close();
//输出重定向默认的
System.setOut(con);
}
}
相关文章推荐
- java学习笔记IO输入与输出——常用工具类(1)
- JavaIO 总结笔记<六> 另类IO观点概括
- java IO输入输出流笔记
- Java IO学习笔记+代码(3)
- Java IO学习笔记六
- Java IO笔记(StreamTokenizer)
- java io 笔记一:RandomAccessFile(随机访问) 类
- 黑马程序员---Java基础学习笔记IO
- IO篇二( IO典型的使用方法 thinking in java 4th 阅读笔记)
- Java IO学习笔记(JDK1.5)
- java io 笔记二:FileOutPutStream、FileInPutStream、FileWriter、FileReader类
- Java基础-IO-File(笔记)
- [学习笔记]Java基础_IO(包含对象流序列化使用和RandomAccessFile 类的使用)
- Java IO学习笔记总结
- 备忘笔记:转载_JAVA标准输入读取整型
- 【转载】J2SE知识点归纳笔记(七)---Java IO Part 2:获取键盘输入与IO流体系图
- Java IO学习笔记:概念与原理
- 黑马程序员________Java中IO技术字节流字符流的应用及File类学习笔记
- Java学习笔记之IO(四):BufferedInputStream缓冲输入字节流
- Java学习笔记之IO(六):字节流对中文数据的读写