FileChannel类的简单用法
2008-10-31 09:22
323 查看
清单一:
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class GetChannel
{
private static final int BSIZE = 1024;
public static void main(String[] args)throws IOException
{
FileChannel fc = new FileOutputStream ("data.txt").getChannel();
fc.write(ByteBuffer.wrap("some txt".getBytes()));//write() Writes a sequence of bytes to
//this channel from the given buffer
fc.close();
fc = new RandomAccessFile("data.txt","rw").getChannel();
fc.position(fc.size());//abstract FileChannel position(long newPosition)
//Sets this channel's file position.
fc.write(ByteBuffer.wrap("some more".getBytes()));
fc.close();
fc =new FileInputStream("data.txt").getChannel();
ByteBuffer bf = ByteBuffer.allocate(BSIZE);//static ByteBuffer allocate(int capacity)
//Allocates a new byte buffer.
//一旦调用read()来告知FileChannel向ByteBuffer存储字节,就必须调用缓冲器上的filp(),
//让它做好别人存储字节的准备(是的,他是有点拙劣,但请记住他是很拙劣的,但却适于获取大速度)
//
fc.read(bf);// Reads a sequence of bytes from this channel into the given buffer
bf.flip();
while(bf.hasRemaining())
System.out.print((char)bf.get());
}
}
清单二:
//Copying a file using channels and buffers;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class ChannelCopy
{
private static final int BSIZE = 1024;
public static void main(String [] args)throws IOException
{
if (args.length!=2)
{
System.out.println("argument:sourcefile destfile");
System.exit(1);
}
FileChannel
in = new FileInputStream (args[0]).getChannel(),
out = new FileOutputStream (args[1]).getChannel();
ByteBuffer bb = ByteBuffer.allocate(BSIZE);
while (in.read(bb)!=-1)
{
bb.flip();
out.write(bb);
bb.clear();//prepare for reading;清空缓冲区;
}
}
}
清单三
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class TransferTo
{
public static void main(String [] args) throws IOException
{
if(args.length!=2)
{
System.out.println("argument: sourcefile destfile");
System.exit(1);
}
FileChannel
in = new FileInputStream(args[0]).getChannel(),
out = new FileOutputStream(args[1]).getChannel();
//abstract long transferTo(long position, long count, WritableByteChannel target)
// Transfers bytes from this channel's file to the given writable byte channel.
in.transferTo(0,in.size(),out);
//or
//using out
//abstract long transferFrom(ReadableByteChannel src, long position, long count)
// Transfers bytes into this channel's file from the given readable byte channel.
// out.transferFrom(in,0,in.size());
}
}
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class GetChannel
{
private static final int BSIZE = 1024;
public static void main(String[] args)throws IOException
{
FileChannel fc = new FileOutputStream ("data.txt").getChannel();
fc.write(ByteBuffer.wrap("some txt".getBytes()));//write() Writes a sequence of bytes to
//this channel from the given buffer
fc.close();
fc = new RandomAccessFile("data.txt","rw").getChannel();
fc.position(fc.size());//abstract FileChannel position(long newPosition)
//Sets this channel's file position.
fc.write(ByteBuffer.wrap("some more".getBytes()));
fc.close();
fc =new FileInputStream("data.txt").getChannel();
ByteBuffer bf = ByteBuffer.allocate(BSIZE);//static ByteBuffer allocate(int capacity)
//Allocates a new byte buffer.
//一旦调用read()来告知FileChannel向ByteBuffer存储字节,就必须调用缓冲器上的filp(),
//让它做好别人存储字节的准备(是的,他是有点拙劣,但请记住他是很拙劣的,但却适于获取大速度)
//
fc.read(bf);// Reads a sequence of bytes from this channel into the given buffer
bf.flip();
while(bf.hasRemaining())
System.out.print((char)bf.get());
}
}
清单二:
//Copying a file using channels and buffers;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class ChannelCopy
{
private static final int BSIZE = 1024;
public static void main(String [] args)throws IOException
{
if (args.length!=2)
{
System.out.println("argument:sourcefile destfile");
System.exit(1);
}
FileChannel
in = new FileInputStream (args[0]).getChannel(),
out = new FileOutputStream (args[1]).getChannel();
ByteBuffer bb = ByteBuffer.allocate(BSIZE);
while (in.read(bb)!=-1)
{
bb.flip();
out.write(bb);
bb.clear();//prepare for reading;清空缓冲区;
}
}
}
清单三
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class TransferTo
{
public static void main(String [] args) throws IOException
{
if(args.length!=2)
{
System.out.println("argument: sourcefile destfile");
System.exit(1);
}
FileChannel
in = new FileInputStream(args[0]).getChannel(),
out = new FileOutputStream(args[1]).getChannel();
//abstract long transferTo(long position, long count, WritableByteChannel target)
// Transfers bytes from this channel's file to the given writable byte channel.
in.transferTo(0,in.size(),out);
//or
//using out
//abstract long transferFrom(ReadableByteChannel src, long position, long count)
// Transfers bytes into this channel's file from the given readable byte channel.
// out.transferFrom(in,0,in.size());
}
}
相关文章推荐
- context.openFileoutput及文件存储,删除,读取简单用法
- Java中File用法的注意事项和FileInputStream和FileOutputStream的简单使用
- C#的OpenFileDialog的简单用法
- 关于openFileOutput及文件存储,删除,读取简单用法
- File类的简单用法
- RandomAccessFile简单用法
- Assembly.Load,Assembly.LoadFrom,Assembly.LoadFile的简单用法
- SaveFileDialog的简单用法
- File类简单用法!
- python中with的简单用法
- sqlhelper中事务的简单用法
- Html.RenderAction简单用法
- C# Skip和Take的简单用法
- Python 中 Virtualenv 和 pip 的简单用法详解
- jQuery Valdate、bootstrap-datepicker和distpicker的一些简单用法
- pthread的简单用法
- Android TabLayout的简单用法
- 关于c#中的Timer控件的简单用法
- 转:一个简单的jQuery插件ajaxfileupload实现ajax上传文件例子
- Android中AsyncTask的简单用法(简化版)