android randomaccessfile 优化,优化RandomAccessFile类后完整版代码

1 packagekbps.io;2 3 importjava.io.RandomAccessFile;4 importjava.io.File;5 importjava.io.IOException;6 importjava.io.FileNotFoundException;7 importjava.util.ResourceBundle;8 9 /**10 * 

Title: BufferedRandomAccessFile

11 * 

Description: this class provide Buffered Read & Write by extend RandomAccessFile

12 * 

Copyright: Copyright (c) 2002 Cui Zhixiang 

13 * 

Company: soho 

14 *@authorCui Zhixiang15 *@version1.0, 2002/10/1216 */17 18 publicclassBufferedRandomAccessFileextendsRandomAccessFile {19 20 staticResourceBundle res=ResourceBundle.getBundle("kbps.io.Res");21 privatestaticfinalintDEFAULT_BUFFER_BIT_LEN=10;22 privatestaticfinalintDEFAULT_BUFFER_SIZE=1<this.bufendpos) {117 this.flushbuf();118 this.seek(pos);119 120 if((posthis.bufendpos)) {121 thrownewIOException();122 }123 }124 this.curpos=pos;125 returnthis.buf[(int)(pos-this.bufstartpos)];126 }127 128 publicbooleanwrite(bytebw)throwsIOException {129 returnthis.write(bw,this.curpos);130 }131 132 publicbooleanappend(bytebw)throwsIOException {133 returnthis.write(bw,this.fileendpos+1);134 }135 136 publicbooleanwrite(bytebw,longpos)throwsIOException {137 138 if((pos>=this.bufstartpos)&&(pos<=this.bufendpos)) {//write pos in buf139 this.buf[(int)(pos-this.bufstartpos)]=bw;140 this.bufdirty=true;141 142 if(pos==this.fileendpos+1) {//write pos is append pos143 this.fileendpos++;144 this.bufusedsize++;145 }146 }else{//write pos not in buf147 this.seek(pos);148 149 if((pos>=0)&&(pos<=this.fileendpos)&&(this.fileendpos!=0)) {//write pos is modify file150 this.buf[(int)(pos-this.bufstartpos)]=bw;151 152 }elseif(((pos==0)&&(this.fileendpos==0))||(pos==this.fileendpos+1)) {//write pos is append pos153 this.buf[0]=bw;154 this.fileendpos++;155 this.bufusedsize=1;156 }else{157 thrownewIndexOutOfBoundsException();158 }159 this.bufdirty=true;160 }161 this.curpos=pos;162 returntrue;163 }164 165 publicvoidwrite(byteb[],intoff,intlen)throwsIOException {166 167 longwriteendpos=this.curpos+len-1;168 169 if(writeendpos<=this.bufendpos) {//b[] in cur buf170 System.arraycopy(b, off,this.buf, (int)(this.curpos-this.bufstartpos), len);171 this.bufdirty=true;172 this.bufusedsize=(int)(writeendpos-this.bufstartpos+1);//(int)(this.curpos - this.bufstartpos + len - 1);173 174 }else{//b[] not in cur buf175 super.seek(this.curpos);176 super.write(b, off, len);177 }178 179 if(writeendpos>this.fileendpos)180 this.fileendpos=writeendpos;181 182 this.seek(writeendpos+1);183 }184 185 publicintread(byteb[],intoff,intlen)throwsIOException {186 187 longreadendpos=this.curpos+len-1;188 189 if(readendpos<=this.bufendpos&&readendpos<=this.fileendpos ) {//read in buf190 System.arraycopy(this.buf, (int)(this.curpos-this.bufstartpos), b, off, len);191 }else{//read b[] size > buf[]192 193 if(readendpos>this.fileendpos) {//read b[] part in file194 len=(int)(this.length()-this.curpos+1);195 }196 197 super.seek(this.curpos);198 len=super.read(b, off, len);199 readendpos=this.curpos+len-1;200 }201 this.seek(readendpos+1);202 returnlen;203 }204 205 publicvoidwrite(byteb[])throwsIOException {206 this.write(b,0, b.length);207 }208 209 publicintread(byteb[])throwsIOException {210 returnthis.read(b,0, b.length);211 }212 213 publicvoidseek(longpos)throwsIOException {214 215 if((posthis.bufendpos)) {//seek pos not in buf216 this.flushbuf();217 218 if((pos>=0)&&(pos<=this.fileendpos)&&(this.fileendpos!=0)) {//seek pos in file (file length > 0)219 this.bufstartpos=pos&this.bufmask;220 this.bufusedsize=this.fillbuf();221 222 }elseif(((pos==0)&&(this.fileendpos==0))||(pos==this.fileendpos+1)) {//seek pos is append pos223 224 this.bufstartpos=pos;225 this.bufusedsize=0;226 }227 this.bufendpos=this.bufstartpos+this.bufsize-1;228 }229 this.curpos=pos;230 }231 232 publiclonglength()throwsIOException {233 returnthis.max(this.fileendpos+1,this.initfilelen);234 }235 236 publicvoidsetLength(longnewLength)throwsIOException {237 if(newLength>0) {238 this.fileendpos=newLength-1;239 }else{240 this.fileendpos=0;241 }242 super.setLength(newLength);243 }244 publiclonggetFilePointer()throwsIOException {245 returnthis.curpos;246 }247 248 privatelongmax(longa,longb) {249 if(a>b)returna;250 returnb;251 }252 253 publicvoidclose()throwsIOException {254 this.flushbuf();255 super.close();256 }257 258 publicstaticvoidmain(String[] args)throwsIOException {259 longreadfilelen=0;260 BufferedRandomAccessFile brafReadFile, brafWriteFile;261 262 brafReadFile=newBufferedRandomAccessFile("C:\\WINNT\\Fonts\\STKAITI.TTF");263 readfilelen=brafReadFile.initfilelen;264 brafWriteFile=newBufferedRandomAccessFile(".\\STKAITI.001","rw",10);265 266 bytebuf[]=newbyte[1024];267 intreadcount;268 269 longstart=System.currentTimeMillis();270 271 while((readcount=brafReadFile.read(buf))!=-1) {272 brafWriteFile.write(buf,0, readcount);273 }274 275 brafWriteFile.close();276 brafReadFile.close();277 278 System.out.println("BufferedRandomAccessFile Copy & Write File:"279 +brafReadFile.filename280 +"FileSize:"281 +java.lang.Integer.toString((int)readfilelen>>1024)282 +"(KB)"283 +"Spend:"284 +(double)(System.currentTimeMillis()-start)/1000285 +"(s)");286 287 java.io.FileInputStream fdin=newjava.io.FileInputStream("C:\\WINNT\\Fonts\\STKAITI.TTF");288 java.io.BufferedInputStream bis=newjava.io.BufferedInputStream(fdin,1024);289 java.io.DataInputStream dis=newjava.io.DataInputStream(bis);290 291 java.io.FileOutputStream fdout=newjava.io.FileOutputStream(".\\STKAITI.002");292 java.io.BufferedOutputStream bos=newjava.io.BufferedOutputStream(fdout,1024);293 java.io.DataOutputStream dos=newjava.io.DataOutputStream(bos);294 295 start=System.currentTimeMillis();296 297 for(inti=0; i>1024)308 +"(KB)"309 +"Spend:"310 +(double)(System.currentTimeMillis()-start)/1000311 +"(s)");312 }313 }314
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值