令牌上的文件复制操作Android语法错误



下面的代码在我用~~~~ ~~~~标记的地方给了我一个错误。上面写着:

令牌上的语法错误,错误的构造

有人能帮我吗?

如果你帮我解决这个错误,或者给我另一个文件夹副本代码,我将非常感谢!

    @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;
    }
}

在这一行中,您应该调用函数。并且函数应该在整个onCreate方法之后构造:

@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) {
           copyFolder(srcFolder, destFolder);
        }
    });
}
public void copyFolder(String srcFolder,String  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;
  }
}

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

您应该只带来指令和运行方法,而不是定义一个新方法。

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

相关内容

  • 没有找到相关文章

最新更新