首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

FileChannel示范

2014-05-14 
FileChannel示例FileChannel???public class TestFileChannel {/** * 复制文件 * @param srcfile * @param

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=0byte [] introBytes =  introduction.getBytes();int length = 4+4+1+4+introBytes.length;//age.length+stature.length+sex.length+一个int(introduction占用字节数)+introduction.lengthByteBuffer 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=0fileChannel.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 方法生成的文件内容:


FileChannel示范
?

热点排行