java noi读取文件
来源:互联网 发布:淘宝默认付款是几天 编辑:程序博客网 时间:2024/06/05 19:29
要利用nio进行文件的去读需要明白缓冲区的原理可以通过以下的链接进行了解~
http://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.html#
以下是几种读取利用noi读取和写入文件的几种方法.
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;/*参考网址 * http://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.html * */public class NFile{/**/public static StringBuilder readFile(String dir) throws Exception{FileInputStream input = new FileInputStream(dir);//获取通道FileChannel fc = input.getChannel();//创建缓冲区.ByteBuffer buffer = ByteBuffer.allocate(2048);int n = -1;StringBuilder sb = new StringBuilder("");while((n = fc.read(buffer)) != -1){//System.out.println(buffer);System.out.println(n);buffer.flip();/*flip,将limit设置为当前的position的位置,position=0*///sb.append(new String(buffer.array(),0,n));byte[] b = new byte[n] ;buffer.get(b);sb.append(new String(b));System.out.println(buffer);/*清空缓冲区让缓冲区可以重新进入读取数据,设置当前的limit为capacity,position为0*/buffer.clear();}if(null != input) {input.close();}return sb;}public static void readFile() throws Exception{RandomAccessFile aFile = new RandomAccessFile("d:\\text\\group.txt", "rw");FileChannel inChannel = aFile.getChannel();ByteBuffer buf = ByteBuffer.allocate(2048);int bytesRead = inChannel.read(buf);while (bytesRead != -1) {System.out.print(bytesRead);buf.flip();while(buf.hasRemaining()){/*利用get进行直接的读取,也可以get一个byte数组*/ System.out.print((char) buf.get());}buf.clear();bytesRead = inChannel.read(buf);}aFile.close();}public static boolean writeFile(String dir,String content ) throws Exception{FileOutputStream output = new FileOutputStream(dir);FileChannel fout = output.getChannel();byte[] contenByte = content.getBytes("GBK");//byte[] contenByte = content.getBytes();//方法1://ByteBuffer buffer = ByteBuffer.allocate(contenByte.length);//buffer.put(contenByte);//buffer.flip();//方法二:ByteBuffer buffer = ByteBuffer.wrap(contenByte);fout.write(buffer);fout.close();output.close();return true;}/*文件直接映射到内存中进行读取.*/public static StringBuilder FileMap(String dir) throws Exception{/*打开文件刘*/FileInputStream input = new FileInputStream(dir);/*获取管道*/FileChannel fc = input.getChannel();MappedByteBuffer mbb = fc.map( FileChannel.MapMode.READ_ONLY, 0, fc.size() );StringBuilder sb = new StringBuilder("");byte[] b = new byte[(int)fc.size()] ;mbb.get(b);sb.append(new String(b));input.close();return sb;}public static void main(String[] args) throws Exception{StringBuilder sb = null;//sb = FileMap("d:\\text\\group.txt");readFile();// sb = readFile("d:\\text\\group.txt");//writeFile("d:\\text\\group_cpy.txt",sb.toString());System.out.println("");System.out.println(sb);}}
0 0
- java noi读取文件
- java noi socket
- java读取ini文件
- JAVA读取XML文件
- java读取中文文件
- Java读取文件
- JAVA读取XML文件
- java读取中文文件
- java读取pdf文件
- JAVA读取word文件
- JAVA读取PDF文件
- java读取word文件
- java读取excel文件
- java读取excel 文件
- java 读取文件(中文)
- java读取文件源代码
- java 读取文件
- Java读取Properties文件
- android Handler/Looper 总结
- xcode7以上UIWebView报错:The resource could not be loaded
- IOS绘图
- C++设计模式[十]享元模式
- angularjs中的$watch,对对象进行监视
- java noi读取文件
- 剑指offer(六)
- 专业级的PHP IDE——Zend Studio正式发布v13[附下载]
- 开发插曲:注解的使用
- CSS3 border-radius属性
- 如何为我们的Snappy Ubuntu应用编译并打包Snap(1)
- Android Edittext输入后切换界面出现白块
- A+B for Input-Output Practice (V)
- iOS App瘦身注意armv7 armv7s arm64