处理流类型:
1、处理流之首先缓冲流:
解释:
例子:TestBufferStream1.java
package com.zhj.www;import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;public class TestBufferStream1 {public static void main(String[] args) {try {FileInputStream fileInputStream = new FileInputStream("E:\\JavaCode\\eclipse-workspace\\TestFileWriter\\src\\com\\zhj\\www\\Hello.dat");//从某个文件开始读入BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);int c = 0;System.out.println(fileInputStream.read());System.out.println(fileInputStream.read());fileInputStream.mark(100);for(int i = 0;i< 10 && (c = bufferedInputStream.read()) != -1;i++) {System.out.println(c + " ");}System.out.println();fileInputStream.reset();for(int i = 0;i< 10 && (c = bufferedInputStream.read()) != -1;i++) {System.out.println(c + " ");}fileInputStream.close();} catch (IOException e) {e.getStackTrace();}}
}
运行结果:
?
什么是流,就是管道,在一个文件中插了一条管道,可以读入,也可以写入。节点流也可以这么理解。
处理流呢?在节点流的基础上,外面又包了一层管道。
例子:TestBufferStream2.java
package com.zhj.www;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TestBufferStream2 {public static void main(String[] args) {try {BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("E:\\JavaCode\\eclipse-workspace\\TestBufferStream2\\src\\com\\zhj\\www\\HelloWorld.txt"));BufferedReader bufferedReader = new BufferedReader(new FileReader("E:\\JavaCode\\eclipse-workspace\\TestBufferStream2\\src\\com\\zhj\\www\\HelloWorld.txt"));String string = null;for(int i = 1;i<=100;i++) {string= String.valueOf(Math.random());bufferedWriter.write(string);bufferedWriter.newLine();}bufferedWriter.flush();while ((string = bufferedReader.readLine()) != null) {System.out.println(string);}bufferedWriter.close();bufferedReader.close();} catch (IOException e) {e.printStackTrace();}}
}
2、处理流之转换流:
举个例子:(TestTransForm1.java)
package com.zhj.www;
import java.io.*;
public class TestTransForm1 {public static void main(String[] args) {try {//转换流:OuputStream转化为Writer;OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("E:\\JavaCode\\eclipse-workspace\\TestTransForm1\\src\\com\\zhj\\www\\hello.txt"));outputStreamWriter.write("Hello_World! ");System.out.println(outputStreamWriter.getEncoding());//输出编码方式outputStreamWriter.close();outputStreamWriter =new OutputStreamWriter(new FileOutputStream("E:\\JavaCode\\eclipse-workspace\\TestTransForm1\\src\\com\\zhj\\www\\hello.txt", true),"ISO8859_1");outputStreamWriter.write("Heihei!");System.out.println(outputStreamWriter.getEncoding());outputStreamWriter.close();} catch (Exception e) {e.getStackTrace();}}
}
分析这个例子:
再举个例子:(TestTransForm2.java)
package com.zhj.www;import java.io.*;public class TestTransForm2 {public static void main(String[] args) {InputStreamReader inputStreamReader = new InputStreamReader(System.in);BufferedReader bufferedInputStream = new BufferedReader(inputStreamReader);String string= null;try {string =bufferedInputStream.readLine();while (string!= null) {if (string.equalsIgnoreCase("exit")) {break;}System.out.println(string.toUpperCase());string = bufferedInputStream.readLine();}bufferedInputStream.close();} catch (IOException e) {e.getStackTrace();}}}
分析这个例子:
3、处理流之数据流:
直接上例子:(TestDataStream.java)
package com.zhj.www;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class TestDataStream {public static void main(String[] args) {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);try {dataOutputStream.writeDouble(Math.random());dataOutputStream.writeBoolean(true);ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());System.out.println(byteArrayInputStream.available());//9DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);System.out.println(dataInputStream.readDouble());System.out.println(dataInputStream.readBoolean());dataOutputStream.close();dataInputStream.close();} catch (IOException e) {e.getStackTrace();}}
}
//Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值,是Java语言常用代码
简单流程分析:
4、处理流之Print流:
举个例子:TestPrintStream1.java
分析:
代码意思:就是将从键盘输入的数据,保存到log.dat;然后再从log.data输出到命令行。
再举个例子:TestPrintStream2.java
简单分析:
再在举个例子:TestPrintStream3.java
简单分析:
代码的意思就是:把从键盘输入的内容保存到日志文件中,只要从键盘输入的不为null,就继续把从键盘输入的信息保存到指定的日志文件中,但当输入的等于exit,就停止往日志中写。
5、处理流之Object流: