简体   繁体   English

无法将命令写入我的DataOutputStream

[英]Can't write command into my DataOutputStream

I can't write my command into my DataOutputStream because it needs datatype BYTES , how can i get the bytes from my command? 我无法将命令写入DataOutputStream因为它需要数据类型BYTES ,如何从命令中获取字节?

"tinymix" -> toBytes
"exit" -> toBytes

I somehow need to convert this to Bytes, but how? 我某种程度上需要将其转换为字节,但是如何?

Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});

        DataOutputStream stdin = new DataOutputStream(p.getOutputStream());
        InputStream stdout = p.getInputStream();

        stdin.write("tinymix\n"); // THIS LINE IS BROKEN
        stdin.write("exit\n");   // THIS LINE IS BROKEN

        stdin.flush();
        stdin.close();

You can write a string with (for example) a OutputStreamWriter : 您可以使用(例如) OutputStreamWriter编写字符串:

OutputStreamWriter stdin= new OutputStreamWriter(p.getOutputStream());

instread of using a DataOutputStream . 了解使用DataOutputStream

Your code would look like this: 您的代码如下所示:

Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});

OutputStreamWriter stdin = new OutputStreamWriter(p.getOutputStream());
InputStream stdout = p.getInputStream();

stdin.write("tinymix\n");
stdin.write("exit\n");

stdin.flush();
stdin.close();

You can call getBytes() on a string object to get the string as bytes 您可以在字符串对象上调用getBytes()以字符串形式获取字节

Change this 改变这个

stdin.write("tinymix\n"); // THIS LINE IS BROKEN
stdin.write("exit\n");   // THIS LINE IS BROKEN

into this 进入这个

stdin.write("tinymix\n".getBytes());
stdin.write("exit\n".getBytes());

Try 尝试

stdin.writeUTF("str");

That allows you to write strings using DataOutputStream 这样就可以使用DataOutputStream编写字符串

Then you can use 那你可以用

std.readUTF();

to read your string back 读回你的弦

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM