这是我第一次在这里提问,我对JAVA知之甚少。
我需要将HexOld方法的输出转换为Extract方法。我尝试使用getter和setter但似乎没有用。下面是我的代码
public HexOld() throws FileNotFoundException, IOException{
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
CapturePane capturePane = new CapturePane();
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton start = new JButton ("Start");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
frame.add(panel);
frame.setVisible(true);
panel.add(capturePane);
panel.setSize(500,700);
panel.add(start);
start.setLocation(10,20);
start.setSize(40,40);
start.setVisible(true);
start.setLocation(0, 0);
start.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
Extract();
}
});
PrintStream ps = System.out;
System.setOut(new PrintStream(new StreamCapturer("", capturePane, ps)));
StringBuilder sb = new StringBuilder();
try (BufferedInputStream is = new BufferedInputStream(new FileInputStream("C:/Temp/images.jpg"))) {
for (int b; (b = is.read()) != -1;) {
String s = Integer.toHexString(b).toUpperCase();
if (s.length() == 1) {
sb.append('0');
}
sb.append(s).append(' ');
}
int idx = sb.length() - 48;
while (idx > 0)
{
sb.insert(idx, "-");
idx = idx - 48;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String delimiter = "-";
String[] temp;
temp = sb.toString().split(delimiter);
for(int i =0; i < temp.length ; i++)
System.out.println(temp[i]);
}
});
我希望将ths方法的输出转换为此方法
public void Extract(){
String str ="What should i put here to get the output from method above?" ;
Pattern pattern = Pattern.compile("(FF D8.*?FF D9)" );
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
//System.out.println(matcher.group(1));
JOptionPane.showMessageDialog(null,matcher.group(1));
}