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)toInputStreamReader
butdidnotfixtheproblem.
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: