今天学习的内容是其它IO流类
一、RandomAccessFile类
虽然RandomAccessFile类直接继承自Object类,不是IO流体系中的一员,但是它支持对文件的随机读取与写入(源和目的地只能是文件)。从本质上来说,RandomAccessFile的工作方式类似于把DataOutputStream和DataInputStream结合起来使用,并且添加了一些新功能。其实RandomAccessFile对象内部维护了一个可扩展的byte数组,并通过文件指针读写数组中的元素,随机读写的原理就是通过RandomAccessFile类的seek()方法设置文件指针的位置。程序示例:
public class Test92 {
public static void main(String[] args) throws IOException {
writeTest();
readTest();
randomWriteTest();
}
private static void randomWriteTest() throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf1.txt", "rw");
raf.seek(1*8);//通过seek()方法设置文件指针的位置,可以随机写入文件
raf.write("hehe".getBytes());
raf.close();
}
private static void readTest() throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.txt", "r");
raf.seek(1*8);//通过seek()方法设置文件指针的位置,可以随机读取文件
byte[] buf = new byte[4];
raf.read(buf);// 先读四个字节
String s = new String(buf);
System.out.println(s);
int a = raf.readInt();
System.out.println(a);
System.out.println("当前文件指针的位置"+raf.getFilePointer());//16
raf.close();
}
private static void writeTest() throws FileNotFoundException, IOException {
//如果文件不存在则创建,如果文件存在则不创建(和File类的creatNewFile()方法类似)
RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
raf.write("你好".getBytes());// 写入了四个字节
raf.writeInt(97);// 写入了四个字节
raf.write("我好".getBytes());// 写入了四个字节
raf.writeInt(98);// 写入了四个字节
raf.close();
}
}
二、PipedOutputStream类与PipedInputStream类
PipedInputStream类与PipedOutputStream类也称为管道流,输入输出流可以直接进行连接,要结合线程使用。
程序示例:
public class Test93 {
public static void main(String[] args) throws IOException {
PipedInputStream input = new PipedInputStream();
PipedOutputStream output = new PipedOutputStream();
input.connect(output);// 将输入输出流直接进行连接
new Thread(new Input(input)).start();
new Thread(new Output(output)).start();
}
}
class Input implements Runnable {
private PipedInputStream in;
public Input(PipedInputStream in) {
super();
this.setIn(in);
}
public void run() {
try {
byte[] buf = new byte[1024];
int len = in.read(buf);
String s = new String(buf,0,len);
System.out.println(s);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public PipedInputStream getIn() {
return in;
}
public void setIn(PipedInputStream in) {
this.in = in;
}
}
class Output implements Runnable {
private PipedOutputStream out;
public Output(PipedOutputStream out) {
super();
this.setOut(out);
}
public void run() {
try {
Thread.sleep(5000);
out.write("管道流coming".getBytes());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public PipedOutputStream getOut() {
return out;
}
public void setOut(PipedOutputStream out) {
this.out = out;
}
}
三、DataOutputStream类与DataInputStream类
DataOutputStream类与DataInputStream类用来操作基本数据类型,属于装饰器类,它为字节流添加了操作基本数据类型的功能。程序示例:
public class Test94 {
public static void main(String[] args) throws IOException {
writeTest();
readTest();
}
private static void readTest() throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
System.out.println(dis.readUTF());
dis.close();
}
private static void writeTest() throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeUTF("你好");
dos.close();
}
}
四、操作数组或字符串的流
操作数组或字符串的流,它们的源和目的地都是内存(终于看到了
)。程序示例(以ByteArrayInputStream和ByteArrayOutputStream为例):

public class Test95 {
public static void main(String[] args){
ByteArrayInputStream bis = new ByteArrayInputStream("asfdasf".getBytes());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch =0;
while((ch = bis.read())!=-1){
bos.write(ch);
}
System.out.println(bos.toString());// asfdasf
}
}