java 输入输出IO流:标准输入/输出System.in;System.out;System.err;【重定向输入System.setIn(FileinputStream);输出System.setOut(printStream);】
2024-08-28 21:10:37
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();
}
}
}
最新文章
- Chrome开发者工具不完全指南(二、进阶篇)
- Java链接MySQL练习题:格式化日期、性别;避免代码注入
- linux 服务的操作
- html5的选择器
- 【Unity Shaders】学习笔记——渲染管线
- [Codeforces673C]Bear and Colors(枚举,暴力)
- Windows版Cacti全新安装与升级
- 通过条件注释<;!--[if IE]>;<;!-->;判断浏览器
- 安装了C
- shell 批量创建带随机字符串的html文件
- pig加载两个不同字段个数的文件?load file with different items(f1有42列,f2有43列读到一个对象中)
- spring boot和swagger 整合
- oracle数据库 concat 与 ||
- Spring Boot安装及入门实现
- 洛谷 P5108 仰望半月的夜空 解题报告
- AngularJS路由系列(2)--刷新、查看路由,路由事件和URL格式,获取路由参数,路由的Resolve
- 使用Spring Cloud Security OAuth2搭建授权服务
- Highchar.js插件提示框千分位显示为空格而不是逗号 --(2018 08/06-08/12周总结)
- Internet History, Technology and Security (Week 9)
- 轻松使用div模拟select下拉菜单
热门文章
- 从零开始学Kotlin第三课
- idea添加插件后重启后报错:cannot load project xxxx 解决方案
- 洛谷 P5233 - [JSOI2012]爱之项链(Polya 定理+递推)
- Codeforces 436E - Cardboard Box(贪心/反悔贪心/数据结构)
- 洛谷 P3246 - [HNOI2016]序列(单调栈+前缀和)
- pip 与 conda
- MybatisPlus入门程序
- 05 Windows安装python3.6.4+pycharm环境
- Hive(五)【DQL数据查询】
- CentOS7 安装配置RocketMQ --主从模式(master-slave)异步复制