您的位置:首页 > 其它

Read and Write File Code

2012-04-26 16:48 495 查看
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!");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: