想查看一个文件的二进制码有很多工具可以用HEX的方式读文件,例如:Notepad++、UltraEdit、HexViewer等等。
软件采用命令交互模式,提示输入的命令有6个:
(1)帮助信息
在运行Java程序后就可以看见提示信息,我们直接输入想查看的文件名。
(2)错误提醒
随便输入一个不存在的文件,浏览器就会立刻提醒,该文件找不到,程序也会抛出异常。
(3)交互命令提示
输入一个存在的文本文件v.txt
时,就进入了HEX浏览环境,浏览环境一共8列数据,最下方还有页码提示和输入操作命令提示符。
(4)功能演示
浏览器还有上一次所输入“命令”的提示,方便使用者查看所做过的操作
【scanfile.java】
import java.io.*; //导入IO流
import java.util.Scanner; //导入Scanner类
public class scanfile {
public static void readfile() throws IOException{ //构造读文件函数
String BookName; //定义"书名"变量
int commandchar = 0; //定义"命令"变量
int pos = 0; //定义"位置指针"变量
int page = 1; //定义"页码"变量
System.out.println("Reading software 2018 ygaairspace Copyright(C)");
System.out.println("Usage: input anyfilename");
Scanner scan1 = new Scanner(System.in); //控制台输入"书名"
BookName = scan1.nextLine();
File file = new File(BookName); //创建File对象file
if(!(file.exists())){ //利用File类的exists方法判断文件是否存在
System.out.println("\nfile " +file.getName()+ " not found\n");
}
RandomAccessFile raf = new RandomAccessFile(BookName,"r");//创建RandomAccessFile对象raf
try{
System.out.println("\nCol | 0 1 2 3 4 5 6 7");
System.out.println("----|----------------------------------------------------");
raf.seek(pos); //定位文件指针所指位置
for(int i=0;i<14;i++){
for(int j=0;j<8;j++){
//System.out.println(" "+i+" "+bufr.read());
String str16 = Integer.toHexString(raf.read());
System.out.print(" "+str16);
pos++; //每次读取14*8=112个字符
if((j)%8 == 0){ //一行输出8个码字后换行
System.out.print("\n");
}
}
}
page = pos/112; //页码计算
//System.out.println("\n"+"dos2:"+dos);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("\n\n +page+">"); //输出页码
System.out.println("\n\ncommand key: q(uit) u(p) d(own) s(tart) e(nd) g(o pages)");
System.out.println("new command > ");
while(true){
try {
commandchar = System.in.read();//将输入的指令字符转化为ACSII码
} catch (IOException e) {
e.printStackTrace();
}
if(commandchar == 113){ //字符'q',退出程序
System.exit(0);
commandchar = 0;//重置"命令"变量
}
if(commandchar == 100){//字符'd',向下翻页
System.out.println("\nCol | 0 1 2 3 4 5 6 7");
System.out.println("----|----------------------------------------------------");
raf.seek(pos);
for(int i=0;i<14;i++){
for(int j=0;j<8;j++){
String str16 = Integer.toHexString(raf.read());
System.out.print(" "+str16);
pos++;
if((j)%8 == 0){
System.out.print("\n");
}
}
}
page = pos/112;
System.out.println("\n\n +page+">");
System.out.println("\nyour last command code is "+"'"+((char)commandchar)+"'");
System.out.println("command key: q(uit) u(p) d(own) s(tart) e(nd) g(o pages)");
System.out.println("new command > ");
commandchar = 0;
//System.out.println("\n"+"dos3:"+dos);
}
if(commandchar == 117){//字符'u',向上翻页
if(pos==112){ //判断文件指针是否在第1页,如在第1页,就再次输出第一页码字
raf.seek(0);
System.out.println("\nCol"+page+"| 0 1 2 3 4 5 6 7");
System.out.println("-----|-------------------------------------------------------------");
for(int i=0;i<14;i++){
for(int j=0;j<8;j++){
String str16 = Integer.toHexString(raf.read());
System.out.print(" "+str16);
pos++;
if((j)%8 == 0){
System.out.print("\n");
}
}
}
page = pos/112;
System.out.println("\n\n +page+">");
System.out.println("\nyour last command code is "+"'"+((char)commandchar)+"'");
System.out.println("command key: q(uit) u(p) d(own) s(tart) e(nd) g(o pages)");
System.out.println("new command > ");
}
else{
pos -= 224; //不在第1页,就减去两页的字符,共224个,将指针定位在上一页开头
System.out.println("\nCol | 0 1 2 3 4 5 6 7");
System.out.println("----|----------------------------------------------------");
//System.out.println("\n"+"dos4:"+dos);
raf.seek(pos);
for(int i=0;i<14;i++){
for(int j=0;j<8;j++){
String str16 = Integer.toHexString(raf.read());
System.out.print(" "+str16);
pos++;
if((j)%8 == 0){
System.out.print("\n");
}
}
}
page = pos/112;
System.out.println("\n\n +page+">");
System.out.println("\nyour last command code is "+"'"+((char)commandchar)+"'");
System.out.println("command key: q(uit) u(p) d(own) s(tart) e(nd) g(o pages)");
System.out.println("new command > ");
}
commandchar = 0;
}
if(commandchar == 115){ //字符's',读取第一页
pos = 0;//将位置指针定位在文件开头
raf.seek(pos);
System.out.println("\nCol | 0 1 2 3 4 5 6 7");
System.out.println("----|----------------------------------------------------");
for(int i=0;i<14;i++){
for(int j=0;j<8;j++){
String str16 = Integer.toHexString(raf.read());
System.out.print(" "+str16);
pos++;
if((j)%8 == 0){
System.out.print("\n");
}
}
}
page = pos/112;
System.out.println("\n\n +page+">");
System.out.println("\nyour last command code is "+"'"+((char)commandchar)+"'");
System.out.println("command key: q(uit) u(p) d(own) s(tart) e(nd) g(o pages)");
System.out.println("new command > ");
commandchar = 0;
}
if(commandchar == 101){ //字符'e',读取最后一页
long end = raf.length()-1; //获取文件末尾位置
pos = (int) (end - 112); //将位置指针移至最后一页的开头
System.out.println("\nCol | 0 1 2 3 4 5 6 7");
System.out.println("----|----------------------------------------------------");
//System.out.println("\n"+"dos4:"+dos);
raf.seek(pos);
for(int i=0;i<14;i++){
for(int j=0;j<8;j++){
String str16 = Integer.toHexString(raf.read());
System.out.print(" "+str16);
pos++;
if((j)%8 == 0){
System.out.print("\n");
}
}
}
page = pos/112;
System.out.println("\n\n +page+">");
System.out.println("\nyour last command code is "+"'"+((char)commandchar)+"'");
System.out.println("command key: q(uit) u(p) d(own) s(tart) e(nd) g(o pages)");
System.out.println("new command > ");
commandchar = 0;
}
if(commandchar == 103){ //字符'g',跳转至指定页
System.out.println("\npage? Example:1 2 3... >");
Scanner scan2 = new Scanner(System.in); //控制台输入"页码"
int jump = scan2.nextInt(); //读取输入的页码
pos = jump*112-112; //将位置指针定位在指定页的开头
System.out.println("\nCol | 0 1 2 3 4 5 6 7");
System.out.println("----|----------------------------------------------------");
raf.seek(pos);
for(int i=0;i<14;i++){
for(int j=0;j<8;j++){
String str16 = Integer.toHexString(raf.read());
System.out.print(" "+str16);
pos++;
if((j)%8 == 0){
System.out.print("\n");
}
}
}
page = pos/112;
System.out.println("\n\n +page+">");
System.out.println("\nyour last command code is "+"'"+((char)commandchar)+"'");
System.out.println("command key: q(uit) u(p) d(own) s(tart) e(nd) g(o pages)");
System.out.println("new command > ");
commandchar = 0;
}
}
}
public static void main(String[] args) throws IOException {
readfile(); //调用读文件函数
}
}