1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| package edt;
import java.awt.event.ActionEvent;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringWriter;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
public class SaveAction extends AbstractAction {
private static final long serialVersionUID = 1L;
private InterfaceGraphique fenetre;
private static File tmp;
public SaveAction (InterfaceGraphique fenetre, String texte) {
super (texte);
this.fenetre = fenetre;
}
public static void receptFichier (File temp) {
tmp = temp;
}
public static String loadFile(File f) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
StringWriter out = new StringWriter();
int b;
while ((b=in.read()) != -1) {
out.write(b);
}
out.flush();
out.close();
in.close();
return out.toString();
}
public void actionPerformed (ActionEvent e) {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(fenetre);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String filename = file.getName();
String contenu = null;
try {
contenu = SaveAction.loadFile (tmp);
}
catch (IOException e2) {
e2.printStackTrace();
}
EcrireFichier fichierInter = null;
try {
fichierInter = new EcrireFichier(filename);
}
catch (IOException e1) {
e1.printStackTrace();
}
try {
fichierInter.ecrireFichierInter(contenu);
}
catch (IOException e2) {
e2.printStackTrace();
}
try {
fichierInter.fermer();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
else {
if (returnVal == JFileChooser.CANCEL_OPTION) {
JOptionPane.showMessageDialog (fenetre, "Opération annulée");
}
}
}
} |