简体   繁体   中英

Android Path is changed: Error Copy file from one folder to another

I want to copy the file from one location to another in my local system directories. There are two folders where one is source and another one is destination. I have added the correct directory path but in the log-cat it is taking different directory path and displaying Exception

The code which i am coding is onto

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.FileChannel;

import org.apache.commons.io.FileUtils;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.widget.Button;
public class AsyncTask extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            String input = "D:\\temp\\red.txt"; //file path is correct 
            InputStream in;
            OutputStream out;
            try {
                in = new FileInputStream(input);
                out = new FileOutputStream("D:\\temp5\\red.txt");
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
}

Logcat

07-09 02:44:01.414: W/System.err(2330): java.io.FileNotFoundException: /D:\temp\red.txt: open failed: ENOENT (No such file or directory)
07-09 02:44:01.414: W/System.err(2330):     at libcore.io.IoBridge.open(IoBridge.java:409)
07-09 02:44:01.424: W/System.err(2330):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
07-09 02:44:01.424: W/System.err(2330):     at java.io.FileInputStream.<init>(FileInputStream.java:105)
07-09 02:44:01.424: W/System.err(2330):     at com.prgguru.example.AsyncTaskExample.onCreate(AsyncTaskExample.java:52)
07-09 02:44:01.434: W/System.err(2330):     at android.app.Activity.performCreate(Activity.java:5231)
07-09 02:44:01.434: W/System.err(2330):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-09 02:44:01.434: W/System.err(2330):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-09 02:44:01.434: W/System.err(2330):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-09 02:44:01.434: W/System.err(2330):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-09 02:44:01.434: W/System.err(2330):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-09 02:44:01.434: W/System.err(2330):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-09 02:44:01.434: W/System.err(2330):     at android.os.Looper.loop(Looper.java:136)
07-09 02:44:01.444: W/System.err(2330):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-09 02:44:01.454: W/System.err(2330):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 02:44:01.454: W/System.err(2330):     at java.lang.reflect.Method.invoke(Method.java:515)
07-09 02:44:01.454: W/System.err(2330):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-09 02:44:01.464: W/System.err(2330):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-09 02:44:01.464: W/System.err(2330):     at dalvik.system.NativeStart.main(Native Method)
07-09 02:44:01.464: W/System.err(2330): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-09 02:44:01.474: W/System.err(2330):     at libcore.io.Posix.open(Native Method)
07-09 02:44:01.474: W/System.err(2330):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-09 02:44:01.484: W/System.err(2330):     at libcore.io.IoBridge.open(IoBridge.java:393)
07-09 02:44:01.484: W/System.err(2330):     ... 17 more

how can you be sure your path is correct? i assume you want to copy a file from your external sdcard to another dir on your external sdcard:

//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File oldFile = new File(sdcard,"/temp/red.txt");

File newFile = new File(sdcard,"/temp5/red.txt");

now you can copy your file with this copy-function:

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();
}

links:

How can I read a text file from the SD card in Android?

How to make a copy of a file in android?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM