IO常用流对象总结

来源:互联网 发布:java tensorflow 编辑:程序博客网 时间:2024/05/17 01:13

一.打印流(PrintWriter和PrintStream):

特点:可以将各种基本数据类型的数据都原样打印。

PrintWriter(字符打印流):

常用构造函数:

PrintWriter(File file)
使用指定文件创建不具有自动行刷新的新 PrintWriter。

PrintWriter(OutputStream out)
根据现有的 OutputStream 创建不带自动行刷新的新 PrintWriter。

PrintWriter(String fileName)
创建具有指定文件名称且不带自动行刷新的新 PrintWriter。

PrintWriter(Writer out)
创建不带自动行刷新的新 PrintWriter。

可以将字符打印进入任何字符输出流,包括System.out

PrintWriter(OutputStream out, boolean autoFlush)
通过现有的 OutputStream 创建新的 PrintWriter。

autoFlush - boolean变量;如果为 true,则printlnprintfformat方法将刷新输出缓冲区

 

PrintStream(字节打印流):

常用构造函数:

PrintStream(File file)
创建具有指定文件且不带自动行刷新的新打印流。

PrintStream(OutputStream out)
创建新的打印流。

PrintStream(String fileName)
创建具有指定文件名称且不带自动行刷新的新打印流。

二.文件的切割和合并(SequenceInputStream):

作用:大文件切割分批传输

示例代码:

/** * 文件的切割和合并 * @author 小苏 * */public class FileSplitDemo {static String path = "G://图片/95ff58976aa17a70e5200c7ae633cac0.jpg";static String[] str = new String[]{"G://图片/split/0.pnp","G://图片/split/1.pnp","G://图片/split/2.pnp"};/** * @param args */public static void main(String[] args) {//文件切割try {split(path);} catch (IOException e) {e.printStackTrace();}//文件合并try {merge(str);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}static void split(String path) throws IOException{int num = 0;FileInputStream fis = new FileInputStream(path);FileOutputStream fos = null;byte[] buff = new byte[1024*1024];int len;while((len = fis.read(buff)) != -1){fos = new FileOutputStream("G://图片/split/"+(num++)+".pnp");fos.write(buff, 0, len);}fos.close();fis.close();}static void merge(String[] str) throws IOException{Vector<FileInputStream> ve = new Vector<FileInputStream>();for(String s : str){ve.add(new FileInputStream(s));}Enumeration<FileInputStream> elements = ve.elements();SequenceInputStream sis = new SequenceInputStream(elements);FileOutputStream fos = new FileOutputStream("G://图片/split/合并文件.png");byte[] buff = new byte[1024*1024]; int len;while((len = sis.read(buff)) != -1){fos.write(buff, 0, len);}fos.close();sis.close();}}

三.对象的序列化(ObjectInputStream,ObjectOutputStream和Serializable(接口)):

1.  被序列化的对象必须实现Serializable接口

2. transient关键字:被transient修饰的关键字不会被序列化

3. 类的静态成员变量不会被序列化

4. serialVersionUID:类的标识符,判断某个对象是否是某个类的实例。是根据类的成员变量得出的一个值

ObjectInputStream常用方法摘要:

 Object

readObject()

从 ObjectInputStream 读取对象。

int

readInt()
          读取一个 32 位的 int 值。

 int

read()
          读取数据字节。

 int

read(byte[] buf, int off, int len)
          读入 byte 数组。

ObjectOutputStream常用方法摘要:

 void

writeObject(Object obj)
          将指定的对象写入 ObjectOutputStream。

 void

writeInt(int val)
          写入一个 32 位的 int 值。

 void

write(byte[] buf)
          写入一个 byte 数组。

 void

write(byte[] buf, int off, int len)
          写入字节的子数组。

 void

write(int val)
          写入一个字节。

 

四.    管道流(PipedInputStream和PipedOutputStream):

特点:输入输出可以直接进行连接,通过结合线程使用

示例代码:
/** * 管道流示例代码 * @author 小苏 * */public class Test12 {public static void main(String[] args) {try {PipedOutputStream pos = new PipedOutputStream();PipedInputStream pis = new PipedInputStream(pos);new Thread(new Reader(pis)).start();new Thread(new Writer(pos)).start();} catch (IOException e) {e.printStackTrace();}}}class Reader implements Runnable{private PipedInputStream pis;public Reader(PipedInputStream pis) {this.pis = pis;}@Overridepublic void run() {try {StringBuffer sb = new StringBuffer();byte[] buff = new byte[1024];int len;while((len = pis.read(buff))!= -1){sb.append(new String(buff,0,len));}pis.close();System.out.println(sb.toString());} catch (IOException e) {e.printStackTrace();}}}class Writer implements Runnable{private PipedOutputStream pos;public Writer(PipedOutputStream pos) {this.pos = pos;}@Overridepublic void run() {try {byte[] buff = "hello world".getBytes();pos.write(buff);pos.flush();pos.close();} catch (IOException e) {e.printStackTrace();}}}

五. RandomAccessFile:

概述:不算是IO体系的子类,直接继承Object;但它是IO的一员,因为它具备读写操作。

 

特点:能指定指针位置对文件进行读写或者修改;如果操作模式为rw,操作的文件不存在则会自动创建,如果存在不会覆盖。可以实现多线程操作同一个文件,多线程下载

 

原理:内部封装了一个数组,通过指针对数组元素进行操作,可以通过

getFilePointer()来获取指针的位置,同时可以通过seek()来改变指针的位置(内部封装了字节输入和输出流)

 

注意:该类只能操作文件,而且还是带着模式操作

操作模式

含意

"r"

以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException

"rw"

打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。

"rws"

打开以便读取和写入,对于 "rw",还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备。

"rwd"  

打开以便读取和写入,对于 "rw",还要求对文件内容的每个更新都同步写入到底层存储设备。

常用方法摘要:

 long

length()
          返回此文件的长度。

 void

seek(long pos)
设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。

 int

skipBytes(int n)
尝试跳过输入的 n 个字节以丢弃跳过的字节。只能向前跳过,不能后退

l六.   操作基本数据类型(DataInputStream,DataOutputStream):

特点:直接操作基本数据类型,凡是操作基本数据类型就用这个流

七,操作字节数组(内存流)(ByteArrayInputStream,ByteArrayOutputStream):

   ByteArrayOutputStream:此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用toByteArray()toString() 获取数据。 关闭 ByteArrayOutputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何IOException

    ByteArrayInputStream:ByteArrayInputStream 包含一个内部缓冲区,该缓冲区包含从流中读取的字节。内部计数器跟踪read 方法要提供的下一个字节。 关闭 ByteArrayInputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何IOException

可见两个流都不需要close()操作

用流的读写来操作数组



0 0
原创粉丝点击