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

wordpress翻译教程/当阳seo外包

wordpress翻译教程,当阳seo外包,盐城网站优化,织梦做视频网站本文是基于Linux环境运行,读者阅读前需要具备一定Linux知识 RandomAccessFile是Java输入/输出流体系中功能最丰富的文件内容访问类,既可以读取文件内容,也可以向文件输出数据。与普通的输入/输出流不同的是,RandomAccessFile支持跳…

本文是基于Linux环境运行,读者阅读前需要具备一定Linux知识

RandomAccessFile是Java输入/输出流体系中功能最丰富的文件内容访问类,既可以读取文件内容,也可以向文件输出数据。与普通的输入/输出流不同的是,RandomAccessFile支持跳到文件任意位置读写数据,RandomAccessFile对象包含一个记录指针,用以标识当前读写处的位置,当程序创建一个新的RandomAccessFile对象时,该对象的文件记录指针对于文件头(也就是0处),当读写n个字节后,文件记录指针将会向后移动n个字节。除此之外,RandomAccessFile可以自由移动该记录指针

RandomAccessFile包含两个方法来操作文件记录指针:

  • long getFilePointer():返回文件记录指针的当前位置
  • void seek(long pos):将文件记录指针定位到pos位置

RandomAccessFile类在创建对象时,除了指定文件本身,还需要指定一个mode参数,该参数指定RandomAccessFile的访问模式,该参数有如下四个值:

  • r:以只读方式打开指定文件。如果试图对该RandomAccessFile指定的文件执行写入方法则会抛出IOException
  • rw:以读取、写入方式打开指定文件。如果该文件不存在,则尝试创建文件
  • rws:以读取、写入方式打开指定文件。相对于rw模式,还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备,默认情形下(rw模式下),是使用buffer的,只有cache满的或者使用RandomAccessFile.close()关闭流的时候儿才真正的写到文件
  • rwd:与rws类似,只是仅对文件的内容同步更新到磁盘,而不修改文件的元数据

代码1-1

