为了账号安全,请及时绑定邮箱和手机立即绑定

从内存中读取文件:“/sys/class/”和“/dev/”文件夹

从内存中读取文件:“/sys/class/”和“/dev/”文件夹

森林海 2022-06-23 15:56:40
我想知道如何读取内部存储器中某些文件的值,但这些文件不在“/data/data/myapp/files”文件夹中,它们位于“/dev/”和“/”文件夹中sys/class”文件夹。import...public class Calentando extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate( savedInstanceState );        getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,                WindowManager.LayoutParams.FLAG_FULLSCREEN );        setContentView( R.layout.activity_calentando );    @RequiresApi(api = Build.VERSION_CODES.KITKAT)    private byte[] readFile(String path) {        File file = new File( "/sys/class/gpio/gpio33/value" );        try (FileInputStream fis = new FileInputStream( file );             BufferedInputStream bis = new BufferedInputStream( fis )) {            byte[] buffer = new byte[4096];            int bytesRead;            ByteArrayOutputStream baos = new ByteArrayOutputStream();            while ((bytesRead = bis.read( buffer )) != -1) {                baos.write( buffer, 0, bytesRead );           TextView Tempview = (TextView) findViewById( R.id.temperatura );                Tempview.setText( new String( readFile( path ), Charset.forName( "UTF-8" ) ) );            }            return baos.toByteArray();        } catch (IOException e) {            // handle the exception            return null;        }    }}
查看完整描述

1 回答

?
GCT1015

TA贡献1827条经验 获得超4个赞

由于您的应用程序具有 root 权限,您可能能够访问/dev和/sys/class目录。


您可以列出目录内容:


new File(path).listFiles();

您可以读取二进制文件内容:


private byte[] readFile(String path) {

    File file = new File(path);

    try (FileInputStream fis = new FileInputStream(file);

    BufferedInputStream bis = new BufferedInputStream(fis)) {

        byte[] buffer = new byte[4096];

        int bytesRead;

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        while ((bytesRead = bis.read(buffer)) != -1) {

            baos.write(buffer, 0, bytesRead);

        }

        return baos.toByteArray();

    } catch (IOException e) {

        // handle the exception

        return null;

    }

}

您可以在 a 中设置文件内容TextView(前提是它是文本文件):


TextView Tempview;

Tempview = (TextView) findViewById( R.id.temperatura );

Tempview.setText(new String(readFile(path), Charset.forName("UTF-8")));


查看完整回答
反对 回复 2022-06-23
  • 1 回答
  • 0 关注
  • 242 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号