Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将文件从一个文件夹复制到另一个文件夹_Java_Android - Fatal编程技术网

Java 将文件从一个文件夹复制到另一个文件夹

Java 将文件从一个文件夹复制到另一个文件夹,java,android,Java,Android,我试图在Android路径出现时复制该文件,但没有成功,尽管它已经作为AndroidManifest上的权限存在,但似乎该文件不存在或不知道它在那里。 下面是一段代码: public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case FILE_SELECT_CODE: if (resultCode ==

我试图在Android路径出现时复制该文件,但没有成功,尽管它已经作为AndroidManifest上的权限存在,但似乎该文件不存在或不知道它在那里。 下面是一段代码:

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    switch (requestCode) {
        case FILE_SELECT_CODE:
            if (resultCode == RESULT_OK) {

                Uri uri = data.getData();
                String origem = uri.getPath();
                File src = new File(origem);
                if(src.isFile()){
                    Log.d(TAG,src.toString());
                }

                File root = Environment.getExternalStorageDirectory();
                root = new File(root.toString(), "Kcollector/importa/import.csv");

                try {
                    copy(src,root,true);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

public static void copy(File origem, File destino, boolean overwrite) throws IOException{
    Date date = new Date();
    if (destino.exists() && !overwrite){
        System.err.println(destino.getName()+" já existe, ignorando...");
        return;
    }
    FileInputStream fisOrigem = new FileInputStream(origem);
    FileOutputStream fisDestino = new FileOutputStream(destino);
    FileChannel fcOrigem = fisOrigem.getChannel();
    FileChannel fcDestino = fisDestino.getChannel();
    fcOrigem.transferTo(0, fcOrigem.size(), fcDestino);
    fisOrigem.close();
    fisDestino.close();
    Long time = new Date().getTime() - date.getTime();
    System.out.println("Saiu copy"+time);
}
尝试复制时返回的错误:

W/System.err:java.io.FileNotFoundException:/external_storage/Kcollector/importa/kvendasajustado.csv(无此类文件或目录)
W/System.err:在java.io.FileInputStream.open0(本机方法)

检查运行时权限或使用第一节中指示的方法

最后检查url的格式是否正确。
否则,您可以使用断点(搜索这些单词)消除bug,并准确地找到问题的根源

StackOverflow是一个讲英语的网站。请用英语提问。Log.d行(TAG,src.toString());返回文件路径:/external_storage/Kcollector/importa/kvendasajustado.csv?Habla usted ingles?你会说英语吗?对不起,我已经翻译了。你有检查读写权限的方法吗?
public static void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    try {
        OutputStream out = new FileOutputStream(dst);
        try {
            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            out.close();
        }
    } finally {
        in.close();
    }
}