java基础汇总(四)

在这里我想回顾一下输入输出流的用法,由于在实际中没有怎么使用,理解只能从最基础的,等日后用到再具体补充。java中的数据流通过不同的分类有不同的分法,例如通过流向可以分为输入流、输出流,通过传输单位类型分为字符流与字节流。在这里以字符流与字节流区分分成四类:

字节流:

InputStream 是输入流,抽象类,是所有字节输入流的父类

OutputStream  是输出流,抽象类,是所有字节输出流的父类

字符流:

Reader 是输入流,抽象类,是所有输入字符流的父类

Writer  是输出流,抽象类,是所有输出字符流的父类

下面看一下他们的子类:(下图因为制作比较费时间,故网上查找的)







下面我们首先来看看这四个抽象类所包含的方法:


InputStream 抽象类

public abstract int read() throws IOException//读取一个字节,如果没有数据返回值为-1
public int read(byte[] b)throws IOException//向数组b中读入数据,返回值为读入的个数,如果输入流没有数据,返回-1
public int read(byte[] b, int off,  int len)//throws IOException //从输入流中最多取len长度的字节读入偏移量为off的b数组,必须保证off+len<=b.length
public long skip(long n) throws IOException//从输入流中跳过指定的字节,并返回跳过的字节数
public int available() throws IOException//返回输入流中可取得的字节数
public void close() throws IOException//关闭输入流
public void mark(int readlimit)//标记当前位置
public void reset() throws IOException//重置回标记处
public boolean markSupported()//是否支持mark,reset
别的用法还好理解点,所以测试了下mark的用法:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class streammarktest{
public static void main(String[] args){
try{
InputStream bis=new BufferedInputStream(new FileInputStream("E:\\as.txt"));
System.out.println((char)bis.read());
System.out.println((char)bis.read());
System.out.println((char)bis.read());
bis.mark(10);
System.out.println((char)bis.read());
bis.reset();
System.out.println((char)bis.read());
System.out.println((char)bis.read());
}catch(IOException e){
 e.printStackTrace();
}
}
}
将mark中的数字改了几次结果都一样,还是下面的结果:(在as.txt文件里存的数据是abcdefghijklmn)

a
b
c
d
d
e
OutputStream 抽象类

public abstract void write(int b) throws IOException
public void write(byte[] b)throws IOException//将数组b中的数据写到输出流中
public void write(byte[] b, int off, int len)throws IOException
public void flush()throws IOException//刷新缓冲区,但是OutputStream中的flush()方法没有任何操作
public void close()throws IOException
Reader 抽象类
对于Reader类来说,其方法跟InputStream类方法差不多,只是读取的是字符,另外其又有几个不一样的方法。

public boolean ready() throws IOException//返回值代表输入字符流是否准备好了
public int read()throws IOException
public abstract int read(char[] cbuf, int off,int len) throws IOException//与InputStream相比这个方法变为了抽象方法,都变为了字符数组
public int read(char[] cbuf) throws IOException//都变为了字符数组
public int read() throws IOException
public void close() throws IOException//关闭输入流
public void mark(int readlimit)//标记当前位置
public void reset() throws IOException//重置回标记处
public boolean markSupported()//是否支持mark,reset
Writer 抽象类
public void write(int c)  throws IOException
public void write(char[] cbuf)  throws IOException
public abstract void write(char[] cbuf, int off,int len) throws IOException
public void write(String str) throws IOException
public void write(String str, int off, int len) throws IOException
public Writer append(CharSequence csq) throws IOException
public Writer append(CharSequence csq, int start, int end) throws IOException
public Writer append(char c) throws IOException
public abstract void flush() throws IOException
public abstract void close() throws IOException


下面再让我们从大的方面来研究下字节流跟字符流。

InputStream :

(1)FIleInputStream流,FIleInputStream可以将文件作为数据源来读入数据 。

1 FileInputStream(File file) 
这通过打开一个到实际文件,命名在文件系统中的File对象文件的文件的连接创建一个FileInputStream。
2 FileInputStream(FileDescriptor fdObj) 
这通过使用文件描述符fdObj,它代表在文件系统中某个实际文件的现有连接创建一个FileInputStream。
3 FileInputStream(String name) 
这将创建一个FileInputStream通过打开一个到实际文件的连接,通过路径名名在文件系统命名的文件。


(2)ByteArrayInputStream可以将一个数组作为数据源读入数据

