【Java筑基】IO流基础之常见工具流和进程通信

前 言
🍉 作者简介:半旧518,长跑型选手,立志坚持写10年博客,专注于java后端
☕专栏简介:深入、全面、系统的介绍java的基础知识
🌰 文章简介:本文将深入全面介绍IO流知识,建议收藏备用,创作不易,敬请三连哦
🍎大厂真题:大厂面试真题大全

1.转换流

字符流比字节流在操作上更加方便,Java提供了转换流来实现字节流向字符流的转换。

    public class KeyinTest {
    	public static void main(String[] args) {
    		try (InputStreamReader reader = new InputStreamReader(System.in);
                 	//BufferedReader has readLine() to read by Line
    				BufferedReader br = new BufferedReader(reader)) {
    			String line = null;
    			while ((line = br.readLine()) != null) {
    				if (line.equals("exit")) {
    					System.exit(1);
    				}
    				;
    				System.out.println("输出的内容是:" + line);
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }

2.推回输入流

PushbackInputStream和PushbackReader是推回输入流,它们有一片推回缓冲区域,在读取数据时,会优先从推回缓冲区域读取,只有推回缓冲区域的内容没有装满read()所需数组的大小时才会去流中读取。

    public class PushbackTest {
    	public static void main(String[] args) {
    		// 指定推回缓冲区长度为64.
    		try (PushbackReader pr = new PushbackReader(new FileReader(
    				"src/inputandoutput/PushbackTest.java"), 64)) {
    			char[] buff = new char[32];
    			String lastContent = "";
    			int hasRead = 0;
    			while ((hasRead = pr.read(buff)) > 0) {
    				String content = new String(buff, 0, hasRead);
    				int targetIndex = 0;
    				if ((targetIndex = (content + lastContent)
    						.indexOf("new PushbackIndex")) > 0) {
    					pr.unread((lastContent + content).toCharArray());
    					if (targetIndex > 32) {
    						buff = new char[targetIndex];
    					}
    					pr.read(buff, 0, targetIndex);
    					System.out.print(new String(buff, 0, targetIndex));
    					System.exit(0);
    				} else {
    					System.out.println(lastContent);
    					lastContent = content;
    				}
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }

3.标准输入、输出流

System类中提供了重定向标准输入、输出的方法。

public class RedirectOut {
	public static void main(String[] args) {
		try (PrintStream ps = new PrintStream(new FileOutputStream("out.txt"))) {
			// redirect the output to ps
			System.setOut(ps);
			System.out.println("hello");
			System.out.println(new RedirectOut());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
    public class RedirectIn {
    	public static void main(String[] args) {
    		try (FileInputStream in = new FileInputStream(
    				"src/inputandoutput/RedirectIn.java")) {
    			System.setIn(in);
    			Scanner scan = new Scanner(System.in);
    			// only use \n as Delimiter
    			scan.useDelimiter("\n");
    			while (scan.hasNext()) {
    				System.out.println("content:" + scan.next());
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }

4.进程通信

Runtime对象的exec()方法可以运行平台上其它程序,该方法产生一个Process()对象代表子进程,Process类中就提供了进程通信的方法。

    public class ReadFromTest {
    	public static void main(String[] args) throws IOException {
    		Process p = Runtime.getRuntime().exec("javac");
    		try (BufferedReader br = new BufferedReader(new InputStreamReader(
    				p.getErrorStream()))) {
    			String buff = null;
    			while ((buff = br.readLine()) != null) {
    				System.out.println(buff);
    			}
    		}
    	}
    }

上述代码获取了javac进程的错误流,进行了打印。在下列代码中可以在Java程序中启动Java虚拟机运行另一个java程序,并向另一个程序中输入数据。

    public class WriteToProcess {
    	public static void main(String[] args) throws IOException {
    		Process p = Runtime.getRuntime().exec("java ReadStandard");
    		try (
    		// 以p进程的输出流创建PrintStream,该输出流对本进程为输出流,对p进程则为输入流
    		PrintStream ps = new PrintStream(p.getOutputStream())) {
    			ps.println("normal string");
    			ps.println(new WriteToProcess());
    		}
    	}
    }
    
    class ReadStandard {
    	public static void main(String[] args) throws FileNotFoundException {
    		try (Scanner scan = new Scanner(System.in);
    				PrintStream ps = new PrintStream(
    						new FileOutputStream("out.txt"))) {
    			scan.useDelimiter("\n");
    			while (scan.hasNext()) {
    				ps.println("KeyBoards input:" + scan.next());
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }

这篇文章就介绍到这里了。

“工欲善其事,必先利其器”。要想成为工作上的高手,面试时的题霸,独步江湖,就必须拿到一份"武林秘籍"。
在这里插入图片描述
我个人强推牛客网:找工作神器|大厂java面经汇总|超全笔试题库

推荐理由:
1.刷题题库,题目特别全面,刷爆笔试再也不担心
在这里插入图片描述
链接: 找工作神器|大厂java面经汇总|超全笔试题库
2.超全面试题、成体系、高质量,还有AI模拟面试黑科技
在这里插入图片描述
链接: 工作神器|大厂java面经汇总|超全笔试题库
3.超多面经,大厂面经很多
在这里插入图片描述
4.内推机会,大厂招聘特别多
在这里插入图片描述
链接: 找工作神器|大厂java面经汇总|超全笔试题库
5.大厂真题,直接拿到大厂真实题库,而且和许多大厂都有直接合作,题目通过率高有机会获得大厂内推资格。
在这里插入图片描述
链接: 找工作神器|大厂java面经汇总|超全笔试题库

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半旧518

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值