请教各位大侠,java io 操作从文件读取数据那一(几)个类效率较高?
在线等>>>
谢谢!
在线等>>>
谢谢!
解决方案 »
- 谁会java+ssl会话,需要写个安卓应用的小号注册机...重谢
- 怎么将一个vector装换成一个hashmap
- 如何设置JScrollPane 上下滚动 但是左右不滚动啊
- 请问如何实现限制软件客户端的数量?
- 关于JAVA中数组长度为0的情况
- Runtime.getRuntime().exec(cmd)不能执行shell脚本啊
- 怎么设置JMenuItem菜单项的背景图片 ?
- java死锁问题
- 求The JasperReports Ultimate Guide文档
- 解释下Collections.synchronizedList()都作了哪些工作?
- 关于数据库表设计的问题
- split方法是不是需要编码限制?
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;/** *//**
* @author maoyeye
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class FileUtil ...{
public static ByteBuffer readFile(String filename) throws Exception
{
FileChannel fiChannel = new FileInputStream(filename).getChannel();
MappedByteBuffer mBuf;
mBuf = fiChannel.map(FileChannel.MapMode.READ_ONLY, 0, fiChannel.size());
fiChannel.close();
fiChannel = null;
return mBuf;
}
public static void saveFile(ByteBuffer src, String filename) throws Exception
{
FileChannel foChannel = new FileOutputStream(filename).getChannel();
foChannel.write(src);
foChannel.close();
foChannel = null;
}
public static void saveFile(FileChannel fiChannel, String filename) throws IOException
{
MappedByteBuffer mBuf;
mBuf = fiChannel.map(FileChannel.MapMode.READ_ONLY, 0, fiChannel.size()); FileChannel foChannel = new FileOutputStream(filename).getChannel();
foChannel.write(mBuf);
fiChannel.close();
foChannel.close();
fiChannel = null;
foChannel = null;
}
public static void main(String[] args) throws Exception
{
final String suffix = ".txt";
final String filename = "bufferTemp";
final String srcFile = filename + suffix ;
final String backupFile = filename + "-" + System.currentTimeMillis() + suffix;
ByteBuffer byteBuffer = FileUtil.readFile(srcFile);
FileUtil.saveFile(byteBuffer, backupFile);
byteBuffer = null;
}
}