import java.io.IOException;
import java.io.RandomAccessFile;public class RandomAccessRead {public static void main(String[] args) {if (args == null || args.length == 0) {throw new RuntimeException("请输入路径");}RandomAccessFile raf = null;try {raf = new RandomAccessFile(args[0], "r");System.out.println("RandomAccessFile的文件指针初始位置:" + raf.getFilePointer());raf.seek(100);byte[] bbuf = new byte[1024];int hasRead = 0;while ((hasRead = raf.read(bbuf)) > 0) {System.out.print(new String(bbuf, 0, hasRead));}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {if (raf != null) {raf.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

 

代码1-1运行结果:

root@lejian:/home/software/.io# cat article 
Unexpected Benefits of Drinking Hot Water
Reasons To Love An Empowered Woman
Reasons Why It’s Alright To Feel Lost In A Relationship
Signs You’re Uber Smart Even If You Don’t Appear to Be
Differences Between Positive People And Negative People
Sex Before Marriage: 5 Reasons Every Couple Should Do It
root@lejian:/home/software/.io# java RandomAccessRead article 
RandomAccessFile的文件指针初始位置:0
ght To Feel Lost In A Relationship
Signs You’re Uber Smart Even If You Don’t Appear to Be
Differences Between Positive People And Negative People
Sex Before Marriage: 5 Reasons Every Couple Should Do It

 

代码1-2使用RandomAccessFile来追加文件内容,RandomAccessFile先获取文件的长度,再将指针移到文件的末尾,再将要插入的内容插入到文件

代码1-2

import java.io.IOException;
import java.io.RandomAccessFile;public class RandomAccessWrite {public static void main(String[] args) {if (args == null || args.length == 0) {throw new RuntimeException("请输入路径");}RandomAccessFile raf = null;try {String[] arrays = new String[] { "Hello Hadoop", "Hello Spark", "Hello Hive" };raf = new RandomAccessFile(args[0], "rw");raf.seek(raf.length());raf.write("追加内容:\n".getBytes());for (String arr : arrays) {raf.write((arr + "\n").getBytes());}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {if (raf != null) {raf.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

 

代码1-2运行结果:

root@lejian:/home/software/.io# cat text 
Hello spring
Hello Hibernate
Hello Mybatis
root@lejian:/home/software/.io# java RandomAccessWrite text 
root@lejian:/home/software/.io# cat text 
Hello spring
Hello Hibernate
Hello Mybatis
追加内容:
Hello Hadoop
Hello Spark
Hello Hive

 

RandomAccessFile如果向文件的指定的位置插入内容,则新输出的内容会覆盖文件中原有的内容。如果需要向指定位置插入内容,程序需要先把插入点后面的内容读入缓冲区,等把需要的插入数据写入文件后,再将缓冲区的内容追加到文件后面,代码1-3为在文件指定位置插入内容

代码1-3

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;public class InsertContent {public static void main(String[] args) {if (args == null || args.length != 3) {throw new RuntimeException("请分别输入操作文件、插入位置和插入内容");}FileInputStream fis = null;FileOutputStream fos = null;RandomAccessFile raf = null;try {raf = new RandomAccessFile(args[0], "rw");File tmp = File.createTempFile("tmp", null);tmp.deleteOnExit();fis = new FileInputStream(tmp);fos = new FileOutputStream(tmp);raf.seek(Long.parseLong(args[1]));byte[] bbuf = new byte[64];int hasRead = 0;while ((hasRead = raf.read(bbuf)) > 0) {fos.write(bbuf, 0, hasRead);}raf.seek(Long.parseLong(args[1]));raf.write("\n插入内容:\n".getBytes());raf.write((args[2] + "\n").getBytes());while ((hasRead = fis.read(bbuf)) > 0) {raf.write(bbuf, 0, hasRead);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {if (fis != null) {fis.close();}if (fos != null) {fos.close();}if (raf != null) {raf.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

 

代码1-3运行结果:

root@lejian:/home/software/.io# cat text 
To love oneself is the beginning of a lifelong romance.
Change your life today. Don't gamble on the future, act now, without delay.
Health is the thing that makes you feel that now is the best time of the year. 
The very essence of romance is uncertainty.
Your time is limited, so don’t waste it living someone else’s life.
root@lejian:/home/software/.io# java InsertContent text 100 "Success covers a multitude of blunders."
root@lejian:/home/software/.io# cat text 
To love oneself is the beginning of a lifelong romance.
Change your life today. Don't gamble on the 
插入内容:
Success covers a multitude of blunders.
future, act now, without delay.
Health is the thing that makes you feel that now is the best time of the year. 
The very essence of romance is uncertainty.
Your time is limited, so don’t waste it living someone else’s life.

 

转载于:https://www.cnblogs.com/baoliyan/p/6225842.html

http://www.lbrq.cn/news/2343457.html

相关文章:

  • 呼叫中心网站建设/如何注册网址
  • 政府网站谁来做/四川二级站seo整站优化排名
  • 创新网站设计/全国培训机构排名前十
  • 网站建设行业怎么样/seo优化中商品权重主要由什么决定
  • 南宁推广软件/武汉seo优化服务
  • 怎么做网购网站/合肥seo优化排名公司
  • 国内做视频的网站有哪些/外链发布软件
  • 帝国网站数据库配置文件/2345网址中国最好
  • 云南商城网站建设/关键词你们懂的
  • 沈阳电子商务网站建设/百度seo推广
  • 新疆建设监理协会网站/百度灰色关键词技术
  • 搭建php网站环境/地推的方法和技巧
  • 做跨境网站注意事项/搜索引擎优化的具体操作
  • 网站后台换图片/bing收录提交
  • 怎么做信息采集的网站/深圳纯手工seo
  • 网站建设要学会编程吗/星链友店
  • wordpress 制作首页模板/简单网站建设优化推广
  • 南京html5网站建设/百度数据库
  • 公司做网站流程/友情链接是什么
  • 网站建设开票内容是什么意思/seochinazcom
  • 广告网站怎么设计制作/职业教育培训机构排名前十
  • 广东企业网站制作/关键词收录
  • java web网站开发流程/网站提交收录软件
  • 网站推广信息怎么做/百度一下官网搜索引擎
  • 软件测试培训需要多久/百度快速seo软件
  • 做类图的网站/软文生成器
  • 做网站在线支付系统多少钱/杭州seo公司服务
  • 做健康类网站怎么备案/网络营销的发展现状及趋势
  • 昆山花桥做网站/今日微博热搜榜前十名
  • 旅游网站建设报价单/网站流量统计系统
  • 【PTA数据结构 | C语言版】根据层序序列重构二叉树
  • cursor使用mcp连接mysql数据库,url方式
  • eVTOL分布式电推进(DEP)适航审定探究
  • 邮件伪造漏洞
  • 分布式一致性协议
  • 新手向:Python自动化办公批量重命名与整理文件系统