Untitled

 avatar
unknown
java
3 years ago
1.7 kB
1
Indexable
package ficherosTexto;

import java.io.IOException;

public class LecturaFicheroQuijoteFILES {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Path inPath = Paths.get("ficheros/quijote.txt");
		int ocuerrenciasQuijote = 0;
		int ocuerrenciasDulcinea = 0, ocuerrenciasSancho = 0;
		try {
			String textoCompleto = Files.readString(inPath);
			String informacionBytes = String.format("%d bytes tiene el fichero %s%n",
					textoCompleto.getBytes().length, inPath);
			System.out.printf(informacionBytes);
			System.out.printf("%d líneas que tiene el fichero %s%n",
					textoCompleto.split("\n").length, inPath);
			
			String patternQuijote = "(Quijote|QUIJOTE)";
			Pattern pattern = Pattern.compile(patternQuijote);
	        Matcher matcher = pattern.matcher(textoCompleto);
			while(matcher.find()) 
				ocuerrenciasQuijote++;
			System.out.printf("El fichero %s contiene %d veces la palabra Quijote%n",
					inPath, ocuerrenciasQuijote);
			
			String patternDulcinea = "(Dulcinea|DULCINEA)";
			pattern = Pattern.compile(patternDulcinea);
	        matcher = pattern.matcher(textoCompleto);
			while(matcher.find()) 
				ocuerrenciasDulcinea++;
			System.out.printf("El fichero %s contiene %d veces la palabra Dulcinea%n",
					inPath, ocuerrenciasDulcinea);
			
			String patternSancho = "(Sancho|SANCHO)";
			pattern = Pattern.compile(patternSancho);
	        matcher = pattern.matcher(textoCompleto);
			while(matcher.find()) 
				ocuerrenciasSancho++;
			System.out.printf("El fichero %s contiene %d veces la palabra Sancho%n",
					inPath, ocuerrenciasSancho);
			
			//escribir en fichero
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}