我有一个JFileChooser,它从目录中选择一个文件(我使用openCSV)并将数据填充到JTable中。
public class SelectFileButtonHandler{
//...
public SelectFileButtonHandler(FileReaderWindow fileReaderWindow) {
this.fileReaderWindow = fileReaderWindow;
this.fileChooser = new JFileChooser();
this.fileHandler = new FileHandler();
}
public boolean isFileSelected(){ return (this.result==JFileChooser.APPROVE_OPTION); }
/**
* returns the file's absolute path in a String object
* @return String
*/
public String getCurrentFilePath(){return this.currentFilePath;}
public String getFileType(){
return getFileName().substring(getFileName().lastIndexOf("."), getFileName().length());
}
public String getLastFilePath(){return this.lastFilePath;}
/**
* Sets the file by defining the path
* @param path
*/
public void setFile(String path){this.file = new File(path);}
public void handleCommand() {
this.fileHandler.readFile();
this.fileReaderWindow.setRowData(fileHandler.getFileData());
this.fileReaderWindow.setDataDemoTable(fileReaderWindow.getRowData());
this.fileReaderWindow.setDataDemoTableC2(fileReaderWindow.getRowData());
this.fileHandler.closeFile();
}
@Override
public void actionPerformed(ActionEvent event) {
try{
if(event.getActionCommand().equalsIgnoreCase("Προσθήκη αρχείου")){
openFileDialog();
if(isFileSelected()){
setCurrentFilePath(this.fileChooser.getSelectedFile().getAbsolutePath());
setLastFilePath(getCurrentFilePath());
this.fileHandler.openFile(getCurrentFilePath());
handleCommand();
}
}
else {
nextInChain.actionPerformed(event);
}
}
catch(Exception exception){
JOptionPane.showMessageDialog(null, exception.getMessage(), null, JOptionPane.ERROR_MESSAGE);
}
}
public void openFileDialog(){
FileNameExtensionFilter filter = new FileNameExtensionFilter("txt", "txt", "txt");
this.fileChooser.setFileFilter(filter);
this.result = fileChooser.showOpenDialog(null);
}
public void setCurrentFilePath(String currentFilePath){this.currentFilePath = currentFilePath;}
public void setLastFilePath(String lastFilePath){this.lastFilePath = lastFilePath;}
}
FileHandler.java
public class FileHandler{
/**
* Opens file located to the specific path
* Path is absolute
* @param path
*/
public void openFile(String path) {setFileInputStream(path);}
/**
* Reads data from file.
* Data are separated by split character selected by combo box.
*/
public void readFile() {
try {
setUTF8_CHARSET();
setInputStreamReader(getFileInputStream(), getUTF8_CHARSET());
setCSVReader(getInputStreamReader(), this.fileReaderWindow.getSplitChoose().getSelectedItem().toString().charAt(0));
setFileData(getCSVReader().readAll());
}
catch (IOException ex) {
Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
}
catch(java.lang.NoClassDefFoundError err){
JOptionPane.showMessageDialog(null, err.toString(), null, JOptionPane.ERROR_MESSAGE);
}
}
public void setCSVReader(InputStreamReader inputStreamReader, char separator){
this.csvReader = new CSVReader(inputStreamReader, separator);
}
public void setFileData(List<String[]> fileData){this.fileData = fileData;}
public void setFileInputStream(String path){
try{
this.fileInputStream = new FileInputStream(path);
}
catch(FileNotFoundException exception){
JOptionPane.showMessageDialog(null, "Το αρχείο δε βρέθηκε στο δίσκο.\nΠαρακαλώ, ξαναπροσπαθήστε.", null, JOptionPane.ERROR_MESSAGE);
}
}
public void setInputStreamReader(FileInputStream fileInputStream, CharsetDecoder UTF8_CHARSET){
this.inputStreamReader = new InputStreamReader(fileInputStream, UTF8_CHARSET);
}
}
我的问题是,当我尝试从另一个类调用getLastFilePath时,我得到null而不是路径
public class SplitChooseComboHandler implements ItemListener{
private FileReaderWindow fileReaderWindow;
private FileHandler fileHandler;
private SelectFileButtonHandler selectFileButtonHandler;
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
this.fileReaderWindow = new FileReaderWindow();
this.selectFileButtonHandler = new SelectFileButtonHandler(fileReaderWindow);
this.fileHandler.openFile(selectFileButtonHandler.getLastFilePath());
this.selectFileButtonHandler.handleCommand();
JOptionPane.showMessageDialog(null, "file path is: " + this.selectFileButtonHandler.getLastFilePath(), null, JOptionPane.INFORMATION_MESSAGE);
}
}
}
有人可以解释为什么会这样吗?提前谢谢
答案 0 :(得分:0)
有文件lastFilePath = new File 并在您的文件处理程序中
public static void LastFile{
lastFilePath = (File)selectFileButtonHandler.getLastFilePath();
}
并在您的splitChooseComboHandler中 用LastFilePath替换selectFileButtonHandler.getLastFilePath() 检查是否有效哈哈
答案 1 :(得分:0)
看看你的代码......
// Create a new instance of SelectFileButtonHandler
this.selectFileButtonHandler = new SelectFileButtonHandler(fileReaderWindow);
this.fileHandler.openFile(selectFileButtonHandler.getLastFilePath());
在致电getLastFilePath
时,值lastFilePath
为null
lastFilePath
的值仅由setLastFilePath
方法更改,但仅由actionPerformed
中的SelectFileButtonHandler
方法调用。
这意味着当值path
值传递给setFileInputStream
中的FileHandler
方法时,它是null
...
您可以考虑lastFilePath
null
的可能性,并提供默认值,例如用户的主目录...