Read file .txt (With accents) in java

0

I'm trying to read a file with .txt extension to copy the words to another file.

The problem is that if the original file has accents or special characters, the program does not work.

public static void main(String[] args)  {

    String palabra;
    Scanner input = null;
    PrintStream output = null;
    try {
        input = new Scanner(new File("Quijote01.txt"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    try {
        output = new PrintStream(new File("Copia.txt"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    while(input.hasNext()){

        palabra = input.next();
        output.println(palabra);

    }
}

What is the problem? Thanks.

    
asked by Psg 25.10.2017 в 12:07
source

1 answer

4

Good, you have to specify the encoding of the text you have written in the file, for example:

fr = new FileReader (archivo, "UTF-8");

fr = new FileReader (archivo, "ISO-8859-1");

fr = new FileReader (archivo, "cp1252");
    
answered by 25.10.2017 / 12:22
source