• 【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();
        		}
        	}
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    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();
        		}
        	}
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    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();
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
        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();
        		}
        	}
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    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);
        			}
        		}
        	}
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    上述代码获取了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();
        		}
        	}
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

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

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

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

  • 相关阅读:
    理解ASP.NET Core - 限流(Rate Limiting)
    Java中Map转对象
    字符串思维题练习 DAY4(CF 1849 C , CF 518A , CF1321C , CF1527 C)
    Android源码分析 - Framework层的Binder(客户端篇)
    灵性图书馆:好书推荐-《新零极限》
    高效理解 FreeSql WhereDynamicFilter,深入了解设计初衷[.NET ORM]
    计算机毕业设计ssm图书馆自习室占座选座zg09h系统+程序+源码+lw+远程部署
    (四)Spring Security Oauth2.0 源码分析--客户端端鉴权(token校验)
    Module加载的详细说明-保证你有所收获
    Github 2024-04-24 C开源项目日报 Top9
  • 原文地址:https://blog.csdn.net/qq_41708993/article/details/126003493