文件Copy问题,求bug原因

猿来是妳 2013-03-20 10:36:21
package io_rehearsal;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/*
*
*写一个文件copy方法:
*如果copy的对象是文件,则copy src文件到目标dest文件;
*如果copy的对象是目录,则copy src目录下的所有文件和文件夹(包括子文件夹)到dest目录下。
*/
public class MyCopy
{

public static void main(String[] args)
{
try
{
copy("F:/text","E:/java常用文件");

} catch (IOException e)
{
e.printStackTrace();
}

}
//st1为读路径,str2为写路径
public static void copy(String str1,String str2) throws IOException
{
try
{
File f1 = new File(str1);
File f2 = new File(str2);
FileInputStream fis = new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f2);
if(f1.exists())
{
//判断是否是文件
if(f1.isDirectory())
{
//把该目录下的文件用一个File接受,然后再用递归方法重新判断
File[] arr = f1.listFiles();
if(arr != null){
for (File file2 : arr)
{
//返回路径
copy(file2.getName(),str1+File.separator+file2.getName());
}
}

}
//判断是否文件
else if(f1.isFile())
{
//进行文件读写操作
byte[] buffer = new byte[1024*2];
int len = 0;
while((len = fis.read(buffer))!= -1)
{
fos.write(buffer, 0, len);
}
}
}

} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
...全文
271 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
猿来是妳 2013-04-02
  • 打赏
  • 举报
回复
CSDN 果然越来越没技术大牛了
Johnson518 2013-03-21
  • 打赏
  • 举报
回复
引用 11 楼 jike316 的回复:
引用 10 楼 hifinan 的回复: 引用 8 楼 jike316 的回复:/* * *写一个文件copy方法: *如果copy的对象是文件,则copy src文件到目标dest文件; *如果copy的对象是目录,则copy src目录下的所有文件和文件夹(包括子文件夹)到dest目录下。 */ public class MyCopy { public st……
我想把你的代码格式转换得清楚些,方便看,但是也没转换成功,没有将缩进加进代码段
2399 2013-03-21
  • 打赏
  • 举报
回复
引用 10 楼 hifinan 的回复:
引用 8 楼 jike316 的回复:/* * *写一个文件copy方法: *如果copy的对象是文件,则copy src文件到目标dest文件; *如果copy的对象是目录,则copy src目录下的所有文件和文件夹(包括子文件夹)到dest目录下。 */ public class MyCopy { public static void……
没看到哪里整理了,完全一样啊,是直接拷贝文件到指定目录
Johnson518 2013-03-21
  • 打赏
  • 举报
回复
引用 8 楼 jike316 的回复:
/* * *写一个文件copy方法: *如果copy的对象是文件,则copy src文件到目标dest文件; *如果copy的对象是目录,则copy src目录下的所有文件和文件夹(包括子文件夹)到dest目录下。 */ public class MyCopy { public static void main(String[] args) { try { c……
楼主的代码中有两个错误 1.递归调用的copy方法所用的参数有问题,创建File对象需要这个文件的绝对路径值才能真正找到这个文件并创建正确的对象,如果用相对路径只能在这个Class文件所在的当前路径中创建文件。 2.在将源文件内容写入目标文件中时,目标文件得是文件才行(楼主的目标文件是目录) 归整了下jike316的代码,
/*
 * 
 *写一个文件copy方法: 
 *如果copy的对象是文件,则copy src文件到目标dest文件; 
 *如果copy的对象是目录,则copy src目录下的所有文件和文件夹(包括子文件夹)到dest目录下。
 */
public class MyCopy {

public static void main(String[] args) {
try {
copy("D:\\test1", "D:\\test2");

} catch (IOException e) {
e.printStackTrace();
}

}

// st1为读路径,str2为写路径
public static void copy(String str1, String str2) throws IOException {
try {
File f1 = new File(str1);
if (f1.exists()) {
// 判断是否是文件
if (f1.isDirectory()) {
// 把该目录下的文件用一个File接受,然后再用递归方法重新判断
File[] arr = f1.listFiles();
if (arr != null) {
for (File file2 : arr) {
// 返回路径
copy(str1+File.separator+file2.getName(),str2);
}
}
}
// 判断是否文件
else if (f1.isFile()) {
// 进行文件读写操作
copyFile(str1,str2);
}
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static boolean copyFile(String file1,String dir) throws IOException{
FileInputStream fis = new FileInputStream(file1);
File srcFile = new File(file1);
File dFile = new File(dir+File.separator+srcFile.getName());
if(dFile.exists()){
System.out.println("Already exists not copy");
return false;
}
FileOutputStream fos = new FileOutputStream(dir+File.separator+srcFile.getName());
int b =0;
while((b=fis.read())!=-1){
fos.write(b);
}
fis.close();
fos.close();
return false;
}

} 
冰思雨 2013-03-21
  • 打赏
  • 举报
回复
copy(file2.getName(),str1+File.separator+file2.getName());
这个方法的参数有问题。 你看一下你这个方法的声明部分,第一个参数,是源文件的全路径名,你只输入一个文件名, 没有路径部分,无法找到该文件;第二个参数,是目标文件的全路径名,路径错了,取成源路径了 应该取目标路径(str2)。
//进行文件读写操作
                    byte[] buffer = new byte[1024*2];
                    int len  = 0;
                    while((len = fis.read(buffer))!= -1)
                    {
                        fos.write(buffer, 0, len);
                    }
拷贝文件数据的代码,应该要close掉输入输出流。
2399 2013-03-21
  • 打赏
  • 举报
回复
/* * *写一个文件copy方法: *如果copy的对象是文件,则copy src文件到目标dest文件; *如果copy的对象是目录,则copy src目录下的所有文件和文件夹(包括子文件夹)到dest目录下。 */ public class MyCopy { public static void main(String[] args) { try { copy("D:\\test1", "D:\\test2"); } catch (IOException e) { e.printStackTrace(); } } // st1为读路径,str2为写路径 public static void copy(String str1, String str2) throws IOException { try { File f1 = new File(str1); if (f1.exists()) { // 判断是否是文件 if (f1.isDirectory()) { // 把该目录下的文件用一个File接受,然后再用递归方法重新判断 File[] arr = f1.listFiles(); if (arr != null) { for (File file2 : arr) { // 返回路径 copy(str1+File.separator+file2.getName(),str2); } } } // 判断是否文件 else if (f1.isFile()) { // 进行文件读写操作 copyFile(str1,str2); } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static boolean copyFile(String file1,String dir) throws IOException{ FileInputStream fis = new FileInputStream(file1); File srcFile = new File(file1); File dFile = new File(dir+File.separator+srcFile.getName()); if(dFile.exists()){ System.out.println("Already exists not copy"); return false; } FileOutputStream fos = new FileOutputStream(dir+File.separator+srcFile.getName()); int b =0; while((b=fis.read())!=-1){ fos.write(b); } fis.close(); fos.close(); return false; } }
失落夏天 2013-03-21
  • 打赏
  • 举报
回复
百度第一个,试了,能用

public static void copyDir(String sourceDirPath, String targetDirPath) throws IOException {
        // 创建目标文件夹
        (new File(targetDirPath)).mkdirs();
        // 获取源文件夹当前下的文件或目录
        File[] file = (new File(sourceDirPath)).listFiles();
        for (int i = 0; i < file.length; i++) {
            if (file[i].isFile()) {
                // 复制文件
                String type = file[i].getName().substring(file[i].getName().lastIndexOf(".") + 1);

                if (type.equalsIgnoreCase("txt"))
                    FileUtil.copyFile(file[i], new File(targetDirPath + file[i].getName()), MTOServerConstants.CODE_UTF_8,
                            MTOServerConstants.CODE_GBK);
                else
                    FileUtil.copyFile(file[i], new File(targetDirPath + file[i].getName()));
            }
            if (file[i].isDirectory()) {
                // 复制目录
                String sourceDir = sourceDirPath + File.separator + file[i].getName();
                String targetDir = targetDirPath + File.separator + file[i].getName();
                FileUtil.copyDirectiory(sourceDir, targetDir);
            }
        }
    }

    /**
     * 读取文件中内容
     * 
     * @param path
     * @return
     * @throws IOException
     */
    public static String readFileToString(String path) throws IOException {
        String resultStr = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(path);
            byte[] inBuf = new byte[2000];
            int len = inBuf.length;
            int off = 0;
            int ret = 0;
            while ((ret = fis.read(inBuf, off, len)) > 0) {
                off += ret;
                len -= ret;
            }
            resultStr = new String(new String(inBuf, 0, off, MTOServerConstants.CODE_GBK).getBytes());
        } finally {
            if (fis != null)
                fis.close();
        }
        return resultStr;
    }

    /**
     * 文件转成字节数组
     * 
     * @param path
     * @return
     * @throws IOException
     */
    public static byte[] readFileToBytes(String path) throws IOException {
        byte[] b = null;
        InputStream is = null;
        File f = new File(path);
        try {
            is = new FileInputStream(f);
            b = new byte[(int) f.length()];
            is.read(b);
        } finally {
            if (is != null)
                is.close();
        }
        return b;
    }

    /**
     * 将byte写入文件中
     * 
     * @param fileByte
     * @param filePath
     * @throws IOException
     */
    public static void byteToFile(byte[] fileByte, String filePath) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream(new File(filePath));
            os.write(fileByte);
            os.flush();
        } finally {
            if (os != null)
                os.close();
        }
    }
lccc158848537 2013-03-20
  • 打赏
  • 举报
回复
另外copy函数体中递归的第一个参数不能是文件名,应该是文件路径
猿来是妳 2013-03-20
  • 打赏
  • 举报
回复
引用 2 楼 lccc158848537 的回复:
这个明显是没有找到这个目录哈,看看源目录是否存在
引用 3 楼 lccc158848537 的回复:
另外copy函数体中递归的第二个参数写错了
这个原因都是 源文件肯定是存在的,递归那个改过来了也一样错!
dracularking 2013-03-20
  • 打赏
  • 举报
回复
引用 1 楼 invincible_lanqi 的回复:
java.io.FileNotFoundException: F:\text (拒绝访问。) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at io_rehearsal.MyCopy.copy(MyCopy.java:……
FileInputStream是要求必须读取到文件信息,不可以是文件夹 If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a FileNotFoundException is thrown. 楼主应该在确定的文件上建立文件输入流
猿来是妳 2013-03-20
  • 打赏
  • 举报
回复
肯定存在哈!
lccc158848537 2013-03-20
  • 打赏
  • 举报
回复
另外copy函数体中递归的第二个参数写错了
lccc158848537 2013-03-20
  • 打赏
  • 举报
回复
这个明显是没有找到这个目录哈,看看源目录是否存在
猿来是妳 2013-03-20
  • 打赏
  • 举报
回复
java.io.FileNotFoundException: F:\text (拒绝访问。) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at io_rehearsal.MyCopy.copy(MyCopy.java:37) at io_rehearsal.MyCopy.main(MyCopy.java:22) 错误在这里。传入一个文件路径就OK,目录的话就出现这错误,反正没法拷贝,是不是思路错了?

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