generated from programacionThiar/plantilla_iesthiar-maven-21
44 lines
1.2 KiB
Java
44 lines
1.2 KiB
Java
package iesthiar;
|
|
|
|
import java.io.BufferedWriter;
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Scanner;
|
|
|
|
public class Ejercicio_B3v2 {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Scanner entrada=new Scanner(System.in);
|
|
System.out.print("Introduce el nombre del fichero a ordenar: ");
|
|
String fichero=entrada.nextLine();
|
|
System.out.print("Introduce el nombre del fichero de salida: ");
|
|
String ficheroSalida=entrada.nextLine();
|
|
entrada.close();
|
|
try (FileWriter fw=new FileWriter(new File(ficheroSalida));
|
|
BufferedWriter bfw=new BufferedWriter(fw)){
|
|
entrada=new Scanner(new File(fichero));
|
|
ArrayList<String> palabras=new ArrayList<>();
|
|
while (entrada.hasNextLine())
|
|
palabras.add(entrada.nextLine());
|
|
Collections.sort(palabras);
|
|
for(String p: palabras) {
|
|
bfw.write(p);
|
|
bfw.newLine();
|
|
}
|
|
System.out.println("Fichero ordenado");
|
|
}
|
|
catch (FileNotFoundException e) {
|
|
System.out.println("Error al leer el fichero");
|
|
}
|
|
catch (IOException e) {
|
|
System.out.println("Error al escribir el fichero");
|
|
}
|
|
}
|
|
|
|
}
|