求段简单JAVA的代码

import java.io.*;public class Test{ public static void main(String s[]){ byte[] b1=new byte[3];byte[] b2=new byte[3];try{ File f=new File("F:\\11.txt");RandomAccessFile raf=new RandomAccessFile(f,"rw");long len=raf.length();raf.read(b1,0,3);String str=bytes...
求段简单JAVA的代码
import java.io.*;
public class Test{
public static void main(String s[]){

byte[] b1=new byte[3];
byte[] b2=new byte[3];
try{
File f=new File("F:\\11.txt");
RandomAccessFile raf=new RandomAccessFile(f,"rw");
long len=raf.length();
raf.read(b1,0,3);
String str=bytesToHexString(b1);
System.out.println("前三个字节哈希码是:"+b1.toString());
System.out.println("转换为十六进制后是:"+str);
raf.seek(len-3);
//raf.read(b2,(int)(len-3),3);
b2[0]=raf.readByte();
b2[1]=raf.readByte();
b2[2]=raf.readByte();
str=bytesToHexString(b2);
System.out.println("后三个自己哈希码是:"+b2.toString());
System.out.println("转换为十六进制后是:"+str);
}catch(Exception e){System.out.println("error");}

}
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
}

主要的除了转16进制,就是用RandomAccessFile的seek来快速定位倒数的三个字节的位置。还有问题的话,baiduhi我,随时在线2010-07-23
前三位好读吧,后三位貌似有一个判断文件结尾的方法,不行了就用指针判断
然后读取后三位,转换16进制的网上有现成的,你找一下就ok了2010-07-23
二楼强,,正解。2010-07-23
mengvlog 阅读 350 次 更新于 2025-10-30 02:27:43 我来答关注问题0
檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部