文件操作,输入输出流(stream, writer)

一, 文件常用API
这里写图片描述

二,输入流
这里写图片描述

这里写图片描述
三,输出流
这里写图片描述
这里有一个文件内容复制的实例:

package stream;

import java.io.*;

/**
 * Created by Administrator on 2016/8/7.
 * 文件输入输出流标准示范
 */
public class Test {
    public static void main(String[] args) {

        File source = new File("f:/log/source.txt");
        File target = new File("f:/log/target.txt");
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(target, true); //true 表示向文件添加数据,不覆盖

            int len;
            byte[] bytes = new byte[1024];
            while ((len = fis.read(bytes)) != -1) { //读取资源文件
                /* 这里使用len ,而不是字节的长度*/
                fos.write(bytes, 0, len); //写到目标文件之中
            }
            fos.flush(); //刷新输出流
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally { //关闭流
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
package stream;

import java.io.*;

/**
 * Created by Administrator on 2016/8/7.
 * 缓冲字符流实例
 */
public class CharStream {
    public static void main(String[] args) {
        Reader fr = null;
        BufferedReader br = null;
        Writer wr = null;
        BufferedWriter bw = null;

        try {
            fr = new InputStreamReader(new FileInputStream("f:log/source.txt"), "utf-8"); //使用特定编码方式读取文件,防止文件乱码
            br = new BufferedReader(fr);
            wr = new OutputStreamWriter(new FileOutputStream("f:log/target.txt")); //true:文件追加
            bw = new BufferedWriter(wr);
            String line;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
            }

            bw.flush(); //刷新输出流
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            try {
                bw.close();
                br.close();
                br.close();
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

四,对象序列化实例

package stream;

import threadTest.socket.Person;

import java.io.*;

/**
 * Created by Administrator on 2016/8/7.
 * 序列化对象
 */
public class SeriaStream {
    public static void main(String[] args) {
        ByteArrayOutputStream baos = null;
        ObjectOutputStream ops = null;
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        byte[] bytes = null;
        Person person = new Person(); //被实例化的类
        person.setName("name");

        try {
            /*序列化*/
            baos = new ByteArrayOutputStream(); //存储对象序列化后的字节数组
            ops = new ObjectOutputStream(baos);
            ops.writeObject(person);
            bytes = baos.toByteArray();

            /*反序列化*/
            bais = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bais);
            Person per = (Person)ois.readObject();
            System.out.println(per.getName());

        } catch (IOException e) {
            e.printStackTrace();
        } catch(ClassNotFoundException e1){
            e1.printStackTrace();
        } finally {
            try {
                ois.close();
                bais.close();
                ops.close();
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

注意:
判断是否读取完的方式:
1,如果实 BufferedReader 对象的方法:判断是否为 null;
2,fouze否则判断是否为 -1;
字节流不会产生乱码,可以读取任何文件, 字符流可能会产生乱码
输出流操作不要忘记 flush() 操作;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值