class openFileListener implements ActionListener
{
private String[] hex;
private byte[] data;
public String[] getHexArray() {
return hex;
}
public void actionPerformed(ActionEvent event)
{
// Select file and show the name of file
wavFileImport.showOpenDialog(openButton);
File file = wavFileImport.getSelectedFile();
fileNameField.setText(file.getName());
//Reading file
String path = file.getAbsolutePath();
Path pathname = Paths.get(path);
try {
// read bytedata
data = Files.readAllBytes(pathname);
Path path2 = Paths.get("output.txt");
Files.write(path2, data);
//converts it to 2 digit hex
hex = new String[data.length];
for(int i = 0; i < data.length; i++) {
hex[i]= String.format("%02X", data[i]);
}
PrintWriter pr = new PrintWriter("output2.txt");
for(int i = 0; i < hex.length; i++) {
pr.println(hex[i]);
}
pr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//works
for(int i = 0; i<4; i++) {
System.out.println(getHexArray()[i]);
}
}
} //end of class
openFileListener listener = new openFileListener();
openButton.addActionListener(listener);
// Should print out stuff???
System.out.println(listener.getHexArray()[0]);
您好, 我有一个事件监听器JFileChooser,让我在按下按钮时选择一个文件。 然后我读取文件,并尝试将内容保存在数组中。
我想在事件监听器之外访问hex []。在我的主要()。
我相信我的变量超出了范围?不太确定,几年后我还没有触及Java,所以如果问题是一个非常基本的概念,我会提前道歉。