filewrite未创建新文件android studio,获取'java.io.filenotfoundexception:file.py:open failed:erofs(只读文件系统)'

9bfwbjaz  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(287)

因此,我一直在尝试使用runtime方法在java中运行一个简单的python脚本。在androidstudio之外,它运行得很好,但是在android studio中,我发现了这个错误 java.io.FileNotFoundException: file.py: open failed: EROFS (Read-only file system)' .
我知道这不是通过java运行python脚本的最有效方法,但是请帮助我。我不知道为了编写新脚本要更改什么权限。
这是我的完整代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView resultstv = (TextView)findViewById(R.id.resultsTV);
        Button solvebtn = (Button)findViewById(R.id.solvebtn);
        final EditText enterWordTv =  (EditText)findViewById(R.id.enterWordET);

        solvebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resultstv.setText(anagram(getEnterWordTv(enterWordTv)).toString());
            }
        });
    }

    public String getEnterWordTv(EditText inputFU) {
        String finWord = inputFU.getText().toString();
        return(finWord);
    }

    static ArrayList<String> anagram(Int num){
        ArrayList<String> results = new ArrayList<String>();
        try {
            String mainPrg = "import sys\nprint(sys.argv[1])";
            BufferedWriter out = new BufferedWriter(new FileWriter("AnagremSOlver.py"));
            out.write(mainPrg);
            out.close();
            String number = num;
            Process p = Runtime.getRuntime().exec("python3 AnagremSOlver.py " + number);
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String ret;
            while ((ret = in.readLine()) != null){
                results.add(ret);
            }
            System.out.println(results);
        } catch (Exception e){
            System.out.println(e);
        }
        return(results);
    }

}

其他地方的答复告诉我更新中的权限 AndroidManifest.xml 通过添加 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 但加上它,我还是遇到了同样的问题。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题