HOWTO

Posted by Kaos On

Se trata de realizar una serie de Howto.

Las opciones que nos dan en el ejercicio sueño las siguientes:

- Howto leer un fichero de texto.
- Howto sumar y restar una Fecha.
- Howto escribir en un fichero de texto.
- Howto calcular los días de diferencia entre dos fechas.
- Howto coger el directorio temporal del Sistema Operativo.
- Howto generar números aleatorios.
- Howto convertir un número decimal en octal.
- Howto ejecutar otras aplicaciones en Java? Intérprete de pedidos.


- Howto leer un fichero de texto

File f = new File( "C:\\texto.txt" );
BufferedReader entrada = null;
try
{
entrada = new BufferedReader( new FileReader( f ) );
String linea;
while(entrada.ready())
{
linea = entrada.readLine();
System.out.println(linea);
}
} catch (IOException e)
{
e.printStackTrace();
}
finally
{

try
{
entrada.close();
}catch(IOException e1){}
}

- Howto sumar y restar una fecha

final long MILLSECS_PER_DAY = 24 * 60 * 60 * 1000; //Milisegundos al día
java.util.Date hoy = new Date(); //Fecha de hoy

int any = 2025; int mes = 12; int dia = 01; //Fecha anterior
Calendar calendar = new GregorianCalendar(any, mes-1, dia);
java.sql.Date fecha = new java.sql.Date(calendar.getTimeInMillis());
Calendar calendari = new GregorianCalendar(any, mes, dia);
java.sql.Date data = new java.sql.Date(calendari.getTimeInMillis());
long resta = ( hoy.getTime() - fecha.getTime() )/MILLSECS_PER_DAY;
long suma = ( hoy.getTime() + data.getTime() )/MILLSECS_PER_DAY;

- Howto escribir en un fichero de texto

File inputFile = new File("original.txt");
File outputFile = new File("outagain.txt");

FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile);
int c;
while ((c = fis.read()) != -1)
{
fos.write(c);
}
fis.close();
fos.close();

- Howto calcular los días de diferencia entre dos fechas

final long MILLSECS_PER_DAY = 24 * 60 * 60 * 1000; //Milisegons al día
java.util.Date hoy = new Date(); // Data d'avui
int any = 2009; int mes = 12; int dia = 25; //Data anterior
Calendar calendar = new GregorianCalendar(any, mes-1, dia);
java.sql.Date fecha = new java.sql.Date(calendar.getTimeInMillis());
long diferencia = ( hoy.getTime() - fecha.getTime() )/MILLSECS_PER_DAY;
System.out.println("Els dies de diferencia son: "+diferencia);

-
Howto coger del directorio temporal del Sistema Operativo.

public class TempDirExample
{
public static void main(String[] args)
{
String property = "java.io.tmpdir";
String tempDir =System.getProperty(property);
System.out.println("OS current temporary directory is " + tempDir);
}

- Howto que genera números aleatorios.

int[] ndigitos = new int[10];
int n;

Random rnd = new Random();

for (int i = 0; i <>
{
ndigitos[i] = 0;
}

for (long i=0; i <>
{
n = (int)(rnd.nextDouble() * 10.0);
ndigitos[n]++;
}

for (int i = 0; i <>
{
System.out.println(i+": " + ndigitos[i]);
}

- Howto de convertir un número decimal a octal

public static void main(String[] args) throws IOException
{
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Introdueix el numero decimal que vulguis:");
String deci = buff.readLine();
int value = Integer.parseInt(deci);
String str = Integer.toString(value,8);
System.out.println("octal:=" + str);
}

- Howto ejecutar otras aplicaciones en Java? Interprete de pedidos

public class PruebaRuntime {


public PruebaRuntime()
{
try
{

Process p=Runtime.getRuntime().exec ("cmd /c dir");

InputStream is = p.getInputStream();

BufferedReader br = new BufferedReader (new InputStreamReader (is));

String aux = br.readLine();

while (aux!=null)
{
// Se escribe la linea en pantalla
System.out.println (aux);

// y se lee la siguiente.
aux = br.readLine();
}
}
catch (Exception e)
{
// Excepciones si hay algún problema al arrancar el ejecutable o al leer su salida.*/
e.printStackTrace();
}
}

public static void main(String[] args) {

new PruebaRuntime();
}
}



Categories: