Emphasis on cmd return

5

Considering a method to list the files of a folder using ProcessBuilder I have the incorrect return of the accented words.

Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashSet;

public class TesteCMD {

  public static void main(String[] args) throws Exception {
    LinkedHashSet<String> comandos = new LinkedHashSet<>();
    TesteCMD cmd = new TesteCMD();

    comandos.add("cd C:/Vídeos/Filmes");
    comandos.add("dir /b");

    System.out.println(cmd.executar(comandos.toArray(new String[comandos.size()])));
  }

  public String executar(String... comandos) {
    StringBuilder saida = new StringBuilder();
    BufferedReader leitor;
    ProcessBuilder processos;
    Process processo;
    String linha;

    try {
      processos = new ProcessBuilder("cmd.exe", "/c", String.join(" && ", comandos));
      processo = processos.start();
      processo.waitFor();
      leitor = new BufferedReader(new InputStreamReader(processo.getInputStream()));

      while ((linha = leitor.readLine()) != null) {
        saida.append(linha).append("\n");
      }
    } catch (IOException | InterruptedException ex) {
      return ex.getMessage();
    }

    return saida.toString();
  }
}

Folder

Output

run:AViagemdeChihiro(2001)Batman-TheDarkKnightReturns(2012)ContosdeTerramar(2006)FromuponPoppyHill(2011)LiloeStitch(2002)ManofSteel(2013)MeuAmigoTotoro(1988)MeusVizinhososYamadas(1999)Nausica�doValedoVento(1984)OCasteloAnimado(2004)OCastelonoC�u(1986)OMundodosPequeninos(2010)OReiLe�oOReinodosGatos(2002)OServi�odeEntregasdaKiki(1989)OnlyYesterday(1991)PomPoko(1994)Ponyo(2008)PorcoRosso(1992)PrincesaMononoke(1997)ShingekinoKyojin(2013)SussurosdoCora��o(1995)TheAnimatrix(2003)TheMatrix(1999)T�mulodosVagalumes(1988)UP-AltasAventuras(2009)VdeVingan�a(2005)VidasaoVento(2013)CONSTRUÍDOCOMSUCESSO(tempototal:0segundos)

Iknowyouhaveotherwaystolistthefilesandthat'snotthepoint.Iwouldliketoknowhowtosolveforthereturnofmyprocessestobeaccentedcorrectlyandalsowhythisoccurs.

I'vealreadytriedtoaddencoding(UTF-8,ISO-8859-1andwindows-1252)toInputStreamReaderbutdidnotfixtheproblem.

Note

Ihavetriedothercommands,forexample:

System.out.println(cmd.executar("@ECHO Teste acentuação"));

Results in:

run:
Teste acentua��o

CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)

When should it be:

    
asked by anonymous 27.11.2016 / 07:36

1 answer

4

Use the chcp command to change the cmd charset .

For UTF-8 the code is 65001

The command to execute:

chcp 65001

I'm not sure where I could run within those functions that I submitted.

You can try the multi-command concatenator, the & character

System.out.println(cmd.executar("chcp 65001 & @ECHO Teste acentuação"))

Alternatively you can invoke a .bat archive. Example here: link

To learn more about concatenating multiple commands and output redirection: #> link

PrintStream

Also try changing this snippet:

System.out.println(cmd.executar(comandos.toArray(new String[comandos.size()])));

By this:

PrintStream p = new PrintStream(System.out, true, "UTF-8");
p.println(cmd.executar(comandos.toArray(new String[comandos.size()])));

Text editor encoding

You can also have other reasons such as the text editor. Check which encoding the code editor is saving.

Note

If you are calling the JAVA script from the Windows prompt it may be just the prompt setting. Try it out:

chcp 65001
java /local/do/app/java.jar

Encoding 850

Also try code 850

PrintStream p = new PrintStream(System.out, true, "CP850");
p.println(cmd.executar(comandos.toArray(new String[comandos.size()])));

In this case, remove that command: chcp 65001 &





About us * Here I presented example for UTF-8. Make sure which charset you are using and apply the appropriate code.

    
27.11.2016 / 07:49