dali0125 2010-01-13 22:35
浏览 226
已采纳

创建文件路径,创建文件后,并追加写入数据?

感谢viperasi (初级程序员)上次关于创建7层文件路径,每层最大5个子文件问题的解答,以下是他给出的程序,在他给出程序的基础上,作了小小的修改,写入数据到子文件中。现在我想可以实现追加写入数据的功能,也就是说在生成一个子文件00后,可以追加数据到00文件中,直到00文件大小10mb后,再创建01文件,而不是每次都创建新的文件,如下图~~~
需要大家的指点,多谢~~~

[code="java"]
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileTest {

    static File root=new File("D://testfile");
public static final int CHILD_LIMIT = 5;// 最大啊子文件数
public static final int DEEP_LIMIT = 7;// 文件夹深度

public static void main(String[] args) {

String file = "d://Java08IO.ppt";
    InputStream input = new FileInputStream(file);
testFile(input, root, 0, false);    
}

public static boolean testFile(InputStream in, File root, int k, boolean nextChild)
        throws IOException {
    int i = 0;// 文件名 个位数
    int j = 0;// 文件名 十位数

    if (root.exists() && root.isDirectory() && root.canRead()
            && root.canWrite()) {
        File[] children = root.listFiles();
        File child = null;
        if (children.length == 0) {// 如果子目录数为0
            child = new File(root, j + "" + i);// 创建新文件对象
            if (k == DEEP_LIMIT) {// 如果深度已达限制则创建文件
                BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(child,true));
                byte[] buffer = new byte[1024];
                 int len;  
                 int off = 0;
                while ( (len = in.read(buffer, off, 1024)) != -1) {
                out.write(buffer, off, len);
                }
              out.flush();
                return true;
            } else {// 否则为目录 并深度递增
                child.mkdir();
                k++;
                nextChild = false;
                return testFile(in, child, k, nextChild);// 递归
            }
        } else if (children.length > 0 && children.length < CHILD_LIMIT) {// 如果有子目录且数量未达到子目录限制
            if (k == DEEP_LIMIT) {// 如果深度已达限制 则创建文件
                i = children.length % 10;
                j = children.length / 10;
                child = new File(root, j + "" + i);// 文件名以当前目录子文件数+1
                BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(child,true));
                byte[] buffer = new byte[1024];
                 int len;  
                 int off = 0;
                while ( (len = in.read(buffer, off, 1024)) != -1) {
                out.write(buffer, off, len);
                }
              out.flush();
                return true;// 返回
            } else {// 否则 则进入目录
                if (nextChild) {// 如果标识为下一个子目录 则文件名为当前子目录数+1
                    i = children.length % 10;
                    j = children.length / 10;
                } else {// 否则 为当前子目录最后一个目录
                    i = (children.length - 1) % 10;
                    j = (children.length - 1) / 10;
                }
                child = new File(root, j + "" + i);
                k++;// 深度递增
                if (!child.exists()) {// 如果子目录不存在 则创建目录
                    child.mkdir();
                    nextChild = false;// 并标识进入当前目录
                }
            }
            return testFile(in, child, k, nextChild);// 递归
        } else if (children.length >= CHILD_LIMIT) {// 如果子目录数已达限制
            nextChild = true;// 标识进入下一个子目录
            return testFile(in, root.getParentFile(), --k, nextChild);// 递归
        }
    } else {
        throw new IOException();
    }
    return false;
}

}
[/code]

[img]http://dl.iteye.com/upload/attachment/193116/3b713de8-860f-31bb-b267-7d112b885b64.jpg[/img]

  • 写回答

