gpt4 book ai didi

java - 保存文件和另存为文件方法

转载 作者:太空宇宙 更新时间:2023-11-04 07:14:28 27 4
gpt4 key购买 nike

我正在使用 Netbeans 开发一个编辑器应用程序。我已经编写了使用 Jfilechooser 保存文件的代码,保存功能正常工作。但我在编写简单保存按钮的代码时遇到问题。 当前打开的选项卡的。给我一些想法来保存当前选项卡的内容,例如 ctrl+s 功能。这是我保存为方法的代码。提前致谢。

  private void saveAsmActionPerformed(java.awt.event.ActionEvent evt) {                                        
// TODO add your handling code here:
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == fileChooser.APPROVE_OPTION)
{
File dir1 = fileChooser.getCurrentDirectory();
String dir = dir1.getPath();
String name = fileChooser.getSelectedFile().getName();
//if it dont have .txt at end of name then add it
if (!name.endsWith(".txt"))
{
name = (name + ".txt");
}

try{
File file = new File(dir,name);
int res = 0;

if(file.exists())
{
res = JOptionPane.showConfirmDialog(null, "This file already exists, Overwrite it?");
}
if(res == 0)
{

FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);


FilePanel selectedComp = (FilePanel)tabbedPane.getSelectedComponent();

if (selectedComp != null) {
String text = selectedComp.getTextArea().getText();
bw.write(text);

} else {
System.out.println("No component selected");
}

bw.close();
tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), name);
ClosableTab.apply(tabbedPane, tabbedPane.getTabCount()-1);
}

}
catch(Exception e)
{
System.out.println("err");
}

}
}

最佳答案

执行“另存为”后,将文件存储为字段,然后“保存”写入现有文件。

public class FilePanel /* extends not sure */ {
private File file;

...

public File getFile() { return file; }
public void setFile(File f) { file = f; }

...
}

/*
* not sure how your event structure works
* this is the common way to do it
*
* @Override
* public void actionPerformed(ActionEvent evt) {
* if (evt.getSource() == saveAsButton) {
* saveAsAction();
* } else if (evt.getSource() == saveButton) {
* saveAction();
* }
* }
*
*/

// recommend refactor so "save" can call "save as" without regard to event
private void saveAsAction() {

int exInput = JOptionPane.NO_OPTION;
File file = null;

// use a do-while and "no" to reshow the save dialog if exists

do {
int returnVal = fileChooser.showSaveDialog(null);

if (returnVal != JFileChooser.APPROVE_OPTION) {
return;
}

file = fileChooser.getSelectedFile();

if (!file.getName().endsWith(".txt")) {
file = new File(file.getParentFile(), file.getName() + ".txt");
}

if (file.exists()) {
exInput = JOptionPane.showConfirmDialog(
null, "This file already exists, overwrite it?");

if (exInput == JOptionPane.CANCEL_OPTION) {
return;
}
}
} while (file.exists() && exInput == JOptionPane.NO_OPTION);

FilePanel selectedComp = (FilePanel)tabbedPane.getSelectedComponent();

if (selectedComp != null) {
String text = selectedComp.getTextArea().getText();
file = writeToFile(file, text);
selectedComp.setFile(file);

} else {
System.out.println("No component selected");
return;
}

tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), file.getName());
ClosableTab.apply(tabbedPane, tabbedPane.getTabCount() - 1);
}

private void saveAction() {

FilePanel selectedComp = (FilePanel)tabbedPane.getSelectedComponent();

if (selectedComp != null) {
File file = selectedComp.getFile();

if (file == null) {
// imply no "save as" performed

saveAsAction();
return;
}

String text = selectedComp.getTextArea().getText();
file = writeToFile(file, text);
selectedComp.setFile(file);

} else {
System.out.println("No component selected");
}
}

// recommend refactor so "save" and "save as" can share write code
private static File writeToFile(File file, String text) {

try {

FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);

bw.write(text);
bw.close();

return file;

} catch(Exception e) {
// handle your IO errors better than this
// files are not willy-nilly!

System.out.println("err");
return null;
}
}

关于java - 保存文件和另存为文件方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20168025/

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