package test;import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;public class IoTest {        public StringBuffer fileDoc(File path,StringBuffer sb){
                //目录列表数组
                File[] files = path.listFiles();
                
                //字节流读取
                FileInputStream fileInputStream = null;
                
                for(File file1 : files){
                        if(file1.isDirectory()){//如果是目录
                                //递归
                                fileDoc(file1,sb);
                        }else{
                                try {
                                        fileInputStream = new FileInputStream(file1);
                                        int size = fileInputStream.available();
                                        byte[] tempByte = new byte[size];
                                        
                                        if(fileInputStream.read(tempByte) != size){
                                                System.out.println("文件读取失败!!!");
                                        }else{
                                                //内容转换
                                                String change = new String(tempByte,"utf-8");
                                                
                                                sb.append(change);
                                        }
                                        
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                }
                
                return sb;
        }
        
        //保存数据
        public static void saveDataForTxt(StringBuffer sbStr){
                //总数据
                String allStr = sbStr.toString();
                byte[] bytes = allStr.getBytes();
                
                //将数据放到字节输入流中
                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
                
                //1k的数据
                byte[] newbytes = new byte[1024];
                
                int len = 0;
                int i = 1;
                
                try {
                        while((len = bais.read(newbytes)) != -1){
                        File file = new File("d:\\java\\"+i+".txt");
                        new FileOutputStream(file).write(newbytes,0,len);
                        i++;
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
        
        
        public static void main(String[] args) {
                String path = "d:\\TDDownload\\Test1";
                File file = new File(path);
                
                //要得到的数据集合(初始)
                StringBuffer sbTemp = new StringBuffer();
                
                sbTemp = new IoTest().fileDoc(file,sbTemp);
                
                saveDataForTxt(sbTemp);
        }
}

解决方案 »

  1.   

    这程序想做啥操作?将一个文件(夹)转化为utf-8格式?
      

  2.   

    编写一个程序,将一个目录及其子目录下的所有txt类型的文本文件中的内容合并到若干个新的文本文件中,当第一个新产生的文件中存储的内容达到1Mbytes时,剩下的内容存储到第二个文件中,依次往下,新产生的文本文件名依次为1.txt、2.txt、……。
      

  3.   

    LZ没什么错的,只要在D盘下有JAVA 这个文件夹就好了……我自己的机子上没有出现差错    也很好写……
      

  4.   

    并且所有的文件大小为    1KB   完全没任何问题 try {
                            while((len = bais.read(newbytes)) != -1){
                            File file = new File("d:\\java\\"+i+".txt");
                            new FileOutputStream(file).write(newbytes,0,len);//之前我以为是len的大小问题,后来我-1之后发现没区别
                            i++;
                            }
                    } catch (Exception e) {
                            e.printStackTrace();
                    }
      

  5.   


    public class FileDeal {
    private static final int MAX_SIZE = 1024 * 1022;
    private static final int BUFFER_SIZE = 4 * 1024;
    private static List<File> list = new ArrayList<File>();
    private static int currteFileNo = 0; private FileDeal() {
    } // 搜索所有文件,放入File
    private static void searchAllFiles(File file) {
    if (!file.exists()) {
    return;
    } if (file.isDirectory()) {
    for (File f : file.listFiles()) {
    searchAllFiles(f);
    }
    } else {
    list.add(file);
    }
    } /**
     * 搜索srcPath文件下的所有文件,合并保存到dstPath目录下,当文件大于1M时,自动分割
     */
    public static void writeToFile(String srcPath, String dstPath)
    throws IOException {
    searchAllFiles(new File(srcPath));
    FileInputStream in = null;
    FileOutputStream out = null;
    byte[] buffer = new byte[BUFFER_SIZE];
    try {
    File dstFile = new File(dstPath + File.separator + currteFileNo
    + ".txt");
    out = new FileOutputStream(dstFile);
    for (File f : list) {
    try {
    in = new FileInputStream(f);
    int len = -1;
    while ((len = in.read(buffer)) != -1) {
    out.write(buffer, 0, len);
    out.flush();

    //当文件大于1M时,进行分割
    if (dstFile.length() >= MAX_SIZE) {
    out.close();
    out = null;
    currteFileNo++;
    dstFile = new File(dstPath + File.separator
    + currteFileNo + ".txt");
    out = new FileOutputStream(dstFile);
    }
    }
    } finally {
    if (in != null) {
    in.close();
    in = null;
    }
    }
    }
    } finally {
    if (out != null) {
    out.close();
    out = null;
    }
    }
    }
    }
      

  6.   

    对了,关于  转换成UTF-8的编码格式,我有一点点一问:我用notepad++ 打开 1.txt的文档时,发现  编码  是ANSI的编码格式,  源文件   也是  ANSI的编码格式,貌似这句没作用
      

  7.   


    改进建议:
    1. 所有文件内容装入StringBuffer会导致内存溢出。
    2. 只有1KB时分割,而不是1M
    3. 单个文件大小不足1K时,文件读取、写入会出错。
    4. 未对读入的文件流进行关闭。
    5. fileDoc方法不需要返回值。
    6. File[] files = path.listFiles();  当path为文件而非文件夹时,将会报空指针异常。
      

  8.   


    LZ指的是   1Mbyte  =1024byte = 1KB    亲,懂了吧?
      

  9.   

    1Mbyte和我们说的1M有什么区别么?
      

  10.   

    public class FileDeal {
        private static final int MAX_SIZE = 1024 * 1022;
        private static final int BUFFER_SIZE = 4 * 1024;
        private static List<File> list = new ArrayList<File>();
        private static int currteFileNo = 0;    private FileDeal() {
        }    // 搜索所有文件,放入File
        private static void searchAllFiles(File file) {
            if (!file.exists()) {
                return;
            }        if (file.isDirectory()) {
                for (File f : file.listFiles()) {
                    searchAllFiles(f);
                }
            } else {
                list.add(file);
            }
        }    /**
         * 搜索srcPath文件下的所有文件,合并保存到dstPath目录下,当文件大于1M时,自动分割
         */
        public static void writeToFile(String srcPath, String dstPath)
                throws IOException {
            searchAllFiles(new File(srcPath));
            FileInputStream in = null;
            FileOutputStream out = null;
            byte[] buffer = new byte[BUFFER_SIZE];
            try {
                File dstFile = new File(dstPath + File.separator + currteFileNo
                        + ".txt");
                out = new FileOutputStream(dstFile);
                for (File f : list) {
                    try {
                        in = new FileInputStream(f);
                        int len = -1;
                        while ((len = in.read(buffer)) != -1) {
                            out.write(buffer, 0, len);
                            out.flush();
                            
                            //当文件大于1M时,进行分割
                            if (dstFile.length() >= MAX_SIZE) {
                                out.close();
                                out = null;
                                currteFileNo++;
                                dstFile = new File(dstPath + File.separator
                                        + currteFileNo + ".txt");
                                out = new FileOutputStream(dstFile);
                            }
                        }
                    } finally {
                        if (in != null) {
                            in.close();
                            in = null;
                        }
                    }
                }
            } finally {
                if (out != null) {
                    out.close();
                    out = null;
                }
            }
        }
    }
      

  11.   

    public class FileDeal {
        private static final int MAX_SIZE = 1024 * 1022;
        private static final int BUFFER_SIZE = 4 * 1024;
        private static List<File> list = new ArrayList<File>();
        private static int currteFileNo = 0;    private FileDeal() {
        }    // 搜索所有文件,放入File
        private static void searchAllFiles(File file) {
            if (!file.exists()) {
                return;
            }        if (file.isDirectory()) {
                for (File f : file.listFiles()) {
                    searchAllFiles(f);
                }
            } else {
                list.add(file);
            }
        }    /**
         * 搜索srcPath文件下的所有文件,合并保存到dstPath目录下,当文件大于1M时,自动分割
         */
        public static void writeToFile(String srcPath, String dstPath)
                throws IOException {
            searchAllFiles(new File(srcPath));
            FileInputStream in = null;
            FileOutputStream out = null;
            byte[] buffer = new byte[BUFFER_SIZE];
            try {
                File dstFile = new File(dstPath + File.separator + currteFileNo
                        + ".txt");
                out = new FileOutputStream(dstFile);
                for (File f : list) {
                    try {
                        in = new FileInputStream(f);
                        int len = -1;
                        while ((len = in.read(buffer)) != -1) {
                            out.write(buffer, 0, len);
                            out.flush();
                            
                            //当文件大于1M时,进行分割
                            if (dstFile.length() >= MAX_SIZE) {
                                out.close();
                                out = null;
                                currteFileNo++;
                                dstFile = new File(dstPath + File.separator
                                        + currteFileNo + ".txt");
                                out = new FileOutputStream(dstFile);
                            }
                        }
                    } finally {
                        if (in != null) {
                            in.close();
                            in = null;
                        }
                    }
                }
            } finally {
                if (out != null) {
                    out.close();
                    out = null;
                }
            }
        }
    }
      

  12.   


    eclipse 3.5.2
    jdk 1.6.0_25
    能通过编译....代码不能通过编译的地反会有提示,请根据提示修改代码。
      

  13.   


    package a;import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;public class IoTest {    public StringBuffer fileDoc(File path, StringBuffer sb) {
            // 目录列表数组
            File[] files = path.listFiles();        // 字节流读取
            FileInputStream fileInputStream = null;        for (File file1 : files) {
                if (file1.isDirectory()) {// 如果是目录
                    // 递归
                    fileDoc(file1, sb);
                } else {
                    try {
                        fileInputStream = new FileInputStream(file1);
                        int size = fileInputStream.available();
                        byte[] tempByte = new byte[size];                    if (fileInputStream.read(tempByte) != size) {
                            System.out.println("文件读取失败!!!");
                        } else {
                            // 内容转换
                            String change = new String(tempByte, "utf-8");                        sb.append(change);
                        }                } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }        return sb;
        }    // 保存数据
        public static void saveDataForTxt(StringBuffer sbStr) {
            // 总数据
            String allStr = sbStr.toString();
            byte[] bytes = allStr.getBytes();        // 将数据放到字节输入流中
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);        // 1k的数据
            byte[] newbytes = new byte[1024];        int len = 0;
            int i = 1;
            int cnt = 0;
            int totleCnt = 1024 * 1024; // 1M
            File file = null; //1、在 try外面定义 
            FileOutputStream fos = null;
            try {
                while ((len = bais.read(newbytes)) != -1) {
                    if (cnt == 0) { //刚开始读取 或者刚读完1M的时候需要重新new一个file 和fos ,写入
                        file = new File("d:\\java\\" + i + ".txt");
                        fos = new FileOutputStream(file, true); //2、以追加的模式
                    }
                    fos.write(newbytes, 0, len);
                    cnt += len;
                    if (cnt == totleCnt) {
                        cnt = 0 ;
                        i++;
                        fos.flush() ; //3 、写完1M,应该把 fos flush() ,然后关闭 ;
                        fos.close() ;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    public static void main(String[] args) {
            String path = "d:\\Java\\Test1";
            File file = new File(path);        // 要得到的数据集合(初始)
            StringBuffer sbTemp = new StringBuffer();        sbTemp = new IoTest().fileDoc(file, sbTemp);        saveDataForTxt(sbTemp);
        }
    }
      

  14.   

    各位,我用JCreator和JDK1.40编译,通不过,你们那里能通过?
      

  15.   

    您写的代码在我的JCreator里也通不过,报一堆的错.
      

  16.   

    请问谁知道使用JDK1.4,怎么改上面的代码,据说JDK1.5能通过?
      

  17.   

    好了,会改了!
    这个表达式是java1.5新增的,在java1.4中没有把这句话改为
    Java code
    for (int i = 0; i < files.length; i++)
    {
        //for里面的内容中,将file1改成files[i]
        ......
    }结果就通过了!谢谢.