我是java編程的初學者,我試圖在java中創建十六進制查看器,我的IDE是Netbeans。以下是代碼。Java中的十六進制查看器
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import static java.lang.System.out;
import javax.swing.JFileChooser;
public class hope {
public static void main(String[] args) throws IOException {
JFileChooser open = new JFileChooser();
open.showOpenDialog(null);
File f = open.getSelectedFile();
InputStream is = new FileInputStream(f);
int bytesCounter = 0;
int value = 0;
StringBuilder sbHex = new StringBuilder();
StringBuilder sbResult = new StringBuilder();
while ((value = is.read()) != -1) {
//convert to hex value with "X" formatter
sbHex.append(String.format("%02X ", value));
//if 16 bytes are read, reset the counter,
//clear the StringBuilder for formatting purpose only.
if (bytesCounter == 15) {
sbResult.append(sbHex).append("\n");
sbHex.setLength(0);
bytesCounter = 0;
} else {
bytesCounter++;
}
}
//if still got content
if (bytesCounter != 0) {
//add spaces more formatting purpose only
for (; bytesCounter < 16; bytesCounter++) {
//1 character 3 spaces
sbHex.append(" ");
}
sbResult.append(sbHex).append("\n");
}
out.print(sbResult);
is.close();
}
}
的問題是: 1.它不讀取速度不夠快的文件,「這需要一分鐘閱讀200KB的文件」 2.它提供了「內存不足」時,我嘗試了錯誤大文件例如80mb
我想要它做什麼: 1.以秒爲單位顯示所有的十六進制代碼「讀取並顯示任意大小的文件的十六進制」 2.讀取任何大小的文件,沒有錯誤代碼。
問題:
什麼我需要改變或在我的代碼添加到實現上述「我想要它做」?
看看這個[示例](http://www.gubatron.com/blog/2009/04/20/how-to-讓-A-快速髒hexviewer更新的/)。 – DevilsHnd
謝謝@DevilsHnd,我檢查了一下,但代碼是自2009年以來創建的,我認爲有很多改進要做,並且出於什麼原因我無法實現它,請記住我是新手。沒有評論的Java代碼使我很難理解和編輯。 –
爲什麼不使用'out.print()',而不是'sbHex.append()'和'sbResult.append()'?也許使用StringBuilder是不必要的? – gregn3