Questo è il codice della copia un pò modificato per il salvataggio, ti scrivo anche quello funzionante per la copia?
codice://funzione salva file public static void saveFile() { JFileChooser chooser = new JFileChooser(); File modello = new File("modello_programma.rtf"); chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { return f.getName().toLowerCase().endsWith(".rtf") || f.isDirectory(); } public String getDescription() { return "Documento *.RTF"; } }); int s = chooser.showSaveDialog(new JFrame()); if (s == JFileChooser.APPROVE_OPTION) { try { save(modello); } catch (IOException e) { System.err.println(e.getMessage()); } System.out.println(modello); } } //salvataggio a livello fisico public static void save(File modello) throws IOException { File fromFile = modello; File toFile = new File("modello_programma.rtf"); if (toFile.isDirectory()) toFile = new File(toFile, fromFile.getName()); if (toFile.exists()) { if (!toFile.canWrite()) throw new IOException("FileCopy: " + "destination file is unwriteable: " + modello); System.out.print("Overwrite existing file " + toFile.getName() + "? (Y/N): "); System.out.flush(); BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); String response = in.readLine(); if (!response.equals("Y") && !response.equals("y")) throw new IOException("FileCopy: " + "existing file was not overwritten."); } else { String parent = toFile.getParent(); if (parent == null) parent = System.getProperty("user.dir"); File dir = new File(parent); if (!dir.exists()) throw new IOException("FileCopy: " + "destination directory doesn't exist: " + parent); if (dir.isFile()) throw new IOException("FileCopy: " + "destination is not a directory: " + parent); if (!dir.canWrite()) throw new IOException("FileCopy: " + "destination directory is unwriteable: " + parent); } FileInputStream from = null; FileOutputStream to = null; try { from = new FileInputStream(fromFile); to = new FileOutputStream(toFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = from.read(buffer)) != -1) to.write(buffer, 0, bytesRead); // write } finally { if (from != null) try { from.close(); } catch (IOException e) { ; } if (to != null) try { to.close(); } catch (IOException e) { ; } } }

Rispondi quotando