像UE一样将二进制文件转换为16进制显示
2016-04-28 13:28
423 查看
package test;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ShowBinary extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public ShowBinary(){
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setLayout(null);
int width = 420;
int height = 160;
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
int x = (d.width - width) / 2;
int y = (d.height - height) / 2;
this.setLocation(x, y);
this.setSize(width, height);
this.setTitle("转换二进制文件");
this.setResizable(false);
JLabel label = new JLabel("二进制文件:");
this.add(label);
label.setBounds(25, 10, 80, 30);
final JTextField text = new JTextField();
this.add(text);
text.setBounds(100, 10, 200, 25);
text.setEditable(false);
final JFileChooser file = new JFileChooser();
file.setFileSelectionMode(JFileChooser.FILES_ONLY);
this.add(file);
JButton btn = new JButton("选择");
btn.setBounds(310, 10, 60, 25);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int state = file.showOpenDialog(null);
if(state!=1){
File f = file.getSelectedFile();
text.setText(f.getAbsolutePath());
}
}
});
this.add(btn);
JLabel label1 = new JLabel("可看二进制:");
this.add(label1);
label1.setBounds(25, 50, 80, 25);
final JTextField text1 = new JTextField();
text1.setEditable(false);
this.add(text1);
text1.setBounds(100, 50, 200, 25);
final JFileChooser file1 = new JFileChooser();
file1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
this.add(file1);
JButton btn1 = new JButton("选择");
btn1.setBounds(310, 50, 60, 25);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int state = file1.showOpenDialog(null);
if(state!=1){
File f = file1.getSelectedFile();
text1.setText(f.getAbsolutePath());
}
}
});
this.add(btn1);
JButton sure = new JButton("生成");
sure.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filepath = text.getText();
if(filepath==null||(filepath=filepath.trim()).length()==0){
return;
}
int last = filepath.length();
if(filepath.contains(".")){
last = filepath.lastIndexOf(".");
}
String name = filepath.substring(filepath.lastIndexOf(File.separator), last);
String defaultPath = filepath.substring(0, filepath.lastIndexOf(File.separator));
String path = text1.getText();
if(path!=null&&path.trim().length()>0){
defaultPath = path.trim();
}
String targetName = defaultPath + name + ".txt";
create(filepath, targetName);
}
});
this.add(sure);
sure.setBounds(100, 90, 60, 30);
JButton close = new JButton("关闭");
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
this.add(close);
close.setBounds(200, 90, 60, 30);
this.setVisible(true);
}
private void create(String source, String target){
FileOutputStream os = null;
try{
os = new FileOutputStream(target);
byte[] bts = Files.readAllBytes(Paths.get(source));
int i = 1;
for(byte b:bts){
int h = 0xf&(b>>>4);
int l = 0xf&b;
String s = (Integer.toHexString(h)+Integer.toHexString(l)).toUpperCase()+" ";
os.write(s.getBytes());
if(i%16==0){
os.write("\n".getBytes());
}
i++;
}
os.flush();
}catch (Exception e) {
// skip
}finally{
if(os!=null){
try{
os.close();
}catch (Exception e) {
}
}
}
}
public static void main(String[] args) {
new ShowBinary();
}
}
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ShowBinary extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public ShowBinary(){
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setLayout(null);
int width = 420;
int height = 160;
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
int x = (d.width - width) / 2;
int y = (d.height - height) / 2;
this.setLocation(x, y);
this.setSize(width, height);
this.setTitle("转换二进制文件");
this.setResizable(false);
JLabel label = new JLabel("二进制文件:");
this.add(label);
label.setBounds(25, 10, 80, 30);
final JTextField text = new JTextField();
this.add(text);
text.setBounds(100, 10, 200, 25);
text.setEditable(false);
final JFileChooser file = new JFileChooser();
file.setFileSelectionMode(JFileChooser.FILES_ONLY);
this.add(file);
JButton btn = new JButton("选择");
btn.setBounds(310, 10, 60, 25);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int state = file.showOpenDialog(null);
if(state!=1){
File f = file.getSelectedFile();
text.setText(f.getAbsolutePath());
}
}
});
this.add(btn);
JLabel label1 = new JLabel("可看二进制:");
this.add(label1);
label1.setBounds(25, 50, 80, 25);
final JTextField text1 = new JTextField();
text1.setEditable(false);
this.add(text1);
text1.setBounds(100, 50, 200, 25);
final JFileChooser file1 = new JFileChooser();
file1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
this.add(file1);
JButton btn1 = new JButton("选择");
btn1.setBounds(310, 50, 60, 25);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int state = file1.showOpenDialog(null);
if(state!=1){
File f = file1.getSelectedFile();
text1.setText(f.getAbsolutePath());
}
}
});
this.add(btn1);
JButton sure = new JButton("生成");
sure.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filepath = text.getText();
if(filepath==null||(filepath=filepath.trim()).length()==0){
return;
}
int last = filepath.length();
if(filepath.contains(".")){
last = filepath.lastIndexOf(".");
}
String name = filepath.substring(filepath.lastIndexOf(File.separator), last);
String defaultPath = filepath.substring(0, filepath.lastIndexOf(File.separator));
String path = text1.getText();
if(path!=null&&path.trim().length()>0){
defaultPath = path.trim();
}
String targetName = defaultPath + name + ".txt";
create(filepath, targetName);
}
});
this.add(sure);
sure.setBounds(100, 90, 60, 30);
JButton close = new JButton("关闭");
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
this.add(close);
close.setBounds(200, 90, 60, 30);
this.setVisible(true);
}
private void create(String source, String target){
FileOutputStream os = null;
try{
os = new FileOutputStream(target);
byte[] bts = Files.readAllBytes(Paths.get(source));
int i = 1;
for(byte b:bts){
int h = 0xf&(b>>>4);
int l = 0xf&b;
String s = (Integer.toHexString(h)+Integer.toHexString(l)).toUpperCase()+" ";
os.write(s.getBytes());
if(i%16==0){
os.write("\n".getBytes());
}
i++;
}
os.flush();
}catch (Exception e) {
// skip
}finally{
if(os!=null){
try{
os.close();
}catch (Exception e) {
}
}
}
}
public static void main(String[] args) {
new ShowBinary();
}
}
相关文章推荐
- Xcode 开发错误点链接错误之file was built for archive which is not the architecture being linked (i386)
- execute、executeQuery和executeUpdate之间的区别
- UI分析之石家庄铁道大学官网
- UI招聘之我思考
- iOS开发UI篇—UITabBarController简单介绍
- UIPickView的使用和
- UIResponder
- Java String、StringBuffer、StringBuilder深度剖析
- PYQT4 UI 线程分离
- UI测试导入Espresso时的冲突
- 62. Unique Paths && 63. Unique Paths II
- UILabel显示html文本
- MenuItem创建注意事项
- iOS9中UIDatePicker的用法
- <OJ_Sicily>Hanoi_Tower_Sequence
- 数据库中创建unique唯一约束
- String,StringBuffer与StringBuilder的区别
- Android蓝牙4.0API-类-BluetoothGattCharacteristic
- POJ 2299 Ultra-QuickSort(逆序数)
- iOS 之UIButton 使用详解