Android路径已更改:错误将文件从一个文件夹复制到另一个文件夹

时间:2014-07-09 06:54:35

标签: java android android-activity

我想将文件从本地系统目录中的一个位置复制到另一个位置。有两个文件夹,其中一个是源,另一个是目标。我添加了正确的目录路径,但是在log-cat中它采用了不同的目录路径并显示了Exception

我编码的代码是

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

1 个答案:

答案 0 :(得分:0)

你怎么能确定你的道路是正确的?我假设您要将外部SD卡中的文件复制到外部SD卡上的另一个目录:

//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");

现在您可以使用此复制功能复制文件:

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

链接:

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

How to make a copy of a file in android?