/**
     * 复制文件
     * @param pathFrom 源文件
     * @param pathTo 目标文件
     * @param overwrite 是否覆盖目标文件
     * @throws IOException
     */
    public static void CopyFile(String pathFrom,String pathTo,boolean overwrite) throws IOException{
        File fileFrom = new File(pathFrom);
        File fileTo = new File(pathTo);
        if(!fileFrom.exists())
           throw new IOException("From file Not Exists! ");
        boolean isToFileExists = fileTo.exists();
        if(overwrite || !isToFileExists){
            if(!isToFileExists){
                fileTo.mkdirs();
                fileTo.delete();    //直接mkdirs,会生成一个文件夹,复制文件的时候会报错...
            }
            else {
                fileTo.delete();
            }
            FileInputStream reader = new FileInputStream(fileFrom);
            FileOutputStream writer = new FileOutputStream(fileTo);
            try
            {
                CopyFile(reader,writer);
            }
            catch (IOException ex){
                throw ex;
            }
            finally {
                reader.close();
                writer.close();
            }        }
        else {
            throw new IOException("To file is Exists!");
        }
    }    public static void CopyFile(InputStream inputStream,OutputStream outputStream) throws IOException{
        byte[] buffer = new byte[64*1024]; //每个包64KB
        //int total = inputStream.available();
        int readCount = 0;
        while ((readCount = inputStream.read(buffer)) > 0){
            outputStream.write(buffer,0,readCount);
        }
    }

解决方案 »

  1.   

    怎么指定啊,我直接用/data/flow.txt过后  程序会报错
      

  2.   

    Environment.getDataDirectory().getPath()
    Environment.getExternalStorageDirectory().getPath()具体文件放在哪,你自己不知道,别人更不知道了
      

  3.   

    请问一下方法怎么使用啊?就比如说上面那个复制文件的代码,我知道文件的路径,怎么获取?怎么弄成上面那个函数的输入,他的输入是一个字符串比如说
    Context context=this;//首先,在Activity里获取context  
    File file=context.getFilesDir();  
    String path=file.getAbsolutePath();  
    //此处返回的路劲为/data/data/包/files,其中的包就是我们建立的主Activity所在的包  
    //我们可以看到这个路径也是在data文件夹下  
    //程序本身是可以对自己的私有文件进行操作  
    //程序中很多私有的数据会写入到私有文件路径下,这也是android为什么对data数据做保护的原因之一 这个里面,我要复制的文件在/data/data/包/files里面,一个叫flow.txt的文件,我要复制出来,到根目录下,public static void CopyFile(String pathFrom,String pathTo,boolean overwrite)这个的输入输出我应该怎么写呢?
    新手,麻烦指教一下