zoukankan      html  css  js  c++  java
  • 文件IO流完成文件的复制(复杂版本主要用来演示各种流的用途,不是最佳复制方案哦)

    package io;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;

     

    public class FileReaderDemo {
    public static void main(String[] args) {

    String path = "D:\eclipse\workspace\javase\src\io\FileInputStreamDemo.java";
    String outPah = "c:/ganbo.txt";

    File f = new File("D:\eclipse\workspace\javase\src\io\FileInputStreamDemo.java");
    File f2 = new File(outPah);

    //节点流
    FileInputStream fin = null;
    FileOutputStream fo = null;

    //包装流
    InputStreamReader inr = null;
    OutputStreamWriter bpw = null;

    //缓冲流
    BufferedReader br = null;
    BufferedWriter bw = null;


    try {
    //实例化输入流
    fin = new FileInputStream(f);
    inr = new InputStreamReader(fin);
    br = new BufferedReader(inr);

    //实例化输出流
    fo = new FileOutputStream(f2); //注意这里FileOutputStream()还有一个重载的方法,其参数后面加上 true and false 表示是追加还是覆盖写入
    bpw = new OutputStreamWriter(fo);
    bw = new BufferedWriter(bpw);


    //开始复制
    String temp;
    while((temp=br.readLine())!=null){
    System.out.println(temp);

    /**
    * 注意这里向文件中写出一行,默认一行行尾是没有回车换行的,需要我们手动添加 " ";
    * 表示回车,也就是使光标移动到当行的开始位置
    * 表示使光标跳到当前行的下一行
    * 一般就是 : " " 组合着用,其作用相当于键盘上面的 enter键
    */
    bw.write(temp+" ");
    bw.flush();
    }

    } catch (Exception e) {
    e.printStackTrace();
    }finally{

    //关闭资源
    if(br!=null){
    try {
    br.close();
    } catch (IOException e) {
    e.printStackTrace();
    }if(bw!=null){
    try {
    bw.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }




    }
    }

  • 相关阅读:
    WERKZEUG之WSGI阅读笔记
    Express4+Mongodb超简单入门实例
    git 命令小结
    GreenSock Animation Platform
    Nodejs开发框架Express3.0开发手记–从零开始
    交互设计实用指南系列(1) – 操作入口明确
    交互设计实用指南系列(4)—简洁清晰,自然易懂
    交互设计实用指南系列(5) – 突出重点,一目了然
    交互设计实用指南系列(6) –标签明晰、有效
    交互设计实用指南系列(8)—深广度平衡
  • 原文地址:https://www.cnblogs.com/ganbo/p/4081120.html
Copyright © 2011-2022 走看看