Read and Write File Code

package com.hiredmyway.action.test;

import java.io.FileInputStream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.Date;

public class TestChannelFileWrite {

    /**
     * //读写文件方法1
     *
     * @param sourceFile
     * @param targetFile
     * @throws IOException
     */
    public static void copyFile(File sourceFile, File targetFile)
            throws IOException {

        // 新建文件输入流并对它进行缓冲

        FileInputStream input = new FileInputStream(sourceFile);

        BufferedInputStream inBuff = new BufferedInputStream(input);

        // 新建文件输出流并对它进行缓冲

        FileOutputStream output = new FileOutputStream(targetFile);

        BufferedOutputStream outBuff = new BufferedOutputStream(output);

        // 缓冲数组

        byte[] b = new byte[1024 * 5];

        int len;

        while ((len = inBuff.read(b)) != -1) {

            outBuff.write(b, 0, len);

        }

        // 刷新此缓冲的输出流

        outBuff.flush();

        // 关闭流

        inBuff.close();

        outBuff.close();

        output.close();

        input.close();

    }

    /**
     * //读写文件方法2
     * 用管道流
     * @param inFile
     * @param outFile
     * @throws IOException
     */
    public void getFilePath(String inFile, String outFile) throws IOException {
        FileInputStream inf = new FileInputStream(inFile);
        FileOutputStream outf = new FileOutputStream(outFile);
        // ByteBuffer buffer = ByteBuffer.allocate(1024);
        ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
        // 准备文件读取的管道-->相当于烟仓和烟管
        FileChannel inFc = inf.getChannel();
        FileChannel outFc = outf.getChannel();
        Charset charSet = Charset.forName("utf-8");
        // 进行编码解码-->相当于水斗中水的过滤作用
        CharsetDecoder decoder = charSet.newDecoder();
        CharsetEncoder encoder = charSet.newEncoder();
        while (true) {
            // 准备向Buffer中写入数据-->相当于点燃烟丝,完事具备只欠东风
            buffer.clear();
            // 进行字符编码 -->相当于水的过滤作用
            CharBuffer cb = decoder.decode(buffer);
            ByteBuffer bb = encoder.encode(cb);

            // 数据经过编码以后暂存缓冲区-->相当于经过水过滤后的烟暂停在水斗中
            int t = inFc.read(bb);
            if (t == -1) {
                break;
            }
            bb.flip();
            // 将字节码写入目标文件-->相当于烟已经进入到嘴里
            outFc.write(bb);
        }

        outFc.close();
        inFc.close();
    }

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // 读写文件方法1
        copyFile(new File("C:\\summary.txt"), new File("C:\\wk-bak23.txt"));

        Long l = new Date().getTime();
        
//        String inFile = "C:\\Resume_12679_1335256694218.pdf";

        // summary.txt 有中文汉字
        String inFile = "C:\\summary.txt";
        File fileName = new File(inFile);
        if (!fileName.exists()){
            System.out.println("File does't exist!");
            return;
        }
        // String temp = String.valueOf(l);
        String filePath = "C:\\tmp";
        File fp = new File(filePath);
        // 判断文件夹是否存在(tmp 是否有这个目录)
        if (!fp.isDirectory()) {
            // 没有就创建tmp在C:\\盘下
            fp.mkdir();
        }
        // 拷贝到tmp 下
//        String outFile= "\\"+filePath+"resume_"+l+"_backup.pdf";
//        String outFile = "C:\\tmp\\Resume_12679_" + l + "_backup.pdf";
        String outFile = "C:\\tmp\\Resume_12679_" + l + "_backup.txt";

        TestChannelFileWrite tf = new TestChannelFileWrite();
        // 读写文件方法2
        tf.getFilePath(inFile, outFile);
        System.out.println("read write success!");

    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值