Java-IO-字节流和字符流

InputStream和Reader

InputStream

  • int read():从输入流中读取单个字节,返回所对应的字节数据(字节数据可直接转换成int类型)。
  • int read(byte[] b):从输入流中最对读取b.length个字节数据,并将其存储在数组b中,返回实际读取的字节数。
  • int read(byte[] b, int off, int len):从off位置开始将最多len个字节数据读取并存储进字节数组b中。

Reader

  • int read():从输入流中读取单个字符,返回所对应的字符数据(字符数据可直接转换成int类型)。
  • int read(char[] cbuf):从输入流中最对读取b.length个字符数据,并将其存储在数组cbuf中,返回实际读取的字符数。
  • int read(char[] cbuf, int off, int len):从off位置开始将最多len个字符数据读取并存储进字符数组cbuf中。

InputStream和Reader都是抽象类,本身不能创建实例,他们分别有一个读取文件的输入流:FileInputStream和FileReader,它们都是节点流(会直接和指定文件关联)。

package filetest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class FileInputStreamTest {

    public static void main(String[] args) {

        try {
            //创建字节输入流
            FileInputStream fis=new FileInputStream("first.txt");
            //穿件一个长度为1024的字节数组
            byte[] bbuf=new byte[1024];
            //保存实际读取的字节数
            int hasRead=0;
            //使用循环来重复取字节
            while((hasRead=fis.read(bbuf))>0)
            {
                //将读取出来的字节数组转换成字符串输出
                System.out.println(new String(bbuf,0,hasRead));
            }
            fis.close(); //关闭文件输入流
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
package filetest;

import java.io.FileReader;
import java.io.IOException;

public class FileReadderTest {

    public static void main(String[] args) throws IOException {
        //创建字符输入流
        FileReader fr=new FileReader("E:\\Java-IO\\first.txt");
        //创建字符数组
        char[] cbuf =new char[512];
        //保存读取字符数
        int hasRead=0;
        //使用循环来读取字符
        while((hasRead=fr.read(cbuf))>0)
        {
            //将字符数组转换成字符串输出
            System.out.println(new String(cbuf,0,hasRead));
        }
        fr.close();

    }

}

OutputStream和Writer

三个方法:
  • void write(int c):将指定的字节/字符输出到输出流中。
  • void write(byte[]/char[] buf):将字节数组/字符数组中的数据输出到指定的输出流中。
  • void write(byte[]/char[] buf, int off, int len):将字节数组/字符数组从off开始到长度为len的字节/字符输出到输出流中。
Writer还包含两个方法:
  • void write(String str):将str字符串里包含的字符串输出到指定的输出流中。
  • void write(String str, int off, int len):将str字符串从off开始,到len个字符串输出到输出流中。
package filetest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class FileOutputStreamTest {

    public static void main(String[] args) {
        try {
            //创建字节输入流
            FileInputStream fis=new FileInputStream("E:\\Java-IO\\first.txt");
            //创建字节输出流
            FileOutputStream fos=new FileOutputStream("E:\\Java-IO\\second.txt");
            byte[] bbuf=new byte[32];
            int hasRead=0;
            //循环从输入流中取出数据
            while((hasRead=fis.read(bbuf))>0)
            {
                //每读取一次,就写入文件输出流
                fos.write(bbuf,0,hasRead);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
package filetest;

import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {

    public static void main(String[] args) {
        try {
            FileWriter fw=new FileWriter("E:\\Java-IO\\poem.txt");
            //Windows平台的换行符是"\r\n",linux平台使用"\n"
            fw.write("张三\r\n");
            fw.write("李四\r\n");
            fw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值