File 文件存储

上代码

//该函数主要用于得到一个文件夹的句柄,并通过该句柄创建和访问外文件夹。
//注意:getDir的第二个参数(Context.MODE_PRIVATE)是指文件夹的访问权限而并不包括其子文件夹和文件的访问权限
//getAbsolutePath()获得绝对路径
String path=context.getDir("dowmload",Context.MODE_PRIVATE).getAbsolutePath() + "/";
‘path输出  /data/user/0/me.bandu.talk.android.phone/app_dowmload/’

文件拷贝

//文件拷贝
    public static int copySdcardFile(InputStream inputStream, String toFile)
    {
        File file = new File(toFile);
        if (file.exists())
            file.delete();
        File dir = new File(toFile.substring(0,toFile.lastIndexOf("/")));
        if (!dir.exists())
            dir.mkdirs();

        try
        {
            file.createNewFile();
            OutputStream fosto = new FileOutputStream(toFile);
            byte bt[] = new byte[1024];
            int c;
            while ((c = inputStream.read(bt)) != -1)
            {
                fosto.write(bt, 0, c);
            }
            fosto.flush();
            fosto.close();
            inputStream.close();
            return 0;

        } catch (Exception ex)
        {
            return -1;
        }
    }

你可能感兴趣的:(File 文件存储)