A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

废话不说,下面直接上代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Demo3 {

        public static void main(String[] args) throws IOException {

                File f1 = new File("E:\\java2345");
               
                File f2 = new File("G:\\");
               
                copy(f1,f2);
               
        }
       
        public static void copy(File f1,File f2) throws IOException {                                       
               
                if(f1==null)
                        return;               
               
                String[] filenames = f1.list();
               
                for(String filename : filenames)
                       
                        File fl1 = new File(f1,filename);------------------------->这句提示出错....纠结,实在想不通哪里错了                               
                       
                        File fl2 = new File(f2,f1.getName());

                        if(f1.isDirectory()) {               

                                f2.mkdir();
                               
                                copy(f1,fl2);                               
                        }
                        else {                                       
                                copyFile(f1, fl2);
                        }
                }       

        public static void copyFile(File f1, File f2) {

                InputStream in = null;                       
                OutputStream out = null;
                try {
                        in = new FileInputStream(f1);
                    out = new FileOutputStream(f2);
                    readAndwriter(in, out);
                }
                catch(IOException e ) {                       
                        e.printStackTrace();
                }               
                finally {                       
                        ioClose(in, out);
                }
        }

        private static void readAndwriter(InputStream in, OutputStream out)
                        throws IOException {
                byte[] byts = new byte[1024];

                int len;

                while((len = in.read(byts))!=-1){

                    out.write(byts,0,len);

                }
        }

        private static void ioClose(InputStream in, OutputStream out) {
                if(in!=null) {
                        try {
                                in.close();
                        }
                        catch(IOException e){
                               
                                e.printStackTrace();
                        }
                }
                if(out!=null) {
                        try {
                                out.close();
                        }
                        catch(IOException e){
                               
                                e.printStackTrace();
                        }
                }
        }
}

该贴已经同步到 人皇战天的微博

4 个回复

正序浏览
哈哈哈,终于搞定了,果然是copy方法里的问题...
改动后copy(File f1,File f2)的方法代码如下:
public static void copy(File f1,File f2) throws IOException {                                       
               
                if(f1==null)
                        return;               
       
                if(f1.isDirectory()) {
                        f2 = new File(f2,f1.getName());
                        f2.mkdirs();
                }
                String[] filenames = f1.list();
                for(String filename : filenames){                                               

                        File fl1 = new File(f1,filename);                               

                        if(fl1.isDirectory()) {
                                copy(fl1,f2);
                        }
                        else {
                                copyFile(fl1, new File(f2,filename));
                        }
                }
        }
回复 使用道具 举报
试了下你的代码... 仔细观察下,,你的for 语句后面好像没加{} 这个吧。。  整个程序的逻辑都变了
回复 使用道具 举报
本帖最后由 郑苑东 于 2012-4-7 23:44 编辑

好吧。。我终于知道了。。你写了个死循环。。。最好先找找,,,
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Demo3 {
       
        public static void main(String[] args) throws IOException {
       
                File f1 = new File("D:\\ppppppppppppppp");
               
                File f2 = new File("D:\\o");
               
                copy(f1, f2);
               
        }
       
        public static void copy(File f1, File f2) throws IOException {
       
                if (f1 == null)
                        return;
               
                String[] filenames = f1.list();
               
                for (int i = 0; i < filenames.length; i++) {
                        System.out.println(filenames);
                        File fl1 = new File(f1, filenames);
                        // ------------------------->这句提示出错....纠结,实在想不通哪里错了
                       
                        File fl2 = new File(f2, fl1.getName());
                       
                        if (fl1.isDirectory()) {
                               
                                fl2.mkdir();
                               
                                copy(fl1, fl2);
                        }
                        else {
                                copyFile(fl1, fl2);
                        }
                }
        }
       
        public static void copyFile(File f1, File f2) {
       
                InputStream in = null;
                OutputStream out = null;
                try {
                        in = new FileInputStream(f1);
                        out = new FileOutputStream(f2);
                        readAndwriter(in, out);
                }
                catch (IOException e) {
                        e.printStackTrace();
                }
                finally {
                        ioClose(in, out);
                }
        }
       
        private static void readAndwriter(InputStream in, OutputStream out) throws IOException {
       
                byte[] byts = new byte[1024];
               
                int len;
               
                while ((len = in.read(byts)) != -1) {
                       
                        out.write(byts, 0, len);
                       
                }
        }
       
        private static void ioClose(InputStream in, OutputStream out) {
       
                if (in != null) {
                        try {
                                in.close();
                        }
                        catch (IOException e) {
                               
                                e.printStackTrace();
                        }
                }
                if (out != null) {
                        try {
                                out.close();
                        }
                        catch (IOException e) {
                               
                                e.printStackTrace();
                        }
                }
        }
}
用for加强的。。。
回复 使用道具 举报
本帖最后由 郑苑东 于 2012-4-7 23:53 编辑

改动后的。。还有仔细看看我的代码。。你代码逻辑错误了。。。。
package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Demo3 {
       
        public static void main(String[] args) throws IOException {
       
                File f1 = new File("D:\\ppppppppppppppp");
               
                File f2 = new File("D:\\o");
               
                copy(f1, f2);
               
        }
       
        public static void copy(File f1, File f2) throws IOException {
       
                if (f1 == null)
                        return;
               
                String[] filenames = f1.list();
               
                for (int i = 0; i < filenames.length; i++) {
                        System.out.println(filenames);
                        File fl1 = new File(f1, filenames);
                        // ------------------------->这句提示出错....纠结,实在想不通哪里错了
                       
                        File fl2 = new File(f2, fl1.getName());
                       
                        if (fl1.isDirectory()) {
                               
                                fl2.mkdir();
                               
                                copy(fl1, fl2); // 这个地方错了。。而且害我建了两个不能删除的文件夹。。自己也要小心点。。你原来的死循环了。。一直传入f1.。。
                        }
                        else {
                                copyFile(fl1, fl2);  ///这个地方也错了。。
                        }
                }
        }
       
        public static void copyFile(File f1, File f2) {
       
                InputStream in = null;
                OutputStream out = null;
                try {
                        in = new FileInputStream(f1);
                        out = new FileOutputStream(f2);
                        readAndwriter(in, out);
                }
                catch (IOException e) {
                        e.printStackTrace();
                }
                finally {
                        ioClose(in, out);
                }
        }
       
        private static void readAndwriter(InputStream in, OutputStream out) throws IOException {
       
                byte[] byts = new byte[1024];
               
                int len;
               
                while ((len = in.read(byts)) != -1) {
                       
                        out.write(byts, 0, len);
                       
                }
        }
       
        private static void ioClose(InputStream in, OutputStream out) {
       
                if (in != null) {
                        try {
                                in.close();
                        }
                        catch (IOException e) {
                               
                                e.printStackTrace();
                        }
                }
                if (out != null) {
                        try {
                                out.close();
                        }
                        catch (IOException e) {
                               
                                e.printStackTrace();
                        }
                }
        }
}

你的代码让我栈溢出两次啊 。。。加引号 把所有的加入大夸号中。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马