文件复制动作错误android

时间:2013-02-10 17:54:12

标签: android file copy action directory

我有这段代码。

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

    File srcFolder = new File(Environment.getExternalStorageDirectory()
            + "/folder/");
    File destFolder = new File(
            "/data/data/folder");





    Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener() {



        @Override
        public void onClick(View v) {


            ~~~~public void~~~~ copyFolder(srcFolder, destFolder);
            {

                if(src.isDirectory()){

                    //if directory not exists, create it
                    if(!dest.exists()){
                       dest.mkdir();
                       Log.i("Status : ", "Directory copied from " 
                                      + srcFolder + "  to " + destFolder);
                    }

                    //list all the directory contents
                    String files[] = src.list();

                    for (String file : files) {
                       //construct the src and dest file structure
                       File srcFile = new File(src, file);
                       File destFile = new File(dest, file);
                       //recursive copy
                       copyFolder(srcFile,destFile);
                    }

                }else{
                    //if file, then copy it
                    //Use bytes stream to support all file types
                    InputStream in = new FileInputStream(src);
                        OutputStream out = new FileOutputStream(dest); 

                        byte[] buffer = new byte[1024];

                        int length;
                        //copy the file content in bytes 
                        while ((length = in.read(buffer)) > 0){
                           out.write(buffer, 0, length);
                        }

                        in.close();
                        out.close();
                        Log.i("Status : ","File copied from " + srcFolder + " to " + destFolder);
                }
            }





        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

它在我用~~~~~~~~标记的地方给了我一个错误。它说令牌上的语法错误,错位的构造可以有人帮助我吗?

如果您帮助我解决此错误,或者给我另一个文件夹复制代码,我将非常感谢!

1 个答案:

答案 0 :(得分:0)

错误是因为你的onClick方法中有一个方法。

您应该只带来指令和运行方法,但不能定义新方法。

解决方案可能是将copyFolder方法定义到您的类中,并在您的onClick方法中运行它。