FileChannel示例

FileChannel 

 

 

public class TestFileChannel {
	/**
	 * 复制文件
	 * @param srcfile
	 * @param destFile
	 * @throws IOException
	 */
	public void copyFile(File srcfile,File destFile ) throws IOException{
		FileChannel srcfileChannel = new FileInputStream(srcfile).getChannel();
		
		FileChannel destfileChannel = new FileOutputStream(destFile).getChannel();
		srcfileChannel.transferTo(0, srcfileChannel.size(), destfileChannel);
		srcfileChannel.close();
		destfileChannel.close();
	}
	/**
	 * 文件尾部追加另一文件内容
	 * @param srcfile
	 * @param destFile
	 * @throws IOException
	 */
	public void appendFile(File srcfile,File destFile ) throws IOException{
		FileChannel srcfileChannel = new FileInputStream(srcfile).getChannel();
		FileChannel destfileChannel = new FileOutputStream(destFile,true).getChannel();
		destfileChannel.write(ByteBuffer.wrap("\r\n".getBytes()));
		destfileChannel.transferFrom(srcfileChannel, destfileChannel.size(), srcfileChannel.size());
		srcfileChannel.close();
		destfileChannel.close();
	}
	/**
	 * 固定文件格式内容写入
	 * 文件格式:
	 * 1. 性名:类型:String,长度:50 
	 * 2. 年龄:类型:int ,长度 :4
	 * 3. 身高: 类型:flaot ,长度:4
	 * 4. 性别: 类型:byte,长度:1
	 * 5. 简介长度: 类型:int,长度:4
	 * 6. 简介:类型  String,长度:不确定,由简介长度指定
	 * @param file
	 * @throws IOException
	 */
	public void writeFile(File file) throws IOException{
		FileChannel fileChannel = new FileOutputStream(file).getChannel();
		String name="yanlei";//max size=50;
		int age=30;
		float stature = 1.65f;
		byte sex = 1;
		String introduction="good main";
		ByteBuffer nameByteBuffer = ByteBuffer.allocate(50);
		nameByteBuffer.put(name.getBytes());
		nameByteBuffer.rewind();//limit=capacity,position=0
		
		byte [] introBytes =  introduction.getBytes();
		int length = 4+4+1+4+introBytes.length;//age.length+stature.length+sex.length+一个int(introduction占用字节数)+introduction.length
		ByteBuffer otherByteBuffer = ByteBuffer.allocate(length);
		otherByteBuffer.putInt(age);
		otherByteBuffer.putFloat(stature);
		otherByteBuffer.put(sex);
		otherByteBuffer.putInt(introBytes.length);
		otherByteBuffer.put(introBytes);
		otherByteBuffer.rewind();//limit=capacity,position=0
		

		fileChannel.write(new ByteBuffer[]{nameByteBuffer,otherByteBuffer});
		fileChannel.close();
	}
	/**
	 * 固定文件格式内容读取
	 * 文件格式:
	 * 1. 性名:类型:String,长度:50 
	 * 2. 年龄:类型:int ,长度 :4
	 * 3. 身高: 类型:flaot ,长度:4
	 * 4. 性别: 类型:byte,长度:1
	 * 5. 简介长度: 类型:int,长度:4
	 * 6. 简介:类型  String,长度:不确定,由简介长度指定
	 * @param file
	 * @throws IOException
	 */
	public void readFile(File file) throws Exception{
		FileChannel fileChannel = new FileInputStream(file).getChannel();
		ByteBuffer nameByteBuffer = ByteBuffer.allocate(50);
		fileChannel.read(nameByteBuffer);
		String name = new String(delete0(nameByteBuffer.array()));
		int otherLength = 4+4+1+4;
		ByteBuffer otherByteBuffer = ByteBuffer.allocate(otherLength);
		fileChannel.read(otherByteBuffer);
		otherByteBuffer.flip();
		int age = otherByteBuffer.getInt();
		float stature = otherByteBuffer.getFloat();
		byte sex = otherByteBuffer.get();
		int introLength = otherByteBuffer.getInt();
		ByteBuffer introByteBuffer = ByteBuffer.allocate(introLength);
		fileChannel.read(introByteBuffer);
		String introduction = new String(introByteBuffer.array());
		System.out.println("name="+name+",age="+age+",stature="+stature+",sex="+sex+",introduction="+introduction);
		//输出name=yanlei,age=30,stature=1.65,sex=1,introduction=good main
	}
	private byte [] delete0(byte [] data){
		if(data != null){
			int i=data.length-1;
			for(;i>=0;i--){
				if(data[i]!= 0){
					break;
				}
			}
			return Arrays.copyOf(data, i+1);
		}
		return null;
	}
	public static void main(String []args ){
		try{
		TestFileChannel test = new TestFileChannel();
		test.copyFile(new File("test.txt"), new File("destFile.txt"));
		test.appendFile(new File("test.txt"), new File("destFile.txt"));
		test.writeFile(new File("persion.txt"));
		test.readFile(new File("persion.txt"));
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

 

 

writeFile 方法生成的文件内容:



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值