从FileChannel读取数据:通过Buffer对象带辩,将从FileChannel中读取的数据缓磨滑存在Buffer中,瞎行腊然后通过Buffer读取数据。
FileChannel是一个用读写,映射和 *** 作一个文件的通道。
import java.io.Fileimport java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStream
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
public class FileChannelDemo {
/**
* FileChannel是用于读取、写入、映射和 *** 作文件的通道。
*
*文件通道在其文件中稿激岁有一个当前 position,可对其进行查询和修改。
*该文件本身包含一个可读写的长度可变的字节序列,并且可以查询该文件的当前大小。
*写入的字节超出文件的当前大小时,则增加文件的大小;截取 该文件时,则减小文件的大小。键睁
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File in = new File("D:\\铅源in.txt")
File out = new File("D:\\out.txt")
if(in.createNewFile()){
System.out.println("in.txt被创建")
FileOutputStream is = new FileOutputStream(in)
byte[] b = "我一定能行的".getBytes()
is.write(b, 0, b.length)
}
if(out.createNewFile()){
System.out.println("out.txt被创建")
}
FileInputStream is = new FileInputStream(in)
FileOutputStream os = new FileOutputStream(out)
FileChannel fis = is.getChannel()
FileChannel fos = os.getChannel()
ByteBuffer bytedata = ByteBuffer.allocate(100)
while(fis.read(bytedata)!= -1){
//通过通道读写交叉进行。
bytedata.flip()
fos.write(bytedata)
bytedata.clear()
}
fis.close()
fos.close()
is.close()
os.close()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)