【问题标题】:Ask the user to type in the text file name that's going to be created要求用户输入将要创建的文本文件名
【发布时间】:2019-04-19 17:59:05
【问题描述】:

当他按下“生成报告”按钮时,我有创建文本文件的代码,但它仅将文本文件生成为预定名称“Report.txt”。我想要的功能是当用户按下“生成报告”按钮时,一个小窗口应该要求用户输入文件名(可能使用 JOptionPane?)并相应地按下 Ok。

else if (str.equals("Generate Report")) {

    try {
        FileWriter fw = new FileWriter("D:\\Report.txt");  //instead of report.txt, I want to user to type in the name
        BufferedWriter buffer = new BufferedWriter(fw);
        String title="Title: "+lb1.getText();
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/YYYY HH:mm:ss");
        Date date = new Date();
        buffer.write(title);
        buffer.newLine();
        buffer.newLine();
        buffer.write("\nDate and Time of Creation: "+dateFormat.format(date));

        buffer.newLine();
        buffer.newLine();
        buffer.write("\nList of activities & their duration:"); 
        buffer.newLine();
        buffer.newLine();
        buffer.write("List of all paths with the activity names and total duration:");
        buffer.newLine();

        String txtArea = ta1.getText();
        String[] txtArray = txtArea.split("\n");

        for (int i = 0; i < txtArray.length; i++) {
            buffer.write(txtArray[i]);
            buffer.newLine();
        }

        buffer.close();
        ta1.setText("Report Generated");

        buffer.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

【问题讨论】:

  • 仅在用户按下Generate report 按钮时创建FileWriter。那么目标文件名应该已经设置好了,可以在FileWriter的构造函数中使用。
  • 我不明白您包含的代码与问题的相关性。

标签: java swing user-interface filewriter


【解决方案1】:

JOptionPane 可能是最简单的解决方案。

else if(str.equals("Generate Report"))
{

        try {
            String filename = JOptionPane.showInputDialog("Please input a filename");
            FileWriter fw = new FileWriter(filename);
            ...

如果您想将文件保存在特定位置并为其指定特定扩展名,您可以在 FileWriter 的构造函数中指定它们,即

FileWriter fw = new FileWriter("D:\\" + filename +".txt")

【讨论】:

  • 我如何让它像最初我的代码一样存储在 D 盘中?
  • 这不是文本文件!如何将其设为文本文件?
  • 这真是废话。 JOptionPane 不专用于保存文件。 JFileDialog 专用于保存或打开文件,显示现有文件等。
  • @Ayaan,您的问题并不清楚该文件必须存储在 D 盘上(这是一种愚蠢的要求)。
  • @michaeak,这不是我的答案。在回复之前,您应该花时间正确阅读任何文本,并且比将其他人的努力称为废话更具建设性。
【解决方案2】:

这会向用户显示他想要的保存对话框。上面只有一些演示代码。

package save;

import java.io.File;
import java.util.Optional;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class FileSave {

    public static void main(String[] args) {
        JFrame jFrame = new JFrame("someframe");
        jFrame.setVisible(true);
        Optional<File> fileLocation = new FileSave().showSaveDialog(jFrame);
        if (fileLocation.isPresent()) {
            File saveLocation = fileLocation.get();
            System.out.println(saveLocation);
            // TODO your save logic goes here.
        }

    }

    public Optional<File> showSaveDialog(JFrame frame) {

        String property = System.getProperty("user.home");
        File userHome = new File(property);
        JFileChooser chooser = new JFileChooser(userHome);
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        chooser.setSelectedFile(new File(userHome, "Report.txt"));

        int returnVal = chooser.showSaveDialog(frame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            return Optional.of(chooser.getSelectedFile());
        }
        return Optional.empty();
    }
}

【讨论】:

    猜你喜欢
    • 2013-12-03
    • 2022-01-20
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多