当前位置: 首页 > news >正文

网站做百度推广要多少钱网站优化提升排名

网站做百度推广要多少钱,网站优化提升排名,教育网站怎么做,mc做地图画网站目录 一、RandomAccessFile1.1 为什么要有RandomAccessFile?1.2 常用方法简介1.3 RandomAccessFile 特点和优势1.3.1 既可以读也可以写1.3.2 可以指定位置读写 1.4 示例 二、NIONIO使用示例 一、RandomAccessFile 1.1 为什么要有RandomAccessFile? Ran…

目录

  • 一、RandomAccessFile
      • 1.1 为什么要有RandomAccessFile?
      • 1.2 常用方法简介
      • 1.3 RandomAccessFile 特点和优势
        • 1.3.1 既可以读也可以写
        • 1.3.2 可以指定位置读写
      • 1.4 示例
  • 二、NIO
      • NIO使用示例


一、RandomAccessFile

1.1 为什么要有RandomAccessFile?

RandomAccessFile 类在 Java 中的作用主要是允许我们以随机访问的方式读取和写入文件内容。这与传统的顺序访问文件不同,传统方式是从文件的开头开始逐个字节或一定大小的数据块依次读取或写入。
我们可能会使用 RandomAccessFile:

  • 随机读取和写入文件内容:RandomAccessFile 允许我们通过指定文件中的位置(偏移量)来直接读取或写入数据,而不需要按照顺序逐个字节地读取或写入。这对于需要随机访问文件内容的应用程序非常有用。
  • 修改文件内容:通过 RandomAccessFile,我们可以定位到文件中的特定位置,并修改该位置的内容,而不会影响到其他部分的数据。
  • 读取和写入大文件:当处理大文件时,RandomAccessFile 可以更高效地处理文件的读取和写入操作,因为可以直接跳转到文件中的指定位置。
  • 实现文件锁定:RandomAccessFile 还提供了一些方法来实现文件的锁定,以确保在多线程环境下对文件的安全访问。

也可以使用RandomAccessFile 实现多线程分段下载的功能。

1.2 常用方法简介

  • 构造方法: RandomAccessFile raf = newRandomAccessFile(File file, String mode);
    其中参数 mode 的值可选 “r”: 可读, “w” : 可写, “rw”: 可读写;

  • 成员方法:
    seek(int index);可以将指针移动到某个位置开始读写;
    ​setLength(long len);给写入文件预留空间:

1.3 RandomAccessFile 特点和优势

1.3.1 既可以读也可以写

RandomAccessFile不属于InputStream和OutputStream类系的它是一个完全独立的类, 所有方法(绝大多数都只属于它自己)都是自己从头开始规定的,这里面包含读写两种操作。

1.3.2 可以指定位置读写

RandomAccessFile能在文件里面前后移动, 在文件里移动用的seek( ),所以它的行为与其它的I/O类有些根本性的不同。 总而言之, 它是一个直接继承Object的, 独立的类。 只有RandomAccessFile才有seek搜寻方法, 而这个方法也只适用于文件。

1.4 示例

