Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法计算如何存储“0”;另存为;字符串中的文本并使用它_Java_Swing_Awt_Jfilechooser - Fatal编程技术网

Java 无法计算如何存储“0”;另存为;字符串中的文本并使用它

Java 无法计算如何存储“0”;另存为;字符串中的文本并使用它,java,swing,awt,jfilechooser,Java,Swing,Awt,Jfilechooser,我不熟悉Java的UI设计。我正在尝试创建一个GUI,以便从Internet下载一个文件并将其保存在硬盘上。我已经让代码工作,除了一件事,我想补充。我添加了一个JFileChooser,它允许用户选择目标文件夹。但是我不知道如何将文件名更改为用户在JFileChooser菜单上的另存为栏中输入的文件名 浏览按钮 browseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent

我不熟悉Java的UI设计。我正在尝试创建一个GUI,以便从Internet下载一个文件并将其保存在硬盘上。我已经让代码工作,除了一件事,我想补充。我添加了一个
JFileChooser
,它允许用户选择目标文件夹。但是我不知道如何将
文件名更改为用户在
JFileChooser
菜单上的
另存为
栏中输入的文件名

浏览按钮

browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
    chooser = new JFileChooser();
    chooser.setCurrentDirectory(null);
    chooser.setDialogTitle("Select folder to save");
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.setAcceptAllFileFilterUsed(true);

    //chooser.showDialog(downloadButton, "Save");
    if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
    {
        System.out.println("The location to save is: " + chooser.getCurrentDirectory());
        DESTINATION_FOLDER = chooser.getCurrentDirectory().toString();
    }
}
URLConnection connection = downloadUrl.openConnection();

input = new BufferedInputStream(connection.getInputStream());
output = new FileOutputStream(DESTINATION_FOLDER + "/" + filename);
}))

下载按钮

browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
    chooser = new JFileChooser();
    chooser.setCurrentDirectory(null);
    chooser.setDialogTitle("Select folder to save");
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.setAcceptAllFileFilterUsed(true);

    //chooser.showDialog(downloadButton, "Save");
    if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
    {
        System.out.println("The location to save is: " + chooser.getCurrentDirectory());
        DESTINATION_FOLDER = chooser.getCurrentDirectory().toString();
    }
}
URLConnection connection = downloadUrl.openConnection();

input = new BufferedInputStream(connection.getInputStream());
output = new FileOutputStream(DESTINATION_FOLDER + "/" + filename);

此处
filename
应为用户输入的文件名。关于如何完成此操作的提示?

在没有看到更多代码的情况下,我建议最好在您正在使用的类中创建一个全局字符串

public class gui extends JFrame{
    public String filePath="";
    public static void main(String args[]){
        //button code
        browseButton.addActionListener(new ActionListener())
        saveAsButton.addActionListener(new ActionListener())
        URLConnection connection = downloadUrl.openConnection();



    }

public void actionPerformed(ActionEvent e)
{
    if (e.getActionCommand().equals("browseButton"){
        chooser = new JFileChooser();
        chooser.setCurrentDirectory(null);
        chooser.setDialogTitle("Select folder to save");
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setAcceptAllFileFilterUsed(true);

        //chooser.showDialog(downloadButton, "Save");
        if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
        {
            System.out.println("The location to save is: "+chooser.getCurrentDirectory());
            filePath = chooser.getCurrentDirectory().toString();
        }
    else{
        //save as button selected
        input = new BufferedInputStream(connection.getInputStream());
        output = new FileOutputStream(filePath);
    }
}

}
添加此行:

chooser.setDialogType(JFileChooser.SAVE_DIALOG);
完整代码:

browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
    chooser = new JFileChooser();
    chooser.setCurrentDirectory(null);
    chooser.setDialogTitle("Select folder to save");
    chooser.setDialogType(JFileChooser.SAVE_DIALOG);
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.setAcceptAllFileFilterUsed(true);

    //chooser.showDialog(downloadButton, "Save");
    if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
    {
        System.out.println("The location to save is: " + chooser.getCurrentDirectory());
        DESTINATION_FOLDER = chooser.getCurrentDirectory().toString();
    }
}

实际上,您不需要从JFileChooser中的
另存为
栏获取文件名。
就这样做吧:

    browseButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e){
        chooser = new JFileChooser();
        chooser.setCurrentDirectory(null);
        chooser.setDialogTitle("Select folder to save");
        //Don't use the 'FileSelectionMode();'. Let it be Default.
        chooser.setAcceptAllFileFilterUsed(true);
   if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
   {
    file = chooser.getSelectedFile();
    //file should be declared as a File.
     System.out.println("The location to save is: " + chooser.getCurrentDirectory();));
     System.out.println("The FileName is: " + file.getName()); 
   }
} 
下载按钮:

   URLConnection connection = downloadUrl.openConnection();
   input = new BufferedInputStream(connection.getInputStream());
   output = new FileOutputStream(file);  

谢谢你的回复。但是,嗯,我实际上是在问如何使用
文件名
而不是
文件路径
,这是用户在选择器菜单的
另存为
文本栏上指定的。为了更好地理解,我提供了一张图片。