Random rdm=new Random(); //Genero numeros aleatorios entre 0 y 100. System.out.println(rdm.nextInt(100)); //Genero letras aleatorias. Las letras minsculas en cdigo ASCII van del 97 al 123. int letraRandom= rdm.nextInt(25)+97; Leer entrada estndar //Entrada de datos estandar InputStreamReader isr = new InputStreamReader(System.in); BufferedReader entradaConsole=new BufferedReader(isr); while(entradaConsole.readline()) *La lectura recoge un String. Leer entrada desde socket Socket canal = null; BufferedReader entrada = null; canal = new Socket("localhost", 12345); entrada = new BufferedReader(new InputStreamReader(canal.getInputStream())); while (entrada.readline()) Escribir salida estndar a fichero PrintStream ps = new PrintStream( new BufferedOutputStream(new FileOutputStream( new File("javalog.txt"),true)), true); System.setOut(ps); System.setErr(ps); * Indica q queremos q se cree el fichero para aadir siempre al final del fichero Convertir String a int //Convierto el str leido a int numLeido=Integer.parseInt(strLeido); Leer de un fichero (Clase FileReader) //Establecemos el nombre del recurso a acceder archivo = new File("archivo.txt") //Preparamos el acceso al fichero y leemos del mismo (debe encerrase en un try-catch) try{ leer = new FileReader(archivo); br = new BufferedReader(leer); *La lectura recoge un String. //lectura del fichero String linea = br.readLine(); }catch(Exception e){ e.getMessage(); }finally{ //en el bloque finally cerramos el escribir. (debe encerrase en un try-catch) try{ if(null != leer){ leer.close(); }
}catch (Exception e2){
e2.getMessage();}} Escribir en un fichero (Clase FileWriter) //Preparamos el acceso al fichero y escribimos en el mismo (debe encerrase en un try-catch) try{ escribir = new FileWriter("archivo.txt"); pw = new PrintWriter(escribir); pw.println("texto"); }catch (Exception e){ e.getMessage(): }finally{ try{ if(null != escribir){ escribir.close();} }catch (Exception e2){ e2.getMessage();}} Tratamiento de parmetros pasados en la invocacin del programa // Cuando pasamos parmetros al invocar la aplicacin podemos almacenarlos en variables if (args.length > 1) variable1 = args[1]; Identificacin del Sistema Operativo //Identificamos el sistema operativo para poder acceder por su ruta alfichero de forma correcta. String osName = System.getProperty("os.name"); if (osName.toUpperCase().contains("WIN")){ //Windows if (args.length > 1) nombreFichero = args[1].replace("\\", "\\\\"); //Hemos recibido la ruta del fichero en la lnea de comandos else{ nombreFichero = "valor.txt"; //Fichero que se utilizar por defecto } }else{ //GNU/Linux if (args.length > 1) nombreFichero = args[1]; //Hemos recibido la ruta del fichero en la lnea de comandos else{ nombreFichero = "/home/margye/valor.txt"; //Fichero que se utilizar por defecto } }