Saving File using java GUI in pc hard disk

不问归期 提交于 2019-12-11 18:30:50

问题


This is a code for Opening a file using JFileChooser --->

package fileStreamObjectSerialization;

import java.awt.BorderLayout;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class FileDemonstrationJFileChooser extends JFrame {
    private JTextArea outputArea;
    private JScrollPane scrollPane;
    JFileChooser fileChooser = new JFileChooser();

    public FileDemonstrationJFileChooser() {
        // TODO Auto-generated constructor stub
        super("Testing class file");
        outputArea = new JTextArea();
        scrollPane = new JScrollPane(outputArea);
        add(scrollPane, BorderLayout.CENTER);
        setSize(400, 400);
        setVisible(true);

        analyzePath();

        int x = fileChooser.showSaveDialog(outputArea);
                                        // How can i save the file in my hard disk
    }

    private File getFileOrDirectory() {

        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int result = fileChooser.showOpenDialog(null);
        if (result == JFileChooser.CANCEL_OPTION) {
            System.exit(1);
        }
        File fileName = fileChooser.getSelectedFile();
        if ((fileName == null) || (fileName.getName().equals(""))) {
            JOptionPane.showMessageDialog(this, "Invalid name", "Invalid name",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
        return fileName;
    }

    private void analyzePath() {
        File name = getFileOrDirectory();
        if (name.exists()) {
            outputArea.setText(String.format(
                    "%s%s\n%s\n%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s", name
                            .getName(), " exists", (name.isFile() ? "is a file"
                            : "is not a file"),
                    (name.isDirectory() ? "is a directory"
                            : "is not a directory"),
                    (name.isAbsolute() ? "is absolute path"
                            : "is not absolute path"), "Last modified: ", name
                            .lastModified(), "Length: ", name.length(),
                    "Path: ", name.getPath(), "Absolute path: ", name
                            .getAbsolutePath(), "Parent: ", name.getParent()));

            if (name.isDirectory()) {
                String[] directory = name.list();
                outputArea.append("\n\nDirectory contents:\n");

                for (String directoryName : directory) {
                    outputArea.append(directoryName + "\n");
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, name + " does not exist.",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }

    }
}

Now my question is How Can i save the file in my hard disk on clicking the SAVE button on the save dialog ??

来源:https://stackoverflow.com/questions/33881506/saving-file-using-java-gui-in-pc-hard-disk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!