IO流 笔记1(仅供自己参考)

IO流 笔记1(仅供自己参考)

输入流

BufferReader br = new BufferedReader();
//构造方法:
public BufferedReader(Reader in) {
        this(in, defaultCharBufferSize);
    }

从BufferedReader的方法中得知,需要传入一个reader,所以需要new一个InputStreamReader

InputStreamReader isr = new InputStreamReader();
//构造方法:
public InputStreamReader(InputStream in, String charsetName)
        throws UnsupportedEncodingException
    {
        super(in);
        if (charsetName == null)
            throw new NullPointerException("charsetName");
        sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
    }

从此构造方法得知,需要new一个InputStrem,可以通过FileInputStream创建

FileInputStream fis = new FileInputStream();
//构造方法:
public FileInputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null);

即输入流需要的前置条件为:

BufferedReader br = null;
InputStreamReader isr = null;
FileInputStream fis = null;

fis = new FileInputStream("D:/iofile.txt");
isr = new InputStreamReader(fis,"UTF8");
br = new BufferedReader(isr);

后面三段代码会报错,需要try/catch

try{
	fis = new FileInputStream("D:/iofile.txt");
	//""字符串为需要输入读取的路径,
	isr = new InputStreamReader(fis,"UTF8");
	//"UTF8"表示的是设置编码格式为 "UTF8"
	br = new BufferedReader(isr);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

显示出读取到的字符:

String content = null;
while ((content = br.readLine()) != null){
	System.out.println(content);
	}

输出流

输出流的前置代码跟输入流差不多:

BufferedWriter bw = null;
OutputStreamWriter osw = null;
FileOutputStream fos = null;

try {
	fos = new FileOutputStream("D:/io2.txt",true);
	//append:true的意思是再执行的操作是在原有的内容后面继续添加. append:false是默认的,默认覆盖掉原有内容
	osw = new OutputStreamWriter(fos,"UTF8");
	bw = new BufferedWriter(osw);
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}

输出流得到的值需要转换成StringBuffer类型,通过字符串可以进行很多操作。

StringBuffer sb = new StringBuffer();
String content = null;
while ((content = br.readLine()) != null){
	sb.append(content);//讲读取到的内容添加到sb内存中
}
String content1 = sb.toString();
//将sb内存中的内容存入content1字符串中
content1 = content1.replace("hello","not hello");
//将content中的hello替换成 not hello
bw.write(content1);//创建一个新的字符流,将content1传给bw
bw.flush();//强制将缓存区中的数据写入到输出流中
System.out.println(sb);

最后需要将输入输出流都关闭

finally{
    try {
        br.close();
        isr.close();
        fis.close();
        fos.close();
        osw.close();
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

close方法也需要try/catch抛出

完整代码

public static void main(String[] args) {
        BufferedReader br = null;
        InputStreamReader isr = null;
        FileInputStream fis = null;

        BufferedWriter bw = null;
        OutputStreamWriter osw = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream("D:/iofile.txt");
            isr = new InputStreamReader(fis,"UTF8");
            br = new BufferedReader(isr);

            fos = new FileOutputStream("D:/io2.txt",true);
            osw = new OutputStreamWriter(fos,"UTF8");
            bw = new BufferedWriter(osw);
            //以上都能记住了

            StringBuffer sb = new StringBuffer();
            String content = null;
            while ((content = br.readLine()) != null){
//                System.out.println(content);
                sb.append(content);
            }

            String content1 = sb.toString();
            content1 = content1.replace("hello","not hello");

            bw.write(content1);
            bw.flush();
            System.out.println(sb);




        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                br.close();
                isr.close();
                fis.close();
                fos.close();
                osw.close();
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

执行结果

执行前的读取文件iofile.txt内容为:

iofile.txt

执行一次后的输出文件io2.txt

io2.txt 1次

执行5次的io2.txt

io2.txt 5次

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值