Created
August 9, 2019 03:39
-
-
Save parzibyte/ac3af3c1fd23a8a249d38d3bc1569483 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hola mundo | |
Soy el contenido del archivo | |
Programando en Java desde parzibyte.me |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileReader; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
/* | |
Leer el contenido de un archivo con Java | |
https://parzibyte.me/blog | |
*/ | |
class Main { | |
public static void main(String[] args) { | |
String nombreArchivo = "ejemplo.txt"; | |
FileReader fileReader = null; | |
BufferedReader bufferedReader = null; | |
try { | |
fileReader = new FileReader(nombreArchivo); | |
bufferedReader = new BufferedReader(fileReader); | |
StringBuilder stringBuilder = new StringBuilder(""); | |
String linea; | |
while ((linea = bufferedReader.readLine()) != null) { | |
// Lee línea por línea, omitiendo los saltos de línea | |
stringBuilder.append(linea + "\n"); | |
} | |
System.out.println("Contenido: " + stringBuilder.toString()); | |
} catch (IOException e) { | |
System.out.println("Excepción leyendo archivo: " + e.getMessage()); | |
} finally { | |
try { | |
if (fileReader != null) | |
fileReader.close(); | |
if (bufferedReader != null) | |
bufferedReader.close(); | |
} catch (IOException e) { | |
System.out.println("Excepción cerrando: " + e.getMessage()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment