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

我想知道如何读取内部存储器中某些文件的值,但这些文件不在“/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;

        }



    }


}


森林海
浏览 250回答 1
1回答

GCT1015

由于您的应用程序具有 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")));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java