import java.io.*;

public class CopyWriteFinally {
    public static void main(String[] args) 
    // throws necessario per mettere la close() di FileWriter 
    // dentro il blocco finally
        throws IOException {
        System.out.print("Nome file (senza suffisso): ");
        String nome = Input.readLine();
        FileWriter fileout = null;
        try {
            // apre il file in scrittura
            fileout = new FileWriter(nome + ".txt");
            String str;
            do {
                System.out.print("Scrivi una stringa (vuota per terminare): ");
                // legge una stringa da tastiera
                str = Input.readLine();
                
                // il ciclo scrive ogni carattere delle stringa nel file
                for (int i = 0; i < str.length(); i++)
                    fileout.write(str.charAt(i));
                fileout.write('\n');
                
            } while (str.length() > 0);
            
        } catch (IOException e) {
            System.out.println(e);
        } finally { // si tenta comunque di chiudere il file
            if (fileout!=null)
                fileout.close(); 
        }
        System.out.println("\nBye bye!");
    }
}