您的位置:首页 > 运维架构 > Linux

分区结构分析: linux系统可查看某一磁盘结构,效果类似window的winhex。

2016-10-16 20:26 337 查看
package Task2;

import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Task2 extends JFrame {
static JTextArea ta = new JTextArea();
static JTextField tf1 = new JTextField();
static int count = 0;
public Task2(String title) throws IOException{
super(title);
JScrollPane jp = new JScrollPane(ta);
jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jp.setViewportView(ta);
jp.setPreferredSize(new Dimension(700,600));
add(jp);
setSize(450, 600);
setLocation(150, 150);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Button button1 = new Button("选择文件");
button1.setBounds(new Rectangle(20, 20, 70, 25));
button1.addActionListener(new ChooseFile());
tf1.setBounds(new Rectangle(100, 20, 200, 25));
ta.setBackground(Color.white);
ta.setText(" ");
ta.setEditable(true);
ta.setLineWrap(true);
ta.setBounds(new Rectangle(20, 50, 400, 500));
add(ta);
add(tf1);
add(button1);
setVisible(true);
for(int i = 0 ; i <= 9 ; i++){
ta.append('0' + String.valueOf(i)+"   ");
}
for(int i = 10 ; i < 16 ; i++ ){
ta.append(i+"  ");
}
Task2.ta.append("\n");
Task2.ta.append("\n");
}

public static void main(String[] args) throws IOException {
Task2 t = new  Task2("文件分析");
}
}

class ChooseFile implements ActionListener{
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.showOpenDialog(null);

File[] files = fileChooser.getSelectedFiles();
for(int i = 0 ; i < files.length ; i++){
try {
startThread(files[i]);

} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
if(files.length == 1){
Task2.tf1.setText(files[0].getAbsolutePath());
}else
Task2.tf1.setText(files[0].getParent());
}

public void startThread(File file) throws InterruptedException{
java.lang.Runnable runner = new Runnable() {
public void run() {
String  file1 = file.toString();
try {
FileInputStream stream = new FileInputStream(file1);
int c = 0 ;
while((c = stream.read()) != -1){
int v = c&0xFF;
String string1 = Integer.toHexString(v);
Task2.count++;
if(string1.length() < 2) string1 = '0' + string1;
Task2.ta.append(" "+string1+"  ");
if(Task2.count % 16 ==0 ){
Task2.ta.append("\n");
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
new Thread(runner).start();
new Thread(runner).sleep(100);
}

public void ReadAllFiles(String path) {
File f = new File(path);
File[] files = f.listFiles();

}
}

难点在于二进制转换到16进制,代码中已经说明。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: