输入输出流 示例

package file;

import java.io.*;
import java.nio.charset.Charset;

/**
 * Created by Administrator on 2018/1/27.
 * 输入输出流 示例
 */
public class TestFile {
    public static void main(String[] args) {
        //1 复制图片
        try {
            DataInputStream dis = new DataInputStream(new FileInputStream("D:\\花1.jpg"));
            DataOutputStream dos = new DataOutputStream(new FileOutputStream("D:\\花2.jpg"));
            int temp;
            while ((temp = dis.read()) != -1){
                dos.write(temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }

        //2 序列化
        try {
            OutputStream out = new FileOutputStream("D:/Student1.class");
            ObjectOutputStream oos = new ObjectOutputStream(out);
            Student student = new Student();//Student要实现Serializable
            student.setName("测试");
            student.setScore(100d);
            oos.writeObject(student);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //3 反序列化
        try {
            InputStream is = new FileInputStream("D:/Student1.class");
            ObjectInputStream ois = new ObjectInputStream(is);
            Object oo = ois.readObject();
            Student stu = (Student)oo;
            System.out.println(stu.getName());
            System.out.println(stu.getScore());
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

       //4 替换文件内容
        try{
//            BufferedReader br = new BufferedReader(new FileReader("d:/pet.template"));//执行 bw.write(newString)后,写入的文件有乱码,故采用InputStreamReader
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:/pet.template"),"UTF-8"));
            String ss = null;
            StringBuffer sb = new StringBuffer();
            while ((ss = br.readLine()) != null){
                sb.append(ss);
            }
            System.out.println(sb.toString());

//            FileWriter fw = new FileWriter("d:/pet1.txt");
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:/pet1.txt"),"UTF-8"));
            String newString = sb.toString().replace("{name}", "测试11").replace("{type}", "狗狗").replace("{master}", "拉拉");
            bw.write(newString);
            System.out.println(newString);

//            bw.flush();//flush是清空缓冲区,就是立即输出到目标文件,而不是等缓冲区满了再输出,write只是将数据输出到缓冲区,还没有输出到目标文件。
            bw.close();//关闭字符流就不用flush,不关闭的话只能flush后才能写入;close()会调用flush()
            br.close();//如果一直不关闭流,就会导致内存被大量占用,以致程序崩溃

            System.out.println(Charset.defaultCharset());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值