-1

我知道如何复制粘贴文件,但只需一个按钮即可进行复制和粘贴。

static void copyFile(String readFile,String writeFile ){

    try {
        FileInputStream fi = null;
        FileOutputStream fo = null;

        try {
            fi = new FileInputStream(readFile);
            fo = new FileOutputStream(writeFile);

            //Read File

            byte[] byt = new byte[fi.available()];
            int r =0;
            while((r=fi.read(byt))!=-1){

                //fo.write(byt);
                fo.write(byt, 0, r);
            }
            fi.close();
            fo.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    finally{

    }

但是如何通过一个按钮复制并用另一个按钮粘贴?

4

1 回答 1

0

您可以调用将文件从源文件复制到您onClickEventButton.

onClickEvent 处理程序片段:

 Button button1 = (Button) findViewById(R.id.button_id);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click   
                copy(filesrcpath, filedestpath); // Pass Filesrc and FilePath
            }
        });

Copy()片段,它采用两个参数一个源和另一个目标路径。

public void copy(File src, File dst) throws IOException 
{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);

// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) 
   {
    out.write(buf, 0, len);
    }
in.close();
out.close();
}
于 2013-09-28T12:38:41.990 回答