2条回答 默认 最新

  • mginobili 2010-01-14 10:31
    关注

    代码如下 其中MAX_SIZE就是文件的最大长度
    目前为1K,便于测试嘛
    lz需要的是10M,也就是1024 * 1024 * 10
    [code="java"]
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;

    public class FileTest {

    static File root=new File("D:\\testfile");
    public static final int CHILD_LIMIT = 5;// 最大啊子文件数
    public static final int DEEP_LIMIT = 7;// 文件夹深度
    public static final long MAX_SIZE = 1024;//文件最大长度
    
    public static void main(String[] args) throws Exception {
    
        String file = "d:\\1.txt";//这个我改了一下,随便塞点内容进去哈
        InputStream input = new FileInputStream(file);
        testFile(input, root, 0, false);    
    }
    
    public static boolean testFile(InputStream in, File root, int k, boolean nextChild)
            throws IOException {
        int i = 0;// 文件名 个位数
        int j = 0;// 文件名 十位数
    
        if (root.exists() && root.isDirectory() && root.canRead()
                && root.canWrite()) {
            File[] children = root.listFiles();
            File child = null;
            if (children.length == 0) {// 如果子目录数为0
                child = new File(root, j + "" + i);// 创建新文件对象
                if (k == DEEP_LIMIT) {// 如果深度已达限制则创建文件
                    BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(child,true));
                    byte[] buffer = new byte[1024];
                     int len;  
                     int off = 0;
                    while ( (len = in.read(buffer, off, 1024)) != -1) {
                    out.write(buffer, off, len);
                    }
                  out.flush();
                    return true;
                } else {// 否则为目录 并深度递增
                    child.mkdir();
                    k++;
                    nextChild = false;
                    return testFile(in, child, k, nextChild);// 递归
                }
            } else if (children.length > 0 && children.length <= CHILD_LIMIT) {// 如果有子目录且数量未达到子目录限制
                if (k == DEEP_LIMIT) {// 如果深度已达限制 则创建文件
                    File lastmodifiedchild = children[children.length - 1];
                    //检查最后一个文件的大小,如果小于10M则追加,如果超过10M则创建下一个文件(当前目录下的文件已超过子文件数目限制的话 到下一个目录重新创建)
                    if(lastmodifiedchild.length() < MAX_SIZE) {
                        //最后一个修改的文件小于10M,追加修改
                        BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(lastmodifiedchild, true));
                        byte[] buffer = new byte[1024];
                         int len;  
                         int off = 0;
                        while ( (len = in.read(buffer, off, 1024)) != -1) {
                            out.write(buffer, off, len);
                        }
                        out.flush();
                    } else {
                        //大于10M
                        if(children.length < CHILD_LIMIT) {
                            //当前目录下 创建一个新的文件
                            i = children.length % 10;
                            j = children.length / 10;
                            child = new File(root, j + "" + i);// 文件名以当前目录子文件数+1
                            BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(child,true));
                            byte[] buffer = new byte[1024];
                             int len;  
                             int off = 0;
                            while ( (len = in.read(buffer, off, 1024)) != -1) {
                                out.write(buffer, off, len);
                            }
                            out.flush();
                        } else {
                            //下一个上级目录 创建一个新文件
                            nextChild = true;// 标识进入下一个子目录
                            return testFile(in, root.getParentFile(), --k, nextChild);// 递归
                        }
                    }
                    return true;// 返回
                } else {// 否则 则进入目录
                    if (nextChild) {// 如果标识为下一个子目录 则文件名为当前子目录数+1
                        i = children.length % 10;
                        j = children.length / 10;
                    } else {// 否则 为当前子目录最后一个目录
                        i = (children.length - 1) % 10;
                        j = (children.length - 1) / 10;
                    }
                    child = new File(root, j + "" + i);
                    k++;// 深度递增
                    if (!child.exists()) {// 如果子目录不存在 则创建目录
                        child.mkdir();
                        nextChild = false;// 并标识进入当前目录
                    }
                }
                return testFile(in, child, k, nextChild);// 递归
            } else if (children.length >= CHILD_LIMIT) {// 如果子目录数已达限制
                nextChild = true;// 标识进入下一个子目录
                return testFile(in, root.getParentFile(), --k, nextChild);// 递归
            }
        } else {
            throw new IOException();
        }
        return false;
    }
    

    }

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