Java Netbeans绝对路径

Java Netbeans绝对路径,java,netbeans,helper,Java,Netbeans,Helper,我创建了一个文本编辑器和一个保存按钮,我需要创建一个绝对查找器,这样,如果用户不输入.txt,程序将自动这样做,所以它总是保存为txt文件。能帮点忙吗 我的保存按钮的代码 private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooseFile = new JFileChooser();

我创建了一个文本编辑器和一个保存按钮,我需要创建一个绝对查找器,这样,如果用户不输入.txt,程序将自动这样做,所以它总是保存为txt文件。能帮点忙吗

我的保存按钮的代码

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
    JFileChooser chooseFile = new JFileChooser();
    int choosing = chooseFile.showSaveDialog(this);

    if ( choosing == JFileChooser.APPROVE_OPTION)
    {
        try {
            PrintWriter fileSave = new PrintWriter(chooseFile.getSelectedFile());
            //absolute path ends with 

            fileSave.printf(txtArea.getText());
            fileSave.close();
            txtStatus.setText("Saved");

        } catch (FileNotFoundException ex) {
            Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}       

你能不能选中chooseFile.getSelectedFile()并在需要时附加“.txt”?@user2964786:我删除了代码。您可以在JFileChooser对话框上使用包装器,并覆盖getSelectedFile()文件APIWow,您的代码假设文件扩展名正好是三个字母长。当Windows上的文件名通常为8.3时,这甚至是危险的,但现在这种情况太多了,以至于这种检查会导致比它解决的问题更多的问题。这比->+1要好得多
   import org.apache.commons.io.FilenameUtils;

     File f= chooseFile.getSelectedFile();
    String filePath=f.getAbsolutePath();
    if(!filePath.endsWith("txt")){
    if(FilenameUtils.indexOfExtension(filePath)==-1){//user has other provided extension
        filePath+=".txt";
    }
    }