Leer un fichero en Java
by deckerix · October 28, 2008
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CargarFichero {
private static String fichero= "/tmp/fichero.dat";
public CargarFichero() {}
public static void main(String args[]) {
CargarFichero programa = new CargarFichero();
programa.run();
}
public int run() {
BufferedReader in=null;
try {
in = new BufferedReader(new FileReader(fichero));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
String linea = null;
try {
while ((linea = in.readLine() )!= null){
System.out.println(linea);
}
} catch (IOException ex) {
ex.printStackTrace();
}
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return 0;
}
} |
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CargarFichero {
private static String fichero= "/tmp/fichero.dat";
public CargarFichero() {}
public static void main(String args[]) {
CargarFichero programa = new CargarFichero();
programa.run();
}
public int run() {
BufferedReader in=null;
try {
in = new BufferedReader(new FileReader(fichero));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
String linea = null;
try {
while ((linea = in.readLine() )!= null){
System.out.println(linea);
}
} catch (IOException ex) {
ex.printStackTrace();
}
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return 0;
}
}