Java学习笔记之输入流输出流的个人理解及总结

以下是博主学java的时候记的一些笔记,分享给大家,如果有错误或者以为的话,可以在下方留言

输入流

 

字节输入流(FileInputStream):

字节输入流主要用于读取文件中的数据的时候用,比较适合读取字符之类的时候用,但是没怎么有效率,不太适合用于读取汉字。

构造方法: FileInputStream(String name/File file);

: FileInputStream file = new FileInputStream(aaa.txt);

读出数据方法: int read();

              int read(byte b[]);

int read(byte b[],int off,int len);

byte b[]的方式读取文件,将读取到的字节存放到字节数组b[]中。

字节输入流中read()方法读取到文件末尾时返回-1

 

字符输入流(FileReader):

字符输入流也用于读取数据,适合于读取汉字之类的数据,效率也不是特别高。

构造方法: FileReader(String name/File file);

: FileReader fr = new FileReader(hahah.txt);

读取数据的方法: int read();

  int read(char b[]);

                int read(char b[],int off,int len);

char b[]的方式读取文件,将读取到的字符存放到字符数组b[]中。

字符输入流中read()方法读取到文件的末尾时返回-1

 

缓冲输入流(BufferedReader):

缓冲输入流增强了读取文件的能力,适用于按行读取文件。

构造方法: BufferedReader(Reader in);

: FileReader fr = new FileReader(haha.txt);

BufferedReader br = new BufferedReader(fr);

读取数据的方法: public String readLine();

: String s = br.readerLine();

读取文件的一行,赋给字符串s

String s的方式读取文件,将读取到的字符串放到字符串s中。

缓冲输入流中read()方法读取到文件的末尾是返回null

 

数据输入流(DataInputStream):

数据输入流的好处在于读取数值时,不必再关心这个数值应当是多少个字节。

构造方法: DataInputStream(InputStream in);

: FileInputStream fis = new FileInputStream(haha.txt);

DataInputStream dis = new DataInputStream(fis);

读取文件的方法:

 

 

 

对象输入流(ObjectInputStream):

对象输入流是对一个类进行读取的操作,所以,所操作的类必须实现Serializable接口。

构造方法: ObjectInputStream(InputStream in);

: FileInputStream fis = new FileInputStream(lala.txt);

     ObjectInputStream ois = new ObjectInputStream(fis);

 读取文件的方法:readObject(Object obj);该方法返回一个读入时的类

: Students li = (Students)ois.readObject();

 

 

 

输出流

 

字节输出流(FileOutputStream):

构造方法: FileOutputStream(String name/File file);

: FileOutputStream fos = new FileOutputStream(sss.txt);

读入文件方法: public void write(byte b[]);

public void write(byte b[],int off,int len);

功能:b[]个字节到文件。

 

字符输出流(FileWriter):

构造方法: FileWriter(String name/File file);

: FileWriter fw = new FileWriter(haah.txt);

读入文件方法: public void write(int n);

           public void Write(char b[]);

           public void Write(char b[],int off,int len);

功能:b[]个字符到文件。

 

缓冲输出流(BufferedWriter):

构造方法: BufferedWriter(Writer out);

: FileWriter fw = new FileWriter(hahahah.txt);

BufferedWriter bw = new BufferedWriter(fw);

读入文件的方法: public void Write(String s);

                public void Write(String s,int off,int len);

功能: 写字符串写到文件。

换行方法: newLine();

 

数据输出流(DataOutputStream):

构造方法: DataOutputStream(OutputStream out);

: FileOutputStream fos = new FileOutputStream(lala.txt);

DataOutputStream dos = new DataOutputStream(fos);

读入文件的方法:

 

 

 

对象输出流(ObjectOutputStream):

构造方法: ObjectOutputStream(OutputStream out);

: FileOutputStream fos = new FileOutputStream(liji.txt);

ObjectOutputStream oos = new ObjectOutputStream(fos);

读入文件的方法:WriteObject(object obj);

 

随机流(RandomAccessFile)

RandomAccessFile类既是输入流,又是输出流。

构造方法: RandomAccessFile(String name,String mode);

           RandomAccessFile(File file,String mode);

mode的值只能取rwr

RandomAccessFile r = new RandomAccessFile(tom.txt,rw/r);

seek(long a): 用来定位读/写位置。其中,参数a用来确定读/写位置距离文件开头的字节个数.

getFilePointer(): 获取当前的读/写位置。

注意:RandomAccessFile流的readLine()方法在读取非ASCII字符的文件时(比如汉字的文件)会出现乱码现象,需要把readLine()读取的字符串用ISO-8859-1重新编码存放在byte数组中。

方法:(1) 读取字符串。

         String str = in.readLine();

     (2) ISO-8859-1重新编码。

         byte b[] = str.getBytes(ISO-8859-1);

     (3) 使用当前计算机默认编码将字节数组转化为字符串。

         String s = new String(b);

/写文件的方法:

 

 

 

数组流

字节数组输入流(ByteArrayInputStream):

构造方法: ByteArrayInputStream(byte []buf);

          ByteArrayInputStream(byte []buf,int off,int len);

:ByteArrayInputStream bis = new ByteArrayInputStream(b[]);

读取缓冲区的方法: public int read();

                   public int read(byte []b,int off,int len);

byte buf[]的方式读取文件,将读取到的文件存放到字节数组buf[]中。

当读到文件的末尾时,返回-1

 

 

字节数组输出流(ByteArrayOutputStream):

构造方法: ByteArrayOutputStream();

          ByteArrayOutputStream(int size);

:ByteArrayOutputStream b = new ByteArrayOutputStream();

读入缓冲区的方法: public void write(int b);

                   public void write(byte b[],int off,int len);

 

public byte[] toByteArray();该方法返回输出流写入缓冲区的所示字节。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值