1 ByteArrayInputStream(byte[] buf) 
这将创建一个ByteArrayInputStream类,以便它使用的buf为缓冲区数组。
2 ByteArrayInputStream(byte[] buf, int offset, int length) 
这将创建一个ByteArrayInputStream类使用的buf为缓冲区数组。

(3)SequenceInputStream流将多个InputStream类型的数据流转换成一个InputStream。

构造方法:

1 SequenceInputStream(Enumeration<? extends InputStream> e) 
这将初始化通过记住参数,它必须是生产对象的运行时类型为InputStream枚举一个新创建的SequenceInputStream.
2 SequenceInputStream(InputStream s1, InputStream s2) 
这将初始化通过记住两个参数,这将是按顺序阅读,第一S1和S2,然后,提供从这个SequenceInputStream读取的字节一个新创建的SequenceInputStream。

FilterInputStream类也是InputStream的一个子类,它是一个抽象类,也是装饰器类的父类。上面这几个常用的类都是结合其子类来得到我们需要的单一结果流。

使用实例:

1.FileInputStream input = new FileInputStream("temp.dat");

2.  ByteArrayInputStream ba1 = new ByteArrayInputStream("12345".getBytes());
      

3.     ByteArrayInputStream ba1 = new ByteArrayInputStream("12345".getBytes());
        ByteArrayInputStream ba2 = new ByteArrayInputStream("67890".getBytes());
       SequenceInputStream stream = new SequenceInputStream(ba1, ba2)


Vector<FileInputStream> v = new Vector<FileInputStream>();  

  
        v.add(new FileInputStream("c:\\1.txt"));  
        v.add(new FileInputStream("c:\\2.txt"));  
        v.add(new FileInputStream("c:\\3.txt"));  
  
        Enumeration<FileInputStream> en = v.elements();  
  
        SequenceInputStream sis = new SequenceInputStream(en); 

OutputStream:

(1)FileOutputStream

1 FileOutputStream(File file) 
这将创建一个文件输出流写入到由指定的File对象表示文件。
2 FileOutputStream(File file, boolean append) 
这将创建一个文件输出流写入到由指定的File对象表示的文件。
3 FileOutputStream(FileDescriptor fdObj) 
这将创建一个输出文件流写入到指定的文件描述符,它代表了文件系统中的某个实际文件的现有连接。
4 FileOutputStream(String name) 
这将创建一个输出文件流写入到具有指定名称的文件。
5 FileOutputStream(String name, boolean append) 
这将创建一个输出文件流写入到具有指定名称的文件。

(2)ByteArrayOutputStream

1 ByteArrayOutputStream() 
这将创建一个新的字节数组输出流。
2 ByteArrayOutputStream(int size) 
这将创建一个新的字节数组输出流,具有缓冲容量指定的大小,以字节为单位。

使用实例:

1.FileOutputStream output =new FileOutputStream("temp.dat");

2.ByteArrayOutputStream用法


字节数组流:
ByteArrayOutputStream:    可以捕获内存缓冲区的数据,转换成字节数组。
ByteArrayoutputStream bout=new ByteArrayOutputStream();
bout.write(int a);  bout.write(int b);  bout.write(int c);
byte[] buf=bout.toByteArray();//获取内存缓冲中的数据
for(int i=0;i<=buf.length;i++)
{
  System.out.println(buf);
}
bout.close();
注:通过调用reset()方法可以重新定位。
ByteArrayInputStream: 可以将字节数组转化为输入流
ByteArrayInputStream bin=new ByteArrayInputStream(byte[] buf);
int data=0;
while( (b=bin.read())!=-1)
{
  System.out.println(b);
}
bin.close();


与DataOutputStream&DataInputStream联合使用:


ByteArrayOutputStream bout=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(bout);
String name="suntao";
int age=19;
dos.writeUTF(name);
dos.writeInt(age);
byte[] buf=bout.toByteArray();//获取内存缓冲区中的数据
dos.close();
bout.close();


ByteArrayInputStream bin=new ByteArrayInputStream(byte[] buf);
DataInputStream dis=new DataInputStream(bin);
String name=dis.readUTF();//从字节数组中读取
int age=dis.readInt();
dis.close();
bin.close();


装饰器模式类

平时用的比较多的是:

DataInputStream

DataOutputStream

BufferedInputStream

BufferedOutputStream


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值