-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLector.java
More file actions
29 lines (24 loc) · 870 Bytes
/
Lector.java
File metadata and controls
29 lines (24 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Lector {
private File archivo = null;
public String leeArchivo(String path) {
StringBuilder contenido = new StringBuilder();
try {
this.archivo = new File(path);
Scanner scanArchivo = new Scanner(archivo);
while (scanArchivo.hasNextLine()) {
String linea = scanArchivo.nextLine();
if(scanArchivo.hasNextLine())
contenido.append(linea + "\n");
else
contenido.append(linea);
}
scanArchivo.close();
} catch (NullPointerException | FileNotFoundException e) {
System.out.println("Ha ocurrido un error con el archivo indicado.");
}
return contenido.toString();
}
}