应该是权限的问题,你是ROOT了吧?
这样试试啊,看能不能在/system/etc 下面新建一个文件?
另外,问一下,你在运行这个程序时,“授权管理”有没有弹出窗口问你是否允许啊?

解决方案 »

  1.   


    已经弹出咯,也允许咯丫 ,能读,就是不打不开 FileOutputStream  流。
      

  2.   


    再试一下,用R.Explorer,直接编辑这个hosts,能保存吗?看看保存后是不是真的变了,,,因为我改试了,用R.Explorer编辑后保存,没变化,怪了,莫非我的ROOT失效了。
      

  3.   


    创建文件的上一层目录是 etc 目录权限 的权限是 dr-
      

  4.   


    请问权限怎么改?  执行Runtime.getRuntime().exec("su");行吗、?
      

  5.   

    可以的   Runtime.getRuntime().exec("chmod 777 " +  destFile.getAbsolutePath())
      

  6.   


    谢谢了 先 不过,这样写,貌似还是不太行丫  还是抛出 java.io.FileNotFoundException: /system/etc/hosts 这样的异常哦
      

  7.   

    Runtime.getRuntime().exec("mount -o remount,rw rootfs /system/etc")
    rootfs 指文件系统格式,每个硬件厂商的可能不一样。
      

  8.   

    public static boolean runRootCommand(String command) { // "chmod 777 " + destFile.getAbsolutePath()
            Process process = null;   
            DataOutputStream os = null;   
            try {   
                    process = Runtime.getRuntime().exec("su");   
                    os = new DataOutputStream(process.getOutputStream());   
                    os.writeBytes(command+"\n");   
                    os.writeBytes("exit\n");   
                    os.flush();   
                    process.waitFor();   
            } catch (Exception e) {   
                    Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: "+e.getMessage());   
                    return false;   
            }   
            finally {   
                 try {   
                     if (os != null) {   
                                os.close();   
                    }   
                    process.destroy();   
                }    
                 catch (Exception e) {   
                            // nothing   
                }   
            }   
            return true;   
    }  麻烦你看下  上面的终端修改权限的代码 不合适吗  谢谢。、。
      

  9.   

    public static boolean runRootCommand(String command) { // "chmod 777 " + destFile.getAbsolutePath()
            Process process = null;   
            DataOutputStream os = null;   
            try {   
                    process = Runtime.getRuntime().exec("su");   
                    os = new DataOutputStream(process.getOutputStream());   
                    os.writeBytes(command+"\n");   
                    os.writeBytes("exit\n");   
                    os.flush();   
                    process.waitFor();   
            } catch (Exception e) {   
                    Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: "+e.getMessage());   
                    return false;   
            }   
            finally {   
                 try {   
                     if (os != null) {   
                                os.close();   
                    }   
                    process.destroy();   
                }    
                 catch (Exception e) {   
                            // nothing   
                }   
            }   
            return true;   
    }  麻烦你看下  上面的终端修改权限的代码 不合适吗  谢谢。、。
      

  10.   

    system 目录下是只读的, 没办法写。
      

  11.   


    貌似真是这样,不支持修改/system
      

  12.   


    如果不支持修改的话,呐  为什么 我new FileOutputStream(filePath)的是时候,hosts文件却被清空了呢,  这个hosts文件时放在/system/下的,而且这个文件本来是有内容的哦 
      

  13.   

    我也试了楼主的代码,没有作用。并且,我在adb shell中也试了,也不行。虽然是#了,但是仍然说Unable to chmod /system/etc/hosts: Read-only file system然后我又用R.Explorer再试,同样不行。
      

  14.   

    adb shell是可以的,要先执行adb remount/system/etc/hosts应该需要root权限,才能修改,你找个有root权限的应用执行下你的代码试验下。
      

  15.   

    有的机器出场的时候 厂商就把system下的文件权限限定在了用户级别,所以你不能修改里面的内容。