public ArrayList<String> ComandoShell(String comando ){
ArrayList<String> Mensagens = new ArrayList();
try{
boolean flag = true;
Process processo = Runtime.getRuntime().exec("cmd /c " + comando);
DataOutputStream output = new DataOutputStream(processo.getOutputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(processo.getInputStream()));
BufferedReader erros = new BufferedReader(new InputStreamReader(processo.getErrorStream()));
String linha = new String();
do{
linha = input.readLine() ;
Mensagens.add(linha);
if(erros.ready())
{
Mensagens.add(erros.readLine());
}
try{
Thread.sleep(100);
}catch(InterruptedException ie){
System.out.println("Erro na InterruptedException");
}
}while((linha == null) && (erros.readLine() != null));
output.flush();
output.close();
input.close();
}catch(IOException ioe){
ioe.printStackTrace();
//return null;
}
return Mensagens;
}
Formatar disquete
Estou usando o seguinte codigo para formatar um disquete.
Mas logo depois q entro com o comando format a: ele encerra o programa e ñ me retorna nem formata o disquete alguem poderia me dar uma ajudinha
2 Respostas
E aew cara fiz algumas correções no seu código... só n fiz o teste com o comando FORMAT pq n tenho disquetes aqui...
Pode ser que o erro seja na Sixtaxe do comando format acesse o HELP dele FORMAT /? no prompt de comandos
public class TesteDOS {
public static void main(String[] args) {
System.out.println("Aguarde...");
ArrayList<String> Mensagens = comandoShell("systeminfo");
for (String msg : Mensagens) {
System.out.println(" >>> " + msg);
}
}
public static ArrayList<String> comandoShell(String comando) {
ArrayList<String> mensagens = new ArrayList<String>();
try {
Process processo = Runtime.getRuntime().exec("cmd /c " + comando);
DataOutputStream output = new DataOutputStream(processo.getOutputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(processo.getInputStream()));
BufferedReader erros = new BufferedReader(new InputStreamReader(processo.getErrorStream()));
String linha = new String();
do {
if (erros.ready()) {
linha = erros.readLine();
}else {
linha = input.readLine();
}
if(linha != null)
mensagens.add(linha);
} while (linha != null);
output.flush();
output.close();
input.close();
} catch (IOException ioe) {
ioe.printStackTrace();
// return null;
}
return mensagens;
}
}
Pode ser q não de certo pq quado vc executa o FORMAT ele pede para vc teclar enter para confirmar... vc teria que simular isso.
Eu tenho amenor ideia como se faz...
flw
E aew cara fiz algumas correções no seu código... só n fiz o teste com o comando FORMAT pq n tenho disquetes aqui... Pode ser que o erro seja na Sixtaxe do comando format acesse o HELP dele FORMAT /? no prompt de comandospublic class TesteDOS { public static void main(String[] args) { System.out.println("Aguarde..."); ArrayList<String> Mensagens = comandoShell("systeminfo"); for (String msg : Mensagens) { System.out.println(" >>> " + msg); } } public static ArrayList<String> comandoShell(String comando) { ArrayList<String> mensagens = new ArrayList<String>(); try { Process processo = Runtime.getRuntime().exec("cmd /c " + comando); DataOutputStream output = new DataOutputStream(processo.getOutputStream()); BufferedReader input = new BufferedReader(new InputStreamReader(processo.getInputStream())); BufferedReader erros = new BufferedReader(new InputStreamReader(processo.getErrorStream())); String linha = new String(); do { if (erros.ready()) { linha = erros.readLine(); }else { linha = input.readLine(); } if(linha != null) mensagens.add(linha); } while (linha != null); output.flush(); output.close(); input.close(); } catch (IOException ioe) { ioe.printStackTrace(); // return null; } return mensagens; } }Pode ser q não de certo pq quado vc executa o FORMAT ele pede para vc teclar enter para confirmar... vc teria que simular isso.
Eu tenho amenor ideia como se faz...
flw
Valeu kra. Vou fazer o teste qualquer coisa eu posto ai.
Criado 7 de junho de 2008
Ultima resposta 9 de jun. de 2008
Respostas 2
Participantes 2
Alura O que é Python? — um guia completo para iniciar nessa linguagem de programação Acesse agora o guia sobre Python e inicie sua jornada nessa linguagem de programação: o que é e para que serve, sua sintaxe e como iniciar nela!
Casa do Codigo Engenharia de Prompt para Devs: Um guia para aprender a... Por Ricardo Pupo Larguesa — Casa do Codigo