简介:字节流和字符流操作都一样

InputStream和Reader

简介:InputStream和Reader是所有输入流的抽象基类。它们的方法是所有输入流都可使用的方法。能创建实例来执行输入

  1. int read():读取单个字节,返回所读取的字节数据(字节数据转换为int类型返回)。
  2. int read(byte[]b):最多读取b.length个字节的数据,返回实际读取的字节数。
  3. int read(byte[]b,int off,int len):从off开始,长度为len

###直到read(char[] cbuf)或read(byte[]b)方法返回−1,表示结束
###ava 7改写了所有的IO资源类,都实现了AutoCloseable接口,因此都可通过自动关闭资源的try语句来关闭流

  1. public static void main(String[] args) {
  2. //文件位置
  3. File file = new File("D:\\Charles\\Desktop\\apple.txt");
  4. try {
  5. //文件输入流
  6. FileInputStream fileInputStream = new FileInputStream(file);
  7. //一次读取一个byte数组
  8. byte[] bytes = new byte[1024];
  9. int hasRead = 0;
  10. //一次读取一个字节数组,没满则继续
  11. while ((hasRead = fileInputStream.read(bytes))>0){
  12. //转换成字符串
  13. System.out.println(new String(bytes,0,hasRead));
  14. }
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }
  • void mark(int readAheadLimit):在记录指针当前位置记录一个标记(mark)。
  • boolean markSupported():判断此输入流是否支持mark()操作,即是否支持记录标记。
  • void reset():将此流的记录指针重新定位到上一次记录标记(mark)的位置。
  • long skip(long n):记录指针向前移动n个字节/字符。

OutputStream和Writer

  1. void write(int/char c):将指定的字节/字符输出到输出流中,c既可以代表字节,也可以代表字符。
  2. **void write(byte/char[]buf)**:将字节数组/字符数组中的数据输出到指定输出流中。
  3. void write(byte/char[]buf,int off,int len)

###因为字符流直接以字符作为操作单位,所以Writer可以用字符串来代替字符数组

  1. void write(String str)
  2. void write(String str,int off,int len)
    1. public static void main(String[] args) {
    2. File file = new File("D:\\Charles\\Desktop\\apple.txt");
    3. try {
    4. FileReader fileReader = new FileReader(file);
    5. FileWriter fileWriter = new FileWriter("D:\\Charles\\Desktop\\app.txt");
    6. char[] chars = new char[32];
    7. int hasRead = 0;
    8. while ((hasRead = fileReader.read(chars))>0){
    9. //把输入流读取到的内容给输出流
    10. fileWriter.write(chars,0,hasRead);
    11. //字符流需要调用
    12. fileWriter.flush();
    13. }
    14. } catch (IOException e) {
    15. e.printStackTrace();
    16. }
    17. }

注意:

  1. 字符输入流,一次输入不满字符数组,会从此次开始位置往前截取足够字符
  2. 新一次输出流会覆盖旧文件的内容
  3. 字符流需要调用**fulsh()**,字节流则不用
  4. “\n”、”\r”都可以换行

处理流

简介:进行输入/输出操作更简单;执行效率更高。通过处理流来包装节点流,隐藏底层设备上节点流的差异。

  1. 关闭最上层的处理流时,系统会自动关闭被该处理流包装的节点流。

image.png


Printxx:输出文本内容

printStream、PrintWriter

  1. import java.io.*;
  2. public class InputAndOutputStream {
  3. public static void main(String[] args) {
  4. File file = new File("D:\\Charles\\Desktop\\apple.txt");
  5. try {
  6. FileOutputStream fileOutputStream = new FileOutputStream(file);
  7. PrintStream printStream = new PrintStream(fileOutputStream);
  8. printStream.println("床前明月光");
  9. printStream.println("疑是地上霜");
  10. printStream.println("举头望明月");
  11. printStream.println("低头思故乡");
  12. printStream.println(new InputAndOutputStream());
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }

Stringxx:字符串处理流

  • 处理节点是字符串
    1. public class InputAndOutputStream {
    2. public static void main(String[] args) {
    3. File file = new File("D:\\Charles\\Desktop\\apple.txt");
    4. try {
    5. String src = " 静夜思\n"+"床前明月光\n"+"疑是地上霜\n"
    6. +"举头望明月\n"+"低头思故乡";
    7. StringReader stringReader = new StringReader(src);
    8. //控制台输出
    9. StringWriter stringWriter = new StringWriter();
    10. stringWriter.append(src,0,src.length());
    11. System.out.println(stringWriter.toString());
    12. char[] chars = new char[32];
    13. int hasReader = 0;
    14. while((hasReader = stringReader.read(chars))>0){
    15. System.out.println(new String(chars,0,hasReader));
    16. }
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. }
    20. }
    21. }

Filterxx:过滤流


Pipedxx:管道流

  • 它们都是用于实现进程之间通信功能

Bufferedxx:缓冲流

  • 增加了缓冲功能,增加缓冲功能可以提高输入、输出的效率
  • 需要使用flush()才可以将缓冲区的内容写入实际的物理节点

XXStreamReader:转换流

InputStreamReader、OutputStreamWriter:
**System.in**代表标准输入(键盘输入)
System.exit(int status):终止当前正在运行的Java虚拟机

  • sataus:0-正常退出,1-强制结束
  • 不管status为何值程序都会退出,return是回到上一层,而System.exit(status)是回到最上层 ```java public class InputAndOutputStream { public static void main(String[] args) {
    1. //键盘输入内容都是文本内容,所以可以使用InputStreamReader将其转换成字符输入流
    2. InputStreamReader inputStreamReader = new InputStreamReader(System.in);
    3. //可以将普通的Reader再次包装成BufferedReader,
    4. //利用BufferedReader的readLine()方法可以一次读取一行内容
    5. BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    6. String buffer = null;
    7. try {
    8. while ((buffer = bufferedReader.readLine())!=null){
    9. System.out.println(buffer);
    10. if (buffer.equals("exit"))
    11. System.exit(0);
    12. }
    13. } catch (IOException e) {
    14. e.printStackTrace();
    15. }
    } }
  1. ---
  2. <a name="FA3tW"></a>
  3. ## Pushbackxx:推回输入流
  4. **PushbackInputStreamPushbackReader:**
  5. 1. `void unread(int b)`:将一个字节/字符推回到推回缓冲区里,从而允许重复读取刚刚读取的内容。
  6. 1. `void unread(byte[]/char[] buf)`
  7. 1. `void unread(byte[]/char[]b,int off,int len)`
  8. **简介**:当程序调用这两个推回输入流的unread()方法时,系统将会把指定数组的内容**推回到该缓冲区**里,而推回输入流每次调用read()方法时总是**先从推回缓冲区读取**,只有完全读取了推回缓冲区的内容后,但还没**有装满read()所需的数组时才会从原输入流中读取**
  9. - 默认的推回缓冲区的长度为1
  10. - 如果程序中推回到推回缓冲区的内容超出了推回缓冲区的大小,Pushback buffer overflowIOException异常
  11. ```java
  12. public class InputAndOutputStream {
  13. public static void main(String[] args) {
  14. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  15. PushbackReader pushbackReader = new PushbackReader(bufferedReader,64);
  16. String src ;
  17. try {
  18. while ((src = bufferedReader.readLine()) != null){
  19. if (src.contains("Java")){
  20. System.out.println("###########");
  21. pushbackReader.unread(src.toCharArray());
  22. }
  23. if (src.equals("exit")){
  24. System.exit(1);
  25. }
  26. System.out.println(src);
  27. }
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }

重定向标准输入/输出

简介:Java的标准输入/输出分别通过System.in和System.out来代表,在默认情况下它们分别代表键盘和显示器,当程序通过System.in来获取输入时,实际上是从键盘读取输入;当程序试图通过System.out执行输出时,程序总是输出到屏幕。

  1. static void setErr(PrintStream err):重定向“标准”错误输出流。
  2. static void setIn(InputStream in):重定向“标准”输入流。
  3. static void setOut(PrintStream out):重定向“标准”输出流。 ```java public class ReSystem { public static void main(String[] args) {
    1. try {
    2. PrintStream printStream = new PrintStream("D:\\Charles\\Desktop\\输入文件.txt");
    3. //系统的标准输出重定向到该Print Stream输出流,而不控制台
    4. System.setOut(printStream);
    5. System.out.println("Hello World!!!");
    6. } catch (FileNotFoundException e) {
    7. e.printStackTrace();
    8. }
    } }
  1. ```java
  2. public class ReSystem {
  3. public static void main(String[] args) {
  4. try {
  5. FileInputStream fileInputStream = new FileInputStream("D:\\Charles\\Desktop\\输入文件.txt");
  6. PrintStream printStream = new PrintStream("D:\\Charles\\Desktop\\文件.txt");
  7. System.setIn(fileInputStream);
  8. Scanner scanner = new Scanner(System.in);
  9. //设置换行符
  10. scanner.useDelimiter("/n");
  11. while (scanner.hasNext()){
  12. System.out.println(scanner.next());
  13. }
  14. } catch (FileNotFoundException e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. }

注意:标准输出流空格视为换行——scanner.useDelimiter("/n");


Java虚拟机读取其他进程的数据

简介:使用Runtime对象的exec()方法可以运行平台上的其他程序,该方法产生一个Process对象,Process对象代表由该Java程序启动的子进程。Process类提供了如下3个方法,用于让程序和其子进程进行通信。

  1. InputStream getErrorStream():获取子进程的错误流。
  2. InputStream getInputStream():获取子进程的输入流。
  3. OutputStream getOutputStream():获取子进程的输出流。
    1. public class exec {
    2. public static void main(String[] args) {
    3. try {
    4. Process process = Runtime.getRuntime().exec("java");
    5. BufferedReader bufferedReader = new BufferedReader(
    6. new InputStreamReader(process.getErrorStream(), Charset.forName("GBK")));
    7. String str;
    8. while ((str = bufferedReader.readLine())!=null){
    9. System.out.println(str);
    10. }
    11. } catch (IOException e) {
    12. e.printStackTrace();
    13. }
    14. }
    15. }