Visualizzazione dei risultati da 1 a 3 su 3

Discussione: [JAVA] File di testo

  1. #1
    Utente di HTML.it L'avatar di TigerWB
    Registrato dal
    Apr 2002
    Messaggi
    286

    [JAVA] File di testo

    ciao ragazzi,
    ho un problemino, come faccio a salvarmi il testo proveniente dal una texarea in un file di testo?
    grazie

  2. #2
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    allora, classettina :

    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) {
    	}*/
    }
    e poi metodino salva, che ti propone anche dove salvare :

    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();
    				}
    			} 
    		}
    odio chi parla di politica..
    anzi vorrei fondare un partito contro tutto ciò

  3. #3
    Utente di HTML.it L'avatar di TigerWB
    Registrato dal
    Apr 2002
    Messaggi
    286
    grazie lo proverò immediatamente.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.