gpt4 book ai didi

Java Netbeans 绝对路径

转载 作者:行者123 更新时间:2023-12-01 13:42:27 30 4
gpt4 key购买 nike

我创建了一个文本编辑器和一个保存按钮,我需要创建一个绝对查找器,以便如果用户不输入 .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);
}

}
}

最佳答案

   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";
}
}

关于Java Netbeans 绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20607700/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com