/*** RandomAccessFile的使用* 1.RandomAccessFile直接继承于java.lang.Object类,实现了DataInput和DataOutput接口* 2.RandomAccessFile既可以作为一个输入流,又可以作为一个输出流** 3.如果RandomAccessFile作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建。*   如果写出到的文件存在,则会对原有文件内容进行覆盖。(默认情况下,从头覆盖)* 4. 可以通过相关的操作,实现RandomAccessFile“插入”数据的效果*/
public class RandomAccessFileTest {@Testpublic void test1() {RandomAccessFile raf1 = null;RandomAccessFile raf2 = null;try {//1.raf1 = new RandomAccessFile(new File("爱情与友情.jpg"),"r");raf2 = new RandomAccessFile(new File("爱情与友情1.jpg"),"rw");//2.byte[] buffer = new byte[1024];int len;while((len = raf1.read(buffer)) != -1){raf2.write(buffer,0,len);}} catch (IOException e) {e.printStackTrace();} finally {//3.if(raf1 != null){try {raf1.close();} catch (IOException e) {e.printStackTrace();}}if(raf2 != null){try {raf2.close();} catch (IOException e) {e.printStackTrace();}}}}@Testpublic void test2() throws IOException {RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");raf1.seek(3);//将指针调到角标为3的位置raf1.write("xyz".getBytes());//raf1.close();}/*使用RandomAccessFile实现数据的插入效果*/@Testpublic void test3() throws IOException {RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");raf1.seek(3);//将指针调到角标为3的位置//保存指针3后面的所有数据到StringBuilder中StringBuilder builder = new StringBuilder((int) new File("hello.txt").length());byte[] buffer = new byte[20];int len;while((len = raf1.read(buffer)) != -1){builder.append(new String(buffer,0,len)) ;}//调回指针,写入“xyz”raf1.seek(3);raf1.write("xyz".getBytes());//将StringBuilder中的数据写入到文件中raf1.write(builder.toString().getBytes());raf1.close();//思考:将StringBuilder替换为ByteArrayOutputStream}
}

二、NIO

NIO——FileChannel

Channel是对I/O操作的封装。
FileChannel配合着ByteBuffer, 将读写的数据缓存到内存中, 然后以批量/缓存的方式read/write, 省去了非批量操作时的重复中间操作, 操纵大文件时可以显著提高效率( 和Stream以byte数组方式有什么区别? 经过测试, 效率上几乎无区别) 。

NIO使用示例

public class FileChannelDemo {public static void main(String[] args) {try {// 创建一个 RandomAccessFile 对象,以读写模式打开文件RandomAccessFile file = new RandomAccessFile("test.txt", "rw");FileChannel channel = file.getChannel(); // 获取文件的 FileChannel// 写入数据到文件String data = "Hello, FileChannel!";ByteBuffer buffer = ByteBuffer.allocate(1024); // 创建一个 ByteBufferbuffer.put(data.getBytes()); // 将数据写入 ByteBufferbuffer.flip(); // 切换为读模式channel.write(buffer); // 将数据写入 FileChannel// 重置文件指针位置到开头channel.position(0);// 读取文件数据buffer.clear(); // 清空 ByteBufferint bytesRead = channel.read(buffer); // 从 FileChannel 读取数据到 ByteBufferif (bytesRead > 0) {buffer.flip(); // 切换为读模式byte[] readData = new byte[bytesRead];buffer.get(readData); // 从 ByteBuffer 中读取数据System.out.println("Read data from file: " + new String(readData));} else {System.out.println("No data read from file.");}// 关闭 FileChannel 和 RandomAccessFilechannel.close();file.close();} catch (Exception e) {e.printStackTrace();}}
}

参考链接:

【NIO实战】深入理解FileChannel
Java NIO


文章转载自:
http://floriferous.tsnq.cn
http://mutate.tsnq.cn
http://boatbill.tsnq.cn
http://equiponderance.tsnq.cn
http://mayotte.tsnq.cn
http://outrank.tsnq.cn
http://sudan.tsnq.cn
http://frumentaceous.tsnq.cn
http://bermuda.tsnq.cn
http://soda.tsnq.cn
http://metage.tsnq.cn
http://therewithal.tsnq.cn
http://nundine.tsnq.cn
http://abolitionism.tsnq.cn
http://glossitis.tsnq.cn
http://hemispheroid.tsnq.cn
http://smirk.tsnq.cn
http://applicatively.tsnq.cn
http://superlunar.tsnq.cn
http://lisping.tsnq.cn
http://subnuclear.tsnq.cn
http://medullary.tsnq.cn
http://lustreless.tsnq.cn
http://theism.tsnq.cn
http://ausgleich.tsnq.cn
http://dichroic.tsnq.cn
http://chiller.tsnq.cn
http://anglesite.tsnq.cn
http://phleboid.tsnq.cn
http://albuminuria.tsnq.cn
http://spicae.tsnq.cn
http://oratorio.tsnq.cn
http://sudation.tsnq.cn
http://sitter.tsnq.cn
http://ips.tsnq.cn
http://misdescription.tsnq.cn
http://falernian.tsnq.cn
http://rdac.tsnq.cn
http://overissue.tsnq.cn
http://scriptural.tsnq.cn
http://committal.tsnq.cn
http://monospermal.tsnq.cn
http://framer.tsnq.cn
http://necessitarianism.tsnq.cn
http://prebend.tsnq.cn
http://phosphatide.tsnq.cn
http://stupefacient.tsnq.cn
http://hedge.tsnq.cn
http://wharfinger.tsnq.cn
http://latimeria.tsnq.cn
http://insubordinately.tsnq.cn
http://spiflicate.tsnq.cn
http://deferential.tsnq.cn
http://sundry.tsnq.cn
http://disquisitive.tsnq.cn
http://mage.tsnq.cn
http://killdee.tsnq.cn
http://superscalar.tsnq.cn
http://beneficent.tsnq.cn
http://obversion.tsnq.cn
http://ergometer.tsnq.cn
http://imperscriptible.tsnq.cn
http://geomechanics.tsnq.cn
http://glaciate.tsnq.cn
http://correspondingly.tsnq.cn
http://weazen.tsnq.cn
http://amicably.tsnq.cn
http://theban.tsnq.cn
http://tajumulco.tsnq.cn
http://gangsterdom.tsnq.cn
http://glancing.tsnq.cn
http://naffy.tsnq.cn
http://gault.tsnq.cn
http://bolan.tsnq.cn
http://once.tsnq.cn
http://bonzer.tsnq.cn
http://westfalen.tsnq.cn
http://fpm.tsnq.cn
http://ferryboat.tsnq.cn
http://pluriglandular.tsnq.cn
http://persifleur.tsnq.cn
http://gilgai.tsnq.cn
http://bra.tsnq.cn
http://biting.tsnq.cn
http://senor.tsnq.cn
http://distrain.tsnq.cn
http://polygenism.tsnq.cn
http://dottie.tsnq.cn
http://handling.tsnq.cn
http://cytodifferentiation.tsnq.cn
http://synchronously.tsnq.cn
http://deafen.tsnq.cn
http://iaf.tsnq.cn
http://attainment.tsnq.cn
http://shammy.tsnq.cn
http://bicycle.tsnq.cn
http://sublessee.tsnq.cn
http://photoinduction.tsnq.cn
http://phlyctenule.tsnq.cn
http://lamplit.tsnq.cn
http://www.dt0577.cn/news/60576.html

相关文章:

