How do I input My FIle?

时间:2015-08-07 02:31:19

标签: java android development-environment mode

I have dug around and found a function to copy files, my only issue is I do not know where I insert my directory (I'm moving "/data/data/com.android.providers.contacts/databases/contacts2.db" to "/sdcard/tpzb/")

I would appreciate some help very much. All I need is someone to put "FILE TO COPY DIR" and "OUTPUT DIR" in where it goes. I know this is a dumb question but I see many other functions like this, I figure once I see one I will be able to figure it out on my own with out asking. :)

private void moveFile(String inputPath, String inputFile, String outputPath) {

    InputStream in = null;
    OutputStream out = null;
    try {

        //create output directory if it doesn't exist
        File dir = new File (outputPath); 
        if (!dir.exists())
        {
            dir.mkdirs();
        }


        in = new FileInputStream(inputPath + inputFile);        
        out = new FileOutputStream(outputPath + inputFile);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;

            // write the output file
            out.flush();
        out.close();
        out = null;

        // delete the original file
        new File(inputPath + inputFile).delete();  


    } 

1 个答案:

答案 0 :(得分:1)

使用此code复制文件

另外,请勿忘记在AndroidManifest.xml文件

中添加以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

用法

copyFile("/data/data/com.android.providers.contacts/databases/contacts2.db", Environment.getExternalStorageDirectory().toString() + "/tpzb/");