《Java》Java實現一個簡單的“檢視檔案的二進位制碼瀏覽器”
阿新 • • 發佈:2018-11-12
一、檢視檔案的二進位制碼
想檢視一個檔案的二進位制碼有很多工具可以用HEX的方式讀檔案,例如:Notepad++、UltraEdit、HexViewer等等。
Notepad++的HEX-Editor:
二、DIY瀏覽器
1.初步實現的瀏覽功能:
軟體採用命令互動模式,提示輸入的命令有6個:
- ‘q’ 退出程式
- ‘u’ 向上翻頁
- ‘d’ 向下翻頁
- ‘s’ 跳至首頁
- ‘e’ 跳至尾頁
- ‘g’ 跳至指定頁
2.瀏覽器介紹
(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"+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"+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"+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"+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"+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"+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"+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(); //呼叫讀檔案函式
}
}