日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java读取文件 16进制_Java对文件的16进制读取和操作

發布時間:2025/3/20 java 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java读取文件 16进制_Java对文件的16进制读取和操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

大家可以參考一下源代碼的相關部分注釋,然后寫出自己的16進制處理程序。

有幾個重點地方:

16進制字符串-》10進制數 ???????? int input = Integer.parseInt("Str", 16)

10進制整數-》16進制字符串 ???????? String hex = Integer.toHexString(int)

文件讀取方法 ???????? 作為2進制文件直接讀取,一個byte為單位的讀取。 將來我還將在此基礎上制作Java版本的16進制編輯器,請大家多多支持。謝謝。 /** ?* RO Utility ?* Mainly used for: ?* 1.Double Open client ?* 2.Open Unlimited View ?*? 這是個樣本程序,是我針對游戲修改寫的。主要作用是將游戲文件用16進制打開,然后 ?* 修改相關的部分,然后保存。 ?*? ?* @author Ciro Deng(cdtdx@sohu.com) ?* @version 1.0 ?*/ package cn.edu.uestc.rotool; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.StringWriter; /** ?* RO Utility Mainly used for: 1.Double Open client 2.Open Unlimited View ?*? ?* @author Ciro Deng(cdtdx@sohu.com) ?* @version 1.0 ?*? ?*/ public class MainTool { ??? private final String RO_HOME = "D://Games//RO//"; //修改文件的路徑 ??? private final String FILE = "Ragexe"; //修改文件的主文件名 ??? private final String BAK_FILE = FILE + "_BAK.sp2"; //修改文件的備份擴展名 ??? private final String PATCH_FILE = FILE + ".sp2"; //修改文件的擴展名 ??? /** ???? * 進行16進制替換的規則定義 ??? ?* Pattern Array Example: pattern[0][0] = "Original Hex String"; 原16進制字符串 ??? ?* pattern[0][1] = "New Hex String"; 要替換的16進制字符串 ??? ?*/ ??? private final String[][] pattern = { ??? ??? ??? { "85C074095F5E33C05B8BE55DC3", "85C0EB095F5E33C05B8BE55DC3" }, ??? ??? ??? { "85C0740E5F5EB801000000", "85C0EB0E5F5EB801000000" }, // Double ??? ??? ??? // Open ??? ??? ??? { "000066430000C843", "0000004300008644" } // Umlimited View ??? }; ??? /** ???? * 備份文件恢復 ??? ?* ture the backup file into real file ??? ?*? ??? ?*/ ??? public void restore() { ??? ??? if (isExistBackup()) { ??? ??? ??? new File(RO_HOME + PATCH_FILE).delete(); ??? ??? ??? new File(RO_HOME + BAK_FILE) ??? ??? ??? ??? ??? .renameTo(new File(RO_HOME + PATCH_FILE)); ??? ??? ??? System.out.println("[----------------Restore file OK!--------------------]"); ??? ??? } else { ??? ??? ??? System.out.println("Backup file does not exist!"); ??? ??? ??? System.exit(0); ??? ??? } ??? } ??? public void init() { //初始化操作 ??? ??? if (new File(RO_HOME + PATCH_FILE).exists()) { ??? ??? ??? System.out ??? ??? ??? ??? ??? .println("[-------------Initialize original file OK!-----------]"); ??? ??? } else { ??? ??? ??? System.out.println("File is not Existed! Please restore it first!"); ??? ??? } ??? ??? // backup original file ??? ??? if (!isExistBackup()) { ??? ??? ??? new File(RO_HOME + PATCH_FILE) ??? ??? ??? ??? ??? .renameTo(new File(RO_HOME + BAK_FILE)); ??? ??? } ??? ??? System.out ??? ??? ??? ??? .println("[---------------Please choose your action------------]"); ??? ??? System.out.println("1:Modify double open and unlimited view mode!"); ??? ??? System.out.println("2:Restore original mode!"); ??? ??? System.out.println("Please input 1 or 2 and Enter:"); ??? } ??? public void success() { //成功操作提示 ??? ??? System.out.println(); ??? ??? System.out ??? ??? ??? ??? .println("[-------------Patch file OK! Have fun with RO!-------]"); ??? } ??? /** ???? * 進行16進制替換 ??? ?* replace input Hex String with defined pattern ??? ?*? ??? ?* @param original ??? ?* @return ??? ?*/ ??? public String replace(String original) { ??? ??? for (int i = 0; i < pattern.length; i++) { ??? ??? ??? original = original.replaceAll(pattern[i][0].toLowerCase(), ??? ??? ??? ??? ??? pattern[i][1].toLowerCase()); ??? ??? } ??? ??? return original; ??? } ??? /** ???? * 將文件讀取為16進制String ??? ?* Read original File and transfer it into Hex String ??? ?*? ??? ?* @return ??? ?* @throws IOException ??? ?*/ ??? public String readOriginal2Hex() throws IOException { ??? ??? FileInputStream fin = new FileInputStream(new File(RO_HOME + BAK_FILE)); ??? ??? StringWriter sw = new StringWriter(); ??? ??? int len = 1; ??? ??? byte[] temp = new byte[len]; ?????? /*16進制轉化模塊*/ ??? ??? for (; (fin.read(temp, 0, len)) != -1;) { ??? ??? ??? if (temp[0] > 0xf && temp[0] <= 0xff) {? ??? ??? ??? ??? sw.write(Integer.toHexString(temp[0])); ??? ??? ??? } else if (temp[0] >= 0x0 && temp[0] <= 0xf) {//對于只有1位的16進制數前邊補“0” ??? ??? ??? ??? sw.write("0" + Integer.toHexString(temp[0])); ??? ??? ??? } else { //對于int<0的位轉化為16進制的特殊處理,因為Java沒有Unsigned int,所以這個int可能為負數 ??? ??? ??? ??? sw.write(Integer.toHexString(temp[0]).substring(6)); ??? ??? ??? } ??? ??? } ??? ??? return sw.toString(); ??? } ??? /** ???? * 將替換后的16進制字符串寫回文件 ??? ?* write replaced original String to file ??? ?*? ??? ?* @param replaced ??? ?* @throws NumberFormatException ??? ?* @throws IOException ??? ?*/ ??? public void writeNew2Binary(String replaced) throws NumberFormatException, ??? ??? ??? IOException { ??? ??? FileOutputStream fout = new FileOutputStream(RO_HOME + PATCH_FILE); ??? ??? for (int i = 0; i < replaced.length(); i = i + 2) { ??? ??? ??? fout.write(Integer.parseInt(replaced.substring(i, i + 2), 16)); ??? ??? } ??? } ??? /** ??? ?* test direct output string to file ??? ?*? ??? ?* @param temp ??? ?* @throws IOException ??? ?*/ ??? public void writeTest(String temp) throws IOException { ??? ??? FileOutputStream fout = new FileOutputStream(RO_HOME + "test.txt"); ??? ??? for (int i = 0; i < temp.length(); i++) { ??? ??? ??? fout.write(temp.charAt(i)); ??? ??? } ??? } ??? /** ??? ?* check if the backup file exists ??? ?*? ??? ?* @return ??? ?*/ ??? public boolean isExistBackup() { ??? ??? return new File(RO_HOME + BAK_FILE).exists(); ??? } ??? /** ???? * 主要操作方法,組織工作流程 ??? ?* Main process method ??? ?*? ??? ?* @throws IOException ??? ?*/ ??? public void patch() throws IOException { ??? ??? // init ??? ??? init(); ?????? //輸入參數: ?????? //1:進行查找替換 ?????? //2:將備份文件恢復 ??? ??? String input = new BufferedReader(new InputStreamReader(System.in)) ??? ??? ??? ??? .readLine(); ??? ??? if (input.equals("1")) { ??? ??? ??? String temp = null; ??? ??? ??? temp = readOriginal2Hex(); ??? ??? ??? temp = replace(temp); ??? ??? ??? writeNew2Binary(temp); ??? ??? ??? success(); ??? ??? } else if (input.equals("2")) { ??? ??? ??? restore(); ??? ??? } else { ??? ??? ??? System.out.println("Bad input parameter!"); ??? ??? ??? System.exit(0); ??? ??? } ??? } ??? /** ???? * Main方法 ??? ?* main ??? ?*? ??? ?* @param args ??? ?* @throws IOException ??? ?*/ ??? public static void main(String[] args) throws IOException { ??? ??? MainTool tool = new MainTool(); ??? ??? tool.patch(); ??? } }

總結

以上是生活随笔為你收集整理的java读取文件 16进制_Java对文件的16进制读取和操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。