前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Java筑基】IO流基础之常见工具流和进程通信

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

作者头像
用户10127530
发布2022-10-26 18:43:06
2410
发布2022-10-26 18:43:06
举报
文章被收录于专栏:半旧的技术栈半旧的技术栈

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

文章目录

1.转换流

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

代码语言:javascript
复制
    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()所需数组的大小时才会去流中读取。

代码语言:javascript
复制
    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类中提供了重定向标准输入、输出的方法。

代码语言:javascript
复制
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();
		}
	}
}
代码语言:javascript
复制
    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类中就提供了进程通信的方法。

代码语言:javascript
复制
    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程序,并向另一个程序中输入数据。

代码语言:javascript
复制
    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面经汇总|超全笔试题库

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1.转换流
  • 2.推回输入流
  • 3.标准输入、输出流
  • 4.进程通信
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档