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(); } } }
这篇文章就介绍到这里了。