I/O 二:JAVA IO详解(流类)
I/O 2:JAVA IO详解(流类)
f
package com.test.Algorithm; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class FileTest10 { //上面提到读数据的步骤为:1,打开一个流。2,判断是否还有数据,并读取数据。3,关闭流。 public static void main(String[] args) throws IOException { //打开流 FileInputStream fis = new FileInputStream(new File("C:/优秀.txt")); byte[] buffer = new byte[200]; int length = 0; //判断是否有更多数据,当没有时,返回-1。 //上面定义了一个200长度的字节数据,如果test.txt中有500字节长度的数据。 //那么它会循环4次,lenght的长度分别是200,200,100,-1 while (-1 != (length = fis.read(buffer, 0, 200))) { String s = new String(buffer, 0, length); System.out.println(s); } fis.close(); } }
InputStream子类中FilterInputStream是过滤流,其它是节点流。
以下程序简单演示了FileOutputStream的用法
package com.test.Algorithm; import java.io.FileOutputStream; import java.io.IOException; public class FileTest9 { public static void main(String[] args) throws IOException { FileOutputStream os = new FileOutputStream("d:/test.txt"); String str = "hello world"; byte[] buffer = str.getBytes(); os.write(buffer); os.close(); } }
其中FileOutputStream有一个构造函数
FileOutputStream(File file, boolean append)
参数append表示内容是否追加
package com.test.Algorithm; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class FileTest9 { public static void main(String[] args) throws IOException { ByteArrayOutputStream f = new ByteArrayOutputStream(); String str = "hello world"; byte[] buffer = str.getBytes(); f.write(buffer); byte[] result = f.toByteArray(); for (int i = 0; i < result.length; i++) { System.out.println((char) result[i]); } OutputStream os = new FileOutputStream("text.txt"); f.writeTo(os); f.close(); os.close(); } }
package com.test.Algorithm; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class FileTest9 { public static void main(String[] args) throws IOException { DataOutputStream dos = new DataOutputStream(new BufferedOutputStream( new FileOutputStream("a.txt"))); byte b = 3; int i =12; char ch ='a'; float f = 3.3f; dos.writeByte(b); dos.writeInt(i); dos.writeChar(ch); dos.writeFloat(f); dos.close(); DataInputStream dis = new DataInputStream(new BufferedInputStream( new FileInputStream("a.txt"))); //读与写的顺序要保持一致 System.out.println(dis.readByte()); System.out.println(dis.readInt()); System.out.println(dis.readChar()); System.out.println(dis.readFloat()); dis.close(); } }
package com.test.Algorithm; import java.io.IOException; import java.io.InputStream; public class MyByteArrayInputStream extends InputStream{ protected byte[] data; protected int position = 0; public MyByteArrayInputStream(byte[] b){ this.data = b; } public int read() throws IOException { return (position < data.length) ? (data[position++]) : -1; } public static void main(String[] args) { byte[] b = new byte[16]; for (int i = 0; i < b.length; i++) { b[i] = (byte)i; } MyByteArrayInputStream mbais = new MyByteArrayInputStream(b); while(true){ try { int c = mbais.read(); if(c < 0){ break; } System.out.println(c + " "); } catch (IOException e) { e.printStackTrace(); } } } }
package com.test.Algorithm; import java.io.IOException; import java.io.InputStream; public class MyOwnStream extends InputStream{ protected byte[] data; protected int position = 0; protected int mark =0; public MyOwnStream(byte[] b){ this.data = b; } public int read() throws IOException { return (position < data.length) ? (data[position++]) : -1; } //返回仍可读取的字节长度 public int available() throws IOException { return data.length - position; } public void close() throws IOException { position = data.length; } //设置流中的标识位 public synchronized void mark(int readlimit) { this.mark = readlimit; } //将缓冲流复位到标识位置 public synchronized void reset() throws IOException { if(mark <0 || mark >= data.length){ throw new IOException("the position is not valid"); } position = mark; } public boolean markSupported() { return true; } public int read(byte[] b, int off, int len) throws IOException { if(this.position > data.length || len < 0){ return -1; } if((this.position + len) > data.length){ len = data.length - this.position; } //如果只读0个,就直接返回0 if(len == 0){ return 0; } System.arraycopy(data, position, b, off, len); position +=len; return len; } }
学IO最重要的是深刻理解装饰模式!