Advertisement
Guest User

LeerFichero

a guest
Oct 21st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package PaqueteP1;
  7.  
  8. import java.io.IOException;
  9. import java.nio.charset.Charset;
  10. import java.nio.charset.StandardCharsets;
  11. import java.nio.file.Path;
  12. import java.nio.file.Paths;
  13. import static java.rmi.server.LogStream.log;
  14. import java.util.Scanner;
  15.  
  16. /**
  17.  *
  18.  * @author victo
  19.  */
  20. public class LeerFichero {
  21.  
  22.     private final Path ruta;
  23.     private final static Charset ENCODING = StandardCharsets.UTF_8;
  24.  
  25.     public LeerFichero(String nombreFich) {
  26.         ruta = Paths.get(nombreFich);
  27.  
  28.     }
  29.  
  30.     public void procesar() throws IOException {
  31.         try ( Scanner scanner = new Scanner(ruta, ENCODING.name())) {
  32.             for (int i = 0; i < 6; i++) { //Salta las 7 primeras lineas de informacion
  33.                 scanner.nextLine();
  34.             }
  35.             String linea = scanner.nextLine();
  36.             while(scanner.hasNextLine() && !linea.equals("EOF"))
  37.             {
  38.                 procesarLinea(linea);
  39.                 linea = scanner.nextLine();          
  40.             }
  41.         }
  42.     }
  43.     protected void procesarLinea(String linea) {
  44.         //use a second Scanner to parse the content of each line
  45.         try ( Scanner scanner = new Scanner(linea)) {
  46.             scanner.useDelimiter(" ");
  47.             if (scanner.hasNext()) {
  48.                 //assumes the line has a certain structure
  49.                 int id = Integer.parseInt(scanner.next());
  50.                 double x = Double.parseDouble(scanner.next());
  51.                 double y = Double.parseDouble(scanner.next());
  52.                
  53.                 System.out.println("ID: "+id+" "+x+" "+y);
  54.             } else {
  55.                 System.out.println("Error al procesar linea!");
  56.             }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement