- 浏览: 862455 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (509)
- android (55)
- CSS (23)
- eclipse (25)
- Data Structes and Algorithms (53)
- J2SE (87)
- Java_面试学习_j2se (26)
- java_面试学习_非技术 (13)
- java_gui (2)
- java_设计模式 (27)
- JDBC (10)
- java_web (15)
- hibernate (5)
- Oracle (37)
- Struts2 (7)
- Word-----dos (24)
- Jbpm (3)
- java小技巧 (8)
- math (1)
- flex (12)
- WebService (4)
- 生活 (9)
- 小框架或小语言 (27)
- spring (1)
- 面试~~~软实力 (7)
- jstat的用法 (1)
- jmap (1)
- 数据链路层和传输层的流量控制区别 (1)
- shell (0)
- 财商 (1)
- javascript (0)
- js研究 (1)
- 代码收集 (0)
最新评论
-
海尔群:
http://jingyan.baidu.com/articl ...
android加密 -
完美天龙:
------------------------- ...
asm----字节码操纵 -
houniao1990:
大神,请问 string 类型 定义为 oracle的 cha ...
hibernate注解 -
JamesQian:
Line:103
f.doFilter(msg);
是否需 ...
责任链模式_过滤器模式 -
sacoole:
好评
interview--- 如何从N个数中选出最大(小)的n个数?
缓冲区 如:ByteBuffer其实是对基本数据类型的封装 byte[]而已啦, CharBuffer,ShortBuffer,IntBuffer, LongBuffer,FloatBuffer,DoubleBuffer 其实这些所谓的缓冲区就是给基本数据类型多加了几个变量而已 position(当前可以放的位置),limit(最后一个元素位置),capacity 1. IntBuffer buf = IntBuffer.allocate(10); int temp={5,7,9}; buf.put(3); buf.put(temp); buf.flip(); //重设缓冲区(翻动---position=0;limit=原本的position) while(buf.hasRemaining()){ int x = buf.get(); } 2. 创建子缓冲区 public class Test{ public static void main(String[] args){ IntBuffer buf = IntBuffer.allocate(10); IntBuffer sub = null; for(int i=0;i<10;i++){ buf.put(2*i+1); } buf.position(2); //主缓冲区指针设置在第三个元素 buf.limit(6); //主缓冲区limit为6 sub = buf.slice(); //开辟子缓冲区,也就是他有一个引用指向了buf的部分区域 //sub = buf.asReadOnlyBuffer(); //创建只读缓冲区 for(int i=0;i<sub.capacity();i++){ int temp = sub.get(i); sub.put(temp -1); //这是在改变buf中的东西哈,position指哪改哪 } buf.flip(); buf.limit(buf.capacity); //设置limit while(buf.hasRemaining()){ int x = buf.get(); System.out.println(x+","); } } } 3。创建直接缓冲区,如果创建了直接缓冲区,则JVM将尽量最大努力直接对其执行本机的IO操作 public static ByteBuffer allocateDirect(int capacity) 二:通道 通道是一个可以读取和写入数据的一种形式, 一般先放入缓冲区然后写入或读取,可进行双向操作哈 Channel接口定义 void close() //关闭通道 boolean isOpen(); //判断此通道是否是打开的 FileChannel()完成文件双向操作 public class Test{ public static void main(String[] args){ String[] info = {"MLDN","www.mldn.cn","www.mldnjava.cn","haha"}; File file = new File("d:"+File.seperator+"a.txt"); FileOutputStream output = new FileOutputStream(file); FileChannel fout = output.getChannel(); //得到输出通道 ByteBuffer buf = ByteBuffer.allocate(1024); for(int i=0;i<info.length;i++){ buf.put(info[i].getBytes()); //字符串变为字节数组放入缓冲区之中 } buf.flip(); fout.write(buf); //输出缓冲区的内容 fout.close(); output.close(); } } //注意:输出流通道只能写。。。输入流通道只能读。。哎 class Test{ public static void main(String[] args){ File file1 = new File("d:"+File.separator+"note.txt"); File file2 = new File("d:"+File.separator+"outnote.txt"); FileChannel fout = (new FileOutputStream(file2)).getChannel(); //这样写不好,因为流对象需要关闭啦 FileChannel fin = (new FileInputStream(file2)).getChannel(); ByteBuffer buf = ByteBuffer.allocate(1024); int temp =0; while((temp = fin.read(buf)) != -1){ buf.flip(); fout.write(out); buf.clear(); //清空缓冲区 } fin.close(); fout.close(); //input.close(); //output.close(); } } //RamdomAccessFile :较慢 FileInputStream:较慢 缓冲读取:速度较快 内存映射:最快 MappedByteBuffer:使用此种方式读取最快哈 需要将一个输入的操作流绑定在内存映射 FileChannel类的三个内存映射模式: public static final FileChannel.MapMode.READ_ONLY //只读映射 FileChannel.MapMode.READ_WRITE //读写模式 FileChannel.MapMode.PRIVATE //专用(写入时拷贝)映射模式 class Test{ public static void main(String[] args){ File file = new FIle("d:"+File.separator+"a.txt"); FileInputStream input = null; input = new FileInputStream(file); FileChannel fm = null; fm = input.getChannel(); MapperByteBuffer buf =fin.map(FileChannel.MapMode.READ_ONLY,0,file.length()); //把文件直接映射到内存,而不是一行行读 byte data[] = new byte[(int)file.length()]; //开辟空间接收内容 int foot = 0; while(buf.hasRemaining()){ data[foot++] = buf.get(); //读取数据 } System.out.println(new String(data)); fin.close(); input.close(); } } //注意:上述操作代码是最快的,但是如果在使用以上操作代码的时候,执行的是写入操作 则有可能非常危险,因为仅仅只是改变数组中的单个元素这样的简单操作,就有可能直接修改磁盘 上的文件,因为修改数据与将数据保存在磁盘上是一样的
发表评论
-
jdbc--批处理
2012-06-08 18:15 1074http://jdgnewtouch.iteye.com/bl ... -
jdbc
2012-06-07 20:51 853http://www.iteye.com/topic/6466 ... -
j2se----jdk6---httpServer
2012-06-05 20:42 1343package com.tdt.server.httpse ... -
j2se基础---ThreadLocal
2012-06-02 20:47 1016package cn.itcast.heima2; ... -
获取运行时的堆栈信息
2011-12-11 11:00 2129public class Hi { public st ... -
简说XML的解析方式(DOM,SAX,StAX)
2011-09-30 08:44 870一般来说,解析XML文件存在着两种方式,一种是event-ba ... -
j2se----Java异步socket
2011-07-11 17:10 1402用异步输入输出流编写Socket进程通信程序 在Merlin ... -
j2se----socket的缓冲区讨论
2011-07-08 19:52 1725关于socket的发送缓冲区 ... -
Tomcat的Socket实现:org.apache.tomcat.util.net(一)
2011-07-08 19:12 1648org.apache.tomcat.util.net包的内容都 ... -
翻转句子中单词的顺序
2011-07-07 22:42 1698题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺 ... -
j2se---同步的Map
2011-07-07 20:50 973顾名思义LinkedHashMap是比HashMap多了一个链 ... -
j2se---同步的Map
2011-07-07 20:48 10Map<String String> map = ... -
asm----字节码操纵
2011-06-30 09:14 3875想通过asm的代码生成来写.class文件至少得了解下面的 ... -
j2se----java中,如何获得用户当前的工作目录
2011-05-11 09:44 1815获得当前路径, get java current dire ... -
j2se-----zip
2011-05-10 09:05 1019private InitData getInitFile( ... -
j2se-----可变参数列表
2011-04-01 10:53 838public static void main(Strin ... -
UML
2010-12-31 09:42 865组合: 一种强聚合 class Bird{ ... -
j2se-----metadata
2010-12-19 17:14 983DatabaseMetaData的用法 ... -
python------一小时学会
2010-12-08 16:28 1870先上java与python的相互调用 如何在Java中调用Jy ... -
j2ee------download.jsp
2010-12-08 13:39 1092Logger logger = C ...
相关推荐
jaxmpp-j2se-3.0.0.jar(XMPP/Tigase开发,java代码类型客户端使用的jar包) 需要的朋友自己下载,一个朋友让我帮他找的,就顺便发出来给更多需要的朋友吧
core-2.3.0 + QRCode + zxing-1.6-core + zxing-j2se-1.7 集合jar包
j2se-doc-cn java的API文档,有助于你写JAVA程序.希望大家更加踊跃的学习和更好的学习JAVA。
资源名称:Java-J2se、J2ee开发全程视频教程和源码(87集)资源目录:【】Java-J2se-J2ee全程教程(01-10)【】Java-J2se-J2ee全程教程(11-20)【】Java-J2se-J2ee全程教程(21-30)【】Java-J2se-J2ee全程教程(31...
J2SE-IO.ppt,详细介绍J2SE中的IO流
J2SE-孙鑫授课总结的ppt,本来打算把他的同步教学视频也给传上来,只因为太大,而我现在只能传20MB以上的,所以只好罢了...
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
zxing系列JAVA库,主要用于二维码的编码和解码
javase 的api文档 学习java最基本的东西 大家应该都懂得
jar包,官方版本,自测可用
官方版本,亲测可用
官方版本,亲测可用