Java的标准输入输出分别通过System.in和System.out来代表的,在默认情况下它分别代表键盘和显示器,当程序通过System.in来获取输入时,实际上是从键盘读取输入 当程序试图通过 System.out 执行输出时,程序总是输出到屏幕:
 
从System类的源码可以看出in;out;err这几个常量 的类型

重定向标准输出到文件:System.setOut(printStream);

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream; /**
* @ClassName PrintStreamExample
* @projectName: object1
* @author: Zhangmingda
* @description: XXX
* date: 2021/4/18.
*/
public class PrintStreamExample {
public static void main(String[] args) {
try(PrintStream printStream = new PrintStream(new FileOutputStream("输入输出文件读写/src/test/output/System.out.txt"))) {
System.setOut(printStream);
System.out.println("卧槽,打印到哪里了????"); //打印到文件里面了
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

重定向输入System.setIn(FileinputStream);

import com.sun.jdi.PathSearchingVirtualMachine;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner; /**
* @ClassName InputStreamSystemInExample
* @projectName: object1
* @author: Zhangmingda
* @description: XXX
* date: 2021/4/18.
*/
public class InputStreamSystemInExample {
public static void main(String[] args) {
try (InputStream inputStream = new FileInputStream("输入输出文件读写/src/test/output/System.out.txt")){
System.setIn(inputStream);
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()){ //看看是否还有下一行
System.out.println(scanner.nextLine()); //打印下一行
}
}catch (FileNotFoundException e ){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}

  

最新文章

  1. Chrome开发者工具不完全指南(二、进阶篇)
  2. Java链接MySQL练习题:格式化日期、性别;避免代码注入
  3. linux 服务的操作
  4. html5的选择器
  5. 【Unity Shaders】学习笔记——渲染管线
  6. [Codeforces673C]Bear and Colors(枚举,暴力)
  7. Windows版Cacti全新安装与升级
  8. 通过条件注释<!--[if IE]><!-->判断浏览器
  9. 安装了C
  10. shell 批量创建带随机字符串的html文件
  11. pig加载两个不同字段个数的文件?load file with different items(f1有42列,f2有43列读到一个对象中)
  12. spring boot和swagger 整合
  13. oracle数据库 concat 与 ||
  14. Spring Boot安装及入门实现
  15. 洛谷 P5108 仰望半月的夜空 解题报告
  16. AngularJS路由系列(2)--刷新、查看路由,路由事件和URL格式,获取路由参数,路由的Resolve
  17. 使用Spring Cloud Security OAuth2搭建授权服务
  18. Highchar.js插件提示框千分位显示为空格而不是逗号 --(2018 08/06-08/12周总结)
  19. Internet History, Technology and Security (Week 9)
  20. 轻松使用div模拟select下拉菜单

热门文章

  1. 从零开始学Kotlin第三课
  2. idea添加插件后重启后报错:cannot load project xxxx 解决方案
  3. 洛谷 P5233 - [JSOI2012]爱之项链(Polya 定理+递推)
  4. Codeforces 436E - Cardboard Box(贪心/反悔贪心/数据结构)
  5. 洛谷 P3246 - [HNOI2016]序列(单调栈+前缀和)
  6. pip 与 conda
  7. MybatisPlus入门程序
  8. 05 Windows安装python3.6.4+pycharm环境
  9. Hive(五)【DQL数据查询】
  10. CentOS7 安装配置RocketMQ --主从模式(master-slave)异步复制