InputStream 是所有输入流类的父类
OutputStream 是所有输出流类的父类
FileInputStream 文件输入流
FileOutputStream 文件输出流
FileOutputStream out = new FileOutputStream("1.txt"); out.write("hello word!".getBytes()); out.close(); FileInputStream in = new FileInputStream("1.txt"); byte[] b = new byte[1024]; System.out.println(new String(b,0,in.read(b))); in.close();
FileWriter 用来写入字符文件的便捷类
FileRader 用来读取字符文件的便捷类
FileWriter w = new FileWriter("1.txt"); w.write("www.sina.com"); w.close(); FileReader r = new FileReader("1.txt"); char[] c = new char[1024]; int len = r.read(c); r.close(); System.out.println(new String(c,0,len));
注意:
FileReader,FileWriter
用于读取和写入字符流。要读写原始字节流,请考虑使用 FileInputStream和FileOutputStream
。
RandomAccessFile 此类的实例支持对随机访问文件的读取和写入。随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组,意思就是可以操作文件指针。比如跳过多少个字节读取数据,指定读取数据开始的位置等等。(类似断点续传可以使用该类实现)
public class Employee { String name = null; int age = 0; // 为了使数据是等长的记录,就是每条记录是相等大小的,规定name长度为8,(age是int 固定4个字节不用管) public static final int LEN = 8; Employee(String name,int age){ if(name.length() > LEN){ name = name.substring(0,LEN);//大于8截取 }else{ while(name.length() < LEN){//小于8补空 name += "\u0000"; } } this.name = name; this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; public class RandomAccessFiles { public static void main(String[] args) { // TODO Auto-generated method stub Employee e1 = new Employee("张三", 12); Employee e2 = new Employee("李四", 13); Employee e3 = new Employee("赵武", 14); try { RandomAccessFile f = new RandomAccessFile(new File("1.txt"), "rw"); try { //写入数据 f.writeChars(e1.getName()); f.writeInt(e1.getAge()); f.writeChars(e2.getName()); f.writeInt(e2.getAge()); f.writeChars(e3.getName()); f.writeInt(e3.getAge()); f.close(); //读取数据 RandomAccessFile f2 = new RandomAccessFile(new File("1.txt"),"r"); f2.skipBytes(20);//跳过20个字节 String s = ""; for (int i = 0; i < 8; i++) { s += f2.readChar(); } System.out.println(s + ":" + f2.readInt()); s = ""; //文件指针设置为0(开始位置) f2.seek(0); for (int i = 0; i < 8; i++) { s+=f2.readChar(); } System.out.println(s + ":" + f2.readInt()); s= ""; f2.skipBytes(20);//跳过20个字节 for (int i = 0; i < 8; i++) { s+=f2.readChar(); } System.out.println(s + ":" + f2.readInt()); f2.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行结果:
李四:13
张三:12
赵武:14
PipedInputStream 和 PipedOutputStream 主要是实现管道流通信的类,多个线程之间传送流数据。
import java.io.IOException; import java.io.PipedInputStream; //管道接收 public class Receiver extends Thread { PipedInputStream in = new PipedInputStream(); public PipedInputStream getIn() { return in; } public void run(){ byte[] b = new byte[1024]; try { //读取 int len = in.read(b); System.out.println(new String(b,0,len)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
import java.io.*; //管道输出 public class Sender extends Thread { PipedOutputStream out = new PipedOutputStream(); public PipedOutputStream getOut() { return out; } public void run(){ String s = "hello word!"; try { //输出 out.write(s.getBytes()); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class Test { /** * @param args * @throws IOException * @throws InterruptedException */ public static void main(String[] args) throws IOException, InterruptedException { // TODO Auto-generated method stub Sender sender = new Sender(); Receiver receiver = new Receiver(); PipedOutputStream out = sender.getOut(); PipedInputStream in = receiver.getIn(); //连接管道 in.connect(out); receiver.start(); Thread.sleep(1000); sender.start(); } }
ByteArrayInputStream 和 ByteArrayOutputStream 能通过操作流的方法来操作字节数组,一般用来做数据缓存之用
下面是将一个字符串转换成大写的例子,使用了字节缓存区实现。
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Test { /** * @param args * @throws IOException * @throws InterruptedException */ public static void main(String[] args) throws IOException, InterruptedException { // TODO Auto-generated method stub /* Sender sender = new Sender(); Receiver receiver = new Receiver(); PipedOutputStream out = sender.getOut(); PipedInputStream in = receiver.getIn(); in.connect(out); receiver.start(); Thread.sleep(1000); sender.start();*/ ByteArrayInputStream in = new ByteArrayInputStream("abcdefg".getBytes()); ByteArrayOutputStream out = new ByteArrayOutputStream(); tranform(in,out); byte[] b = out.toByteArray(); System.out.println(new String(b)); } //转换大写 public static void tranform(InputStream in , OutputStream out){ try { int len = -1; while((len = in.read())!=-1){ char c = Character.toUpperCase((char)len); out.write(c); } out.close(); } catch (Exception e) { // TODO: handle exception } } }
StringWriter 和 StringReader 也是字符流的缓存区。和字节缓存区的区别是一个操作的是单个字符char的数组,一个是操作原始的字节数组