一、两大抽象及其子类体系
1)InputStream (字节输入流)
2)OutputStream(字节输出流)
二、两大文件流(重点掌握)
1)FileInputStream (读文件中数据)
a)read();
返回读到字节,假如是-1表示已经读到文件尾
b)read(byte[] buf);
将字节读到buf数组,一次最多读buf.length个字节,返回值为读到的字节数
c)read(byte[],int,int
2)FileOutputStream(向文件写数据)
a)write(bytedata);
一次只能写一个字节
b)write(byte[]buf);
一次最多写buf.length个字节
c)write(byte[],intoffset,int length);
将buf数组中的字节,从offset位置开始写,写length个
static void writeData(String data) throws IOException{ OutputStream out=null; //1.构建输出流对象 out=new FileOutputStream("data1.txt"); //out=new FileOutputStream("data1.txt",true);true表示追加 //2.写数据 out.write(data.getBytes());//byte[] //3.释放资源 out.close(); } static void readData() throws IOException{
//1.构建FileInputStream流对象 InputStream in=new FileInputStream("data1.txt"); //2.读数据 while(true){ int n=in.read(); if(n==-1)break; System.out.println((char)n); } //3.释放资源 in.close(); } |
三、一个打印流(PrintStream)(了解)
a)print()
b)println(...);自动添加换行
PrintStream ps=new PrintStream(new FileOutputStream("m1.txt"));标准输出流 ps.println("helloworld"); ps.println("tarena"); ps.close(); |
四、两大缓冲流(重点掌握,对象内置缓冲区)
1)BufferedInputStream
2)BufferedOutputStream
说明:使用BufferedOutputStream写数据时,最后最好记得要刷新(flush)。
高效复制文件:
static void copy03(File src,File dest)throws IOException{ //1.构建流对象 InputStream in =new BufferedInputStream(new FileInputStream(src)); OutputStream out =new BufferedOutputStream(new FileOutputStream(dest)); //2.读写数据 int len=-1; byte buf[]=new byte[1024];// long start=System.nanoTime(); while((len=in.read(buf))!=-1){ out.write(buf,0,len); } out.flush();记得要刷新(flush) //3.释放资源 in.close(); out.close(); } |
五、两大字节数组流(重点掌握)
1)ByteArrayInputStream(byte buf[]);
2)ByteArrayOutputStream();(内置了一个可扩容的数组)
注意:
1)在内存中临时存储多个字节时时可以考虑使用
2)两个方法:可使用 toByteArray()
和 toString()
获取数据。
byte []buf="helloworld".getBytes(); ByteArrayInputStream in==new ByteArrayInputStream(buf); int n1=in.read();//每次读一个字节 int n2=in.read(); System.out.println((char)n1); System.out.println(n2); in.close(); |
六、两大数据流(掌握,读写基本数据类型+字符串)
1)DataInputStream
a)readInt()
b)readChar()
c)readUTF()
d)....
2)DataOutputStream
a)writeInt()
b)writeChar()
c)writeUTF();
ByteArrayOutputStream bos=new ByteArrayOutputStream(); DataOutputStream out=new DataOutputStream(bos); out.writeInt(100); out.writeInt(200); out.writeChar('达'); out.writeUTF("helloworld");写字符串必须用这个,带编码的 out.close(); //========================= DataInputStream in=new DataInputStream(newByteArrayInputStream(bos.toByteArray())); int n1=in.readInt(); int n2=in.readInt(); char c=in.readChar(); String s=in.readUTF();读字符串必须用这个,带编码的 in.close(); System.out.println(n1+"/"+n2); System.out.println(c); System.out.println(s); |
七、两大对象流(掌握,读写对象)
1)ObjectInputStream
a)readObject()
2)ObjectOutputStream
a)writeObject(..)
注意:
1)对象流主要用于对对象进行序列化或反序列化
2)留意transient修饰符的作用(使用此修饰符修饰的属性不会被序列化)
class Student implements Serializable{ private static final long serialVersionUID = -2135079723014730959L;仅仅是个标志而已 String name; /**transient修饰的属性不会被序列化*/ transient int age; Student(String name,int age){ this.name=name; this.age=age; } @Override public String toString() { return name+"/"+age; } } public class ObjectStreamDemo01 { public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("o1.txt")); out.writeObject("helloworld"); out.writeObject(new Student("红武",12)); out.close();
ObjectInputStream in=new ObjectInputStream(new FileInputStream("o1.txt")); Object obj1=in.readObject(); Object obj2=in.readObject(); System.out.println(obj1); System.out.println(obj2); in.close(); } |