Añadido resto de tema

This commit is contained in:
2024-05-13 09:50:34 +02:00
parent eae1c73134
commit 822dda06de
12 changed files with 281 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
package iesthiar;
import java.io.FileReader;
import java.io.IOException;
public class _331_FileReader {
public static void main(String[] args) {
try (FileReader reader = new FileReader("ruta/del/archivo.txt")) {
int caracter;
while ((caracter = reader.read()) != -1) {
System.out.print((char) caracter);
}
} catch (IOException e) {
System.out.println("Error al leer el archivo: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,18 @@
package iesthiar;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class _332_BufferedReader {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("datos.txt"))) {
String linea;
while ((linea = br.readLine()) != null) {
System.out.println(linea);
}
} catch (IOException e) {
System.err.println("Error al leer el archivo: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,22 @@
package iesthiar;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class _333_InputStreamReader {
public static void main(String[] args) {
File f = new File("fichero.txt");
String cadena;
try (FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); // "ISO-8859-1"
BufferedReader bfr = new BufferedReader(isr)) {
while ((cadena = bfr.readLine()) != null)
System.out.printf("%s%n", cadena);
} catch (IOException ex) {
System.err.printf("Error:%s\n", ex.getMessage());
}
}
}

View File

@@ -0,0 +1,23 @@
package iesthiar;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class _334_PruebaScanner {
public static void main(String[] args) {
// Intentamos abrir el fichero
File f = new File("enteros.txt");
try (Scanner lector = new Scanner(f);) {
// Si llega aquí es que ha abierto el fichero :)
while (lector.hasNextInt()) {
int valor = lector.nextInt();
System.out.println("El valor leído es: " + valor);
}
} catch (FileNotFoundException e) {
// En caso de excepción mostramos el error
System.out.println("Error: " + e);
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,29 @@
package iesthiar;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class _341_FileWriter {
public static void main(String[] args) {
try {
File f = new File("enteros.txt");
FileWriter fw = new FileWriter(f);
int valor = 1;
for (int i = 1; i <= 20; i++) {
fw.write("" + valor); // escribimos valor
fw.write(" ");
// escribimos espacio en blanco
valor = valor * 2;
// calculamos próximo valor
}
fw.write("\n"); // escribimos nueva línea
fw.close(); // cerramos el FileWriter
// MEJOR USAR TRY-FOR-RESOURCES
System.out.println("Fichero escrito correctamente");
} catch (IOException e) {
System.out.println("Error: " + e);
}
}
}

View File

@@ -0,0 +1,21 @@
package iesthiar;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class _342_BufferedWriter {
public static void main(String[] args) {
File f = new File("fichero.txt");
try (FileWriter fw = new FileWriter(f);
BufferedWriter bfw = new BufferedWriter(fw)) {
bfw.write("Esto es un texto");
bfw.newLine();
bfw.write("Esto es otro texto");
bfw.write(" que se escribirá en la misma línea");
} catch (IOException ex) {
System.err.printf("Error:%s", ex.getMessage());
}
}
}

View File

@@ -0,0 +1,23 @@
package iesthiar;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class _343_OutputStreamWriter {
public static void main(String[] args) {
File f = new File("fichero.txt");
try (
FileOutputStream fos = new FileOutputStream(f, true);
OutputStreamWriter osw = new OutputStreamWriter(fos, "ISO-8859-1");
BufferedWriter bfw = new BufferedWriter(osw)) {
bfw.write("Esto es un texto");
bfw.newLine();
bfw.write("Esto es otro texto con eñe");
} catch (IOException ex) {
System.err.printf("Error:%s", ex.getMessage());
}
}
}

View File

@@ -0,0 +1,20 @@
package iesthiar;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class _344_PrintWriter {
public static void main(String[] args) {
double num = 42.12;
try (FileWriter fw = new FileWriter("fichero.txt", true); // añade datos
BufferedWriter bfw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bfw, true)) {
pw.printf("num=%06.1f\n", num);
pw.println("linea nueva");
} catch (IOException ex) {
System.err.printf("Error:%s", ex.getMessage());
}
}
}

View File

@@ -0,0 +1,50 @@
package iesthiar;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class _41_LecturaEscritura {
public static void escritura() {
_41_Persona[] pers = { new _41_Persona("Pedro", "981222333", 1234.34f),
new _41_Persona("Juan", "982444555", 2222.34f) };
try (FileOutputStream fos = new FileOutputStream("fichero.dat", false);
// No podemos añadir, siempre vacia contenido previo si lo hubiese
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
for (int cont = 0; cont < pers.length; cont++)
oos.writeObject(pers[cont]);
} catch (IOException ex) {
System.err.println("Error:" + ex.getMessage());
}
}
public static void lectura() {
_41_Persona[] personas = new _41_Persona[100];
boolean eof = false;
File fichero = new File("fichero.dat");
try (FileInputStream fis = new FileInputStream(fichero);
BufferedInputStream bufis = new BufferedInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(bufis)) {
int cont = 0;
while (!eof) {
// while(bufis.available()>0
personas[cont] = (_41_Persona) ois.readObject();
if (++cont > personas.length)
break;
}
} catch (EOFException e) {
eof = true;
} catch (IOException ex) {
System.err.println("Error:" + ex.getMessage());
} catch (ClassNotFoundException ex) {
System.err.println("Err:" + ex.getMessage());
}
}
}

View File

@@ -0,0 +1,22 @@
package iesthiar;
import java.io.Serializable;
public class _41_Persona implements Serializable{
private static final long serialVersionUID = 1L;
public String nombre;
public String telefono;
public float sueldo;
public _41_Persona(String nombre, String telefono, float sueldo) {
this.nombre = nombre;
this.telefono = telefono;
this.sueldo = sueldo;
}
@Override
public String toString() {
return String.format("Nombre:%s%nTelefono:%s%nSueldo:%.2f%n",
nombre, telefono, sueldo);
}
}

View File

@@ -0,0 +1,16 @@
package iesthiar;
import java.util.Properties;
public class _51_EscrituraProperties {
public static void main(String[] args) {
Properties config = new Properties();
//(import.java.util.*)
config.setProperty("user", miUsuario);
config.setProperty("password", miContrasena);
config.setProperty("server", elServidor);
config.setProperty("port", elPuerto);
try {config.store(new FileOutputStream("config.props"),"Fichero de config.");}
catch (IOException ioe) {ioe.printStackTrace();}
}
}

View File

@@ -0,0 +1,20 @@
package iesthiar;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class _52_LeerProperties {
public static void main(String[] args) {
Properties config = new Properties();
try {
config.load(new FileInputStream("config.props"));
usuario = config.getProperty("user");
password = config.getProperty("password");
servidor = config.getProperty("server");
puerto = Integer.valueOf(config.getProperty("port"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}