/*
// Autor: Juan Antonio Ripoll
// Fecha: 03/10/2013
// Descripción:
/*
REALIZA UN PROGRAMA EN JAVA QUE HAGA UNA COPIA DE UN FICHERO A OTRO DE
TODOS SUS DATOS. LOS NOMBRES DEL FICHERO A COPIAR SE LE PASARÁN COMO
PARÁMETRO.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main
{
public static void main(String[] args) throws IOException
{
File file_in;
File file_out;
if (args.length != 2)
{
InputStreamReader c = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(c);
System.out.print("Introduzca la ruta del archivo a copiar: ");
file_in = new File( in.readLine() );
System.out.print("Introduzca la ruta del nuevo archivo: ");
file_out = new File( in.readLine() );
}
else
{
file_in = new File( args[0] );
file_out = new File( args[1] );
}
System.out.println("Realizando copia de fichero un momento ...");
try
{
FileInputStream in = new FileInputStream(file_in);
FileOutputStream out = new FileOutputStream(file_out);
int i = 0;
while(i != -1)
{
i = in.read();
if (i != -1) out.write(i);
}
in.close(); out.close();
System.out.println("La copia se realizó correctamente.");
}
catch(IOException e)
{
System.out.println("Error intentado realizar la copia del archivo.");
}
}
}
Thursday, December 12, 2013
Ejercicio 2
Ejercicio 1
/*
* Autor: Juan Antonio Ripoll
*
* REALIZA UN PROGRAMA EN JAVA QUE MUESTRE LOS FICHEROS DE UN DIRECTORIO. EL
* NOMBRE (DEL FICHERO O DIRECTORIO) SE PASARÁ COMO PARÁMETRO.
*/
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Main
{
public static void main(String[] args) throws IOException
{
String path_file = null;
if (args.length != 1)
{
System.out.print("Introduzca la ruta de un directorio/archivo: ");
InputStreamReader c = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(c);
path_file = in.readLine();
}
else
path_file = args[0];
File archivo = new File( path_file );
if (archivo.isDirectory())
{
File[] archivos = archivo.listFiles();
for (File a : archivos) {
System.out.println(a.getName());
}
}
}
}
Wednesday, October 30, 2013
2.07 - Repeat until 0 do while
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Create a JAVA program to ask the user for a number "x" and display 10*x.
* It must repeat until the user enters 0 (using "do-while").
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number;
do
{
System.out.print("Enter a number: ");
number = in.nextInt();
System.out.println(number*10);
}
while (number != 0);
}
}
2.06 - Repeat until 0
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Create a JAVA program to ask the user for a number "x"
* and display 10*x. It must repeat until the user enters 0 (using "while").
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = in.nextInt();
while (number != 0)
{
System.out.println(number*10);
System.out.print("Enter a number: ");
number = in.nextInt();
}
}
}
2.05 - Greatest of three numbers
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to get three numbers
* from the user and display the greatest one.
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first number: ");
int number1 = in.nextInt();
System.out.print("Enter the second number: ");
int number2 = in.nextInt();
System.out.print("Enter the third number: ");
int number3 = in.nextInt();
if (number1 > number2)
if (number1 > number3)
System.out.println("The greatest: " + number1);
if (number2 > number1)
if (number2 > number3)
System.out.println("The greatest: " + number2);
if (number3 > number1)
if (number3 > number2)
System.out.println("The greatest: " + number3);
}
}
2.04 - Divide if not zero using else
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to ask the user for two numbers, and show
* their division if the second number is not zero; otherwise,
* it will display "I cannot divide
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first number: ");
int number1 = in.nextInt();
System.out.print("Enter the second number: ");
int number2 = in.nextInt();
if (number2!=0)
System.out.print(number1 + " / " + number2 + " = "
+ number1/number2);
else
System.out.print("I cannot divide.");
}
}
2.03 - Divide if not zero
/*
* Autor: Juan Antonio Ripoll
* Date: 30/10/13
*
* Write a JAVA program to ask the user for two numbers, and show
* their division if the second number is not zero; otherwise,
* it will display "I cannot divide
* */
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first number: ");
int number1 = in.nextInt();
System.out.print("Enter the second number: ");
int number2 = in.nextInt();
if (number2!=0)
System.out.print(number1 + " / " + number2 + " = "
+ number1/number2);
else
System.out.print("I cannot divide.");
}
}
Subscribe to:
Posts (Atom)