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 ??
showSaveDialoginstead. The resultingFileneed to exist, you can useFile#getParentFile#mkdirsto make the directories if they don't exist, demonstrated here. You can then write to the file in any number of ways, depending on what you want to save, for example, you could useJTextArea#write