allora, classettina :
e poi metodino salva, che ti propone anche dove salvare :codice:import java.io.*; import java.util.StringTokenizer; public class FileHelper { public static void copy(String orig, String dest) { try { FileInputStream in = new FileInputStream(orig); BufferedInputStream bin = new BufferedInputStream(in); FileOutputStream out = new FileOutputStream(dest); BufferedOutputStream bout = new BufferedOutputStream(out); int i; while ((i=bin.read()) != -1) { bout.write(i); } bout.flush(); in.close(); bin.close(); out.close(); bout.close(); } catch (FileNotFoundException e){ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public static void write(String str, String file) { try { FileWriter f = new FileWriter(file);//Per append [new FileWriter(file, true);] f.write(str); f.close(); } catch (Exception e) { e.printStackTrace(); } } public static String read(String file) { String str = new String(); StringBuffer out = new StringBuffer(); try { FileReader f = new FileReader(file); BufferedReader br = new BufferedReader(f); while ((str = br.readLine()) != null) { out.append(str); out.append("\n"); } br.close(); f.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return out.toString(); } public static String deleteExtension(String filename) { StringTokenizer stFile = new StringTokenizer(filename, "."); int i = stFile.countTokens(); String filenameXML = ""; for (int j = 0 ; j < i - 1; j++) { if (!filenameXML.equals("")) { filenameXML += "."; } filenameXML += stFile.nextToken(); } return filenameXML; } public static String getExtension(String filename) { StringTokenizer stFile = new StringTokenizer(filename, "."); int k = stFile.countTokens(); String extension = ""; for (int j = 0 ; j < k - 1; j++) { stFile.nextToken(); } extension = stFile.nextToken(); return extension; } // Per copiare una cartella /*try { Process proc = Runtime.getRuntime().exec("xcopy d:\\prova d:\\pippo /e /i /q"); } catch (Exception e) { }*/ }
codice:private void salvaConNome() { JFileChooser jFileChooser = new JFileChooser(); File file = new File(""); int returnVal = jFileChooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { try{ file = jFileChooser.getSelectedFile(); FileHelper fh = new FileHelper(); fh.write(jTextArea_TUATEEXTAREA.getText(),file.getAbsolutePath()); /* Writer out = new FileWriter(file); jTextArea_TUATEEXTAREA.write(out); out.close(); */ }catch(Exception e){ e.printStackTrace(); } } }


Rispondi quotando