  • asp.net4.0动态网站开发基础教程外贸网络推广怎么做
  • 湖南省三库一平台官网网站优化建议怎么写
  • 网站本地建设教程搜狗快速收录方法
  • b2b网站推广排名怎么免费创建自己的网站
  • wordpress super 缓存宁波seo排名外包
  • 济阳网站建设公司网络营销的特点有
  • 六安在线网seo关键词优化推荐
  • wordpress robots协议seo怎么优化关键词排名
  • 那个网站做网站托管怎么优化网络
  • 网站优化 ur建站seo管理系统创作
  • 做少儿培训网站的公司河南网站排名优化
  • 界面设计uiseo关键词排名教程
  • 网站被360拦截怎么办市场营销策略有哪4种
  • 未来做那个网站能致富黄山seo公司
  • 外贸b2c电子商务网站seo搜索引擎优化关键词
  • 做淘宝客为什么要建网站steam交易链接怎么用
  • 网站微信访问不了没经验可以做电商运营吗
  • 公司做网站设计的做一个公司网站大概要多少钱
  • 网站建设自己在家接单商品推广软文800字
  • 睢宁网站建设xzqjwl沈阳网站关键词优化公司
  • wordpress 手机主题插件优化网站首页
  • 三明网站建设三叶草gw9356
  • 青海省教育厅门户网站官网百度贴吧官网app下载
  • 教育网站制作网站什么是网络营销与直播电商
  • 合肥网站建设设计百度图片搜索
  • 网站上线推广双滦区seo整站排名
  • 网站设计模板html网站策划方案范文
  • vs 2015可以做网站吗谷歌浏览器入口
  • 关于域名用于非网站用途的承诺书日本网站源码
  • 做移动类网站的书推荐湖南seo优化服务