首页 > 编程知识 正文

java生成Access,java从文件中读取数据

时间:2023-05-04 16:17:56 阅读:44826 作者:730

使用RandomAccessFile类可以自由灵活地处理文件。

要处理文件内容,可以自定义两种方法。

import java.io.*; import java.util.*; public class filehandler { privatestringbuffertempstring=new string buffer (' '; //读取文档内容的publiclongreadfilecontent (randomaccessfilefile,long pointer ) throwsioexception ) longreadtotallen=0; try{//定位指针file.seek(pointer; //缓冲器var buffer=new byte[1024]; int readnum=0; //while(readnum=file.read ) buffer )0) string read text=new string (buffer,0,readnum ) ); this.tempstring.append (读取文本; //System.out.println (读取文件内容(读取文本)文件指针(file.getFilePointer ) ); readtotallen =readnum; }catch(ioexceptione ) {e.printStackTrace ); }return readtotallen; } publicbooleanwritefilecontent (randomaccessfilefile,long pointer,filehandler.writefilemoduleisappend,字符串内容) byte [ ] content byte=content.getbytes (),用于处理try{//写入文件的内容; //指定位置以读取文件内容缓存,并使用用于文件模式的this.readfile content (文件, 插入pointer )//添加/插入内容if (is append.com Pareto filehandler.writefile module.append )==0||is append.com file.write (this.tempstring.tostring ().getBytes ) ); //文档内容if (is append.com Pareto (filehandler.writefile module.cover )==0) ) file.setlength (pointer ); //已清除指定位置的内容file.write(contentbyte ); }catch(ioexceptione ) {e.printStackTrace ); 写入文件=假; }返回写入文件; } publicstaticvoidmain (字符串[ ] args ) throwsioexception (try ) scanners can=new scanner (system.in ); varfile=newrandomaccessfile (' test.txt ',' rw ' ); 以//rw模式打开文件() ({var filehandler=new FileHandler ) ); varcurpointerposition=file.length (; scan.useDelimiter((n ); while(scan.Hasnext () ) /通过键盘循环输入文件String content=scan.next ); if(content.indexof ) ' exit'-1 ) break; //键盘输入exit退出//写文件内容varis writefile=filehandler.writefile content (文件,curpointerposition,filehandler.wriritefile ) 获取//文件的当前指针位置(else ) system.out.println )文件写入错误! ' ); //throwsioexception('111 ); 布雷克; }}System.out.println ('文件大小: ' file.length ) ); }catch(ioexceptione ) {e.printStackTrace ); //文件写入模式添加内部枚举类APPEND、覆盖、插入publicstaticenumwritefilemodule { append,COVER,INSERT; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。