Process process = Runtime.getRuntime().exec("cmd /c dir");
System.out.println(loadStream(process.getInputStream())); //load the stream
System.out.println(loadStream(process.getErrorStream())); //load the stream
static String loadStream(InputStream in) throws IOException
{
int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while ( (ptr = in.read()) != -1)
{
buffer.append( (char) ptr);
}
return buffer.toString();
}
---------------------------
刚才那个loadStream方法,如果dir里面有汉字则显示为乱码。应将该方法修改为如下:
static String loadStream(InputStream in) throws IOException
{
int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while ( (ptr = in.read()) != -1)
{
buffer.append( (char) ptr);
}
String aa=buffer.toString();
byte[] temp= aa.getBytes("ISO-8859-1");
String result=new String(temp);
return result;
}
System.out.println(loadStream(process.getInputStream())); //load the stream
System.out.println(loadStream(process.getErrorStream())); //load the stream
static String loadStream(InputStream in) throws IOException
{
int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while ( (ptr = in.read()) != -1)
{
buffer.append( (char) ptr);
}
return buffer.toString();
}
---------------------------
刚才那个loadStream方法,如果dir里面有汉字则显示为乱码。应将该方法修改为如下:
static String loadStream(InputStream in) throws IOException
{
int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while ( (ptr = in.read()) != -1)
{
buffer.append( (char) ptr);
}
String aa=buffer.toString();
byte[] temp= aa.getBytes("ISO-8859-1");
String result=new String(temp);
return result;
}