Execute commands and file operations for android development (linux)

 
 
public void shellExec() {
        Runtime mRuntime = Runtime.getRuntime(); //Method of executing commands
        try {
            //Process encapsulates the returned result and the result of the execution error
            Process mProcess = mRuntime.exec("ls -l"); //Add parameters
	//Use BufferReader to buffer each character for efficient reading
	//InputStreamReader converts the byte stream data obtained after executing the command into a character stream
	//mProcess.getInputStream() gets the byte stream result after the command is executed
            BufferedReader mReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
	//Instantiate a character buffer
            StringBuffer mRespBuff = new StringBuffer();
	//Instantiate and initialize a character buffer of size 1024, char type
            char[] buff = new char[1024];
            int ch = 0;
	//The read() method reads the content into the buff buffer, the size is the size of the buff, and returns an integer value, that is, the length of the content
	//if length is not null
            while ((ch = mReader.read(buff)) != -1) {
	/ / Fill the content of the buffer buff into the character buffer
                mRespBuff.append(buff, 0, ch);
            }
	//end buffer
            mReader.close();
	// pop up the result
            Toast.makeText(this,mRespBuff.toString(),Toast.LENGTH_SHORT).show();
	// exception handling
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        }

    }

Folder and file creation, deletion

//Instantiate a file object, pass in the folder directory
        File file = new File("/mnt/sdcard/work/mywork");
        //Instantiate a file object, pass in the file name path.
        File mFile = new File("/mnt/sdcard/work/zhiyuan.txt");
        / / Determine whether the file exists, delete it if it exists
        if (mFile.exists()){
            mFile.delete();
        }
        try {
            //Create a file
            mFile.createNewFile();
            //Give a prompt to show that the creation was successful
            Toast.makeText(getApplicationContext(), "The file was created successfully", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace ();
        }
/ / Determine whether the folder exists, if it does not exist, create it, otherwise do not create it
        if (!file.exists()) {
            //Create a folder that is contained in the <span style="color:#FF0000;"> directory but does not exist</span> through the mkdirs() method of file
            file.mkdirs();
        }

Permission: have write permission to external storage

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Reprinted from: http://blog.csdn.net/lixiang_Y/article/details/54946199?locationNum=2&fps=1







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325893295&siteId=291194637