栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

文章目录
  • 1.转换流
  • 2.推回输入流
  • 3.标准输入、输出流
  • 4.进程通信

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面经汇总|超全笔试题库

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1027115.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号