Java executes Windows commands and saves the results. How to Solve the garbled Problem

Source: Internet
Author: User

Package applet;

Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;

 

/*
* This class displays the started Windows Services on the screen. Run the Net start command.
*/

Public class test {
 

Public static void main (string [] ARGs) throws ioexception {
PROCESS p = runtime.getruntime(cmd.exe C ("cmd.exe/C Net start ");
Inputstream is = P. getinputstream ();
Inputstreamreader ISR = NULL;
ISR = new inputstreamreader (is, "GBK"); // "UTF-8"

Int Len = ISR. Read ();
Byte [] buff = new byte [Len];

/**
* Convert byte array to char array
*/
String str2 = new string (buff );
Char [] CH2 = new char [str2.length ()];
For (INT I = 0; I <str2.length (); I ++ ){
CH2 [I] = str2.charat (I );

}

ISR. Read (CH2 );

Fileoutputstream Fos = new fileoutputstream ("C: // start.txt ");

/**
* Convert the char array to a byte array
*/
String STR = string. valueof (CH2, 0, ch2.length );
Byte [] alpha = Str. getbytes ();


FOS. Write (alpha );
System. Out. println (new string (alpha ));



}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.