Thursday, December 12, 2013

Ejercicio 4

// Autor: Juan Antonio Ripoll
// Fecha: 03/10/13

// Descripción

/*
 *  REALIZA UN PROGRAMA QUE CONTENGA UN MENU QUE APAREZCA EN PANTALLA Y 
   
    MUESTRE LAS SIGUIENTES OPCIONES.-
	1.-LEER DESDE TECLADO.
	2.-LEER DESDE FICHERO.
	3.-ESCRIBIR EN FICHERO.
	4.-SALIR.
	
	TECLEANDO 1 NOS PEDIRÁ QUE LE INTRODUZCAMOS UN TEXTO Y ESTA LA 
	IMPRIMIREMOS POR PANTALLA INDICANDO AL FINAL DE LA FRASE “GRACIAS POR LA 
	FRASE”.
	
	TECLEANDO 2 NOS PEDIRÁ EL NOMBRE/RUTA DEL FICHERO A LEER E IMPRIMIREMOS 
	TO-DO EL FICHERO POR PANTALLA.
	
	TECLEANDO 3 NOS PEDIRÁ EL NOMBRE/RUTA DEL FICHERO A ESCRIBIR Y NOS PEDIRÁ 
	QUE INTRODUZCAMOS UN TEXTO POR PANTALLA, QUE LUEGO ESCRIBIREMOS EN EL 
	FICHERO.
	
	TECLEANDO 4 FINALIZARÁ EL PROGRAMA. MIENTRAS NO FINALICEMOS SEGUIRÁ 
	MOSTRANDO EL MENÚ PARA VOLVER A REALIZAR TAREAS.
 * */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main 
{

	public static void main(String[] args) throws NumberFormatException, IOException 
	{
		boolean exit = false;
		
		while (!exit)
		{
			Draw();
			
			int option = getOption();
			
			switch(option)
			{
				case 1:
					Show();		
					break;
				case 2:
					ReadFile();
					break;
				case 3:
					WriteFile();
					break;
				case 4:
					System.out.println("Bye!");
					exit = true;
					break;
				default:
					System.out.println("Error, '" + option + "' opción no reconocida por el sistema");
					break;
			}
		}
	}
	
	
	// Dibuja el menu en pantalla
	public static void Draw()
	{
		System.out.println("1.-LEER DESDE TECLADO.");
		System.out.println("2.-LEER DESDE FICHERO.");
		System.out.println("3.-ESCRIBIR EN FICHERO.");
		System.out.println("4.-SALIR.");
		System.out.println();
		System.out.print("Introduzca una opción: ");
	}
	
	
	public static int getOption() throws NumberFormatException, IOException
	{
		InputStreamReader c = new InputStreamReader(System.in); 
		BufferedReader in = new BufferedReader(c);
	
		int option = Integer.parseInt(in.readLine());
		
		return option;
	}
	
	
	// Pedir cadena y mostrarla por pantalla concatenando
	public static void Show() throws IOException
	{
		System.out.print("Introduzca un texto: ");
		
		InputStreamReader c = new InputStreamReader(System.in); 
		BufferedReader in = new BufferedReader(c);
		
		System.out.println(in.readLine() + " GRACIAS POR LA FRASE.");
		System.out.println();
	}

	
	// Pedir ruta archivo y mostrarlo en pantalla
	public static void ReadFile() throws IOException 
	{
		System.out.print("Introduzca el nombre/ruta de un fichero a leer: ");
		
		InputStreamReader c = new InputStreamReader(System.in); 
		BufferedReader in = new BufferedReader(c);
		
		File file = new File(in.readLine());
		
		FileReader fr = new FileReader(file);
		in = new BufferedReader(fr);
		
		String line = "";
		while (line != null)
		{
			line = in.readLine();
			
			if (line != null) System.out.println(line);
		}
		
		fr.close();
		
		System.out.println();
	}

	
	// Pedir ruta archivo y pedir texto para escribirlo en fichero
	public static void WriteFile() throws IOException
	{
		System.out.print("Introduzca el nombre del fichero a escribir: ");
		
		InputStreamReader c = new InputStreamReader(System.in); 
		BufferedReader in = new BufferedReader(c);
		
		File file = new File(in.readLine());
		
		FileWriter fw = new FileWriter(file);
		in = new BufferedReader(new InputStreamReader(System.in));
		
		String line = "";
		System.out.println("El archivo se guardará automaticamente cuando pulse enter y no escriba nada.");
	
		do
		{
			line = in.readLine();
			
			if (line.length() > 0) fw.write(line + "\r\n");
		}
		while (line.length()>0);
		
		
		fw.close();
		
		System.out.println();
	} 
	
}

No comments:

Post a Comment