java—将一个文件中的内容逐块(例如8字节)交替写入多个文件

cbeh67ev  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(325)

因此,我一直在尝试读取文本文件的内容,并将内容逐块交替写入例如2个新文件中。
我已经尝试了多种方法来实现这一点,但都不起作用(outputstream和fileoutputstream似乎是最合适的)。
在我试着把文件分成三部分,第一部分写在一个文件里,第二部分写在另一个文件里,以此类推。它在outputstream和fileoutputstream中运行得非常好。
但当我想轮流做的时候,它就不起作用了。
我交替地使用循环算法,它本身工作得很好。
如果你能给我举几个例子,我会非常感激的!

public void splitFile(String filePath, int numberOfParts, long sizeOfParts[]) throws FileNotFoundException, IOException, SQLException {
    long bytes = 8;

    OutputStream partsPath[] = new OutputStream[numberOfParts];
    long bytePositition[] = new long[numberOfParts];

    long copy_size[] = new long[numberOfParts];
    for (int i = 0; i < numberOfParts; i++) {
        copy_size[i] = sizeOfParts[i];
        partsPath[i] = new FileOutputStream(path); //Gets Path from my Database (works)
        //System.out.println(cloudsTable.getCloudsPathsFromDatabase(i) + '\\' + name + (i + 1) + fileType);
    }

    InputStream file = new FileInputStream(filePath);

    while (true) {
        boolean done = true;

        for (int i = 0; i < numberOfParts; i++) {

            if (copy_size[i] > 0) {
                done = false;
                if (copy_size[i] > bytes) {
                    copy_size[i] -= bytes;
                    bytePositition[i] += bytes;

                    System.out.println("file " + i + " " + bytePositition[i]);
                    readWrite(file, bytePositition[i], partsPath[i]);
                } else {
                    bytePositition[i] += copy_size[i];

                    System.out.println("rest file " + i + " " + bytePositition[i]);

                    readWrite(file, bytePositition[i], partsPath[i]);
                    copy_size[i] = 0;
                }
            }

        }

        if (done == true) {

            break;
        }

    }
    file.close();
    for (int i = 0; i < partsPath.length; i++) {
        partsPath[i].close();
    }

}

private void readWrite(InputStream file, long bytes, OutputStream path) throws IOException {
    byte[] buf = new byte[(int) bytes];
    while (file.read(buf) != -1) {
        path.write(buf);
        path.flush();
    }

}
代码所做的是,它只在第一个复制的文件中写入原始文件的内容,而下面的文件是空的
编辑:为了澄清代码应该做什么,先将前8个字节写入文件1,将第二个8字节写入文件2,将第三个8字节写入文件3,将第四个8字节写入文件1,依此类推,循环,直到文件1的长度为sizeofparts[0],文件2的长度为sizeofparts[1],文件3的长度为sizeofparts[2]。

50pmv0ei

50pmv0ei1#

主要问题是 readWrite() 方法应该只复制一个8字节的字节块,但是有一个循环使它复制输入文件中所有剩余的字节。
此外,还应增强代码的使用 try-finally 关闭文件,并正确处理文件结尾,以防输入文件短于部分的总和。
我会消除 readWrite() 方法,并合并逻辑以防止重复代码,如下所示:

public void splitFile(String inPath, long[] sizeOfParts) throws IOException, SQLException {
    final int numberOfParts = sizeOfParts.length;

    String[] outPath = new String[numberOfParts];
    // Gets Paths from Database here

    InputStream in = null;
    OutputStream[] out = new OutputStream[numberOfParts];
    try {
        in = new BufferedInputStream(new FileInputStream(inPath));
        for (int part = 0; part < numberOfParts; part++)
            out[part] = new BufferedOutputStream(new FileOutputStream(outPath[part]));

        byte[] buf = new byte[8];
        long[] remain = sizeOfParts.clone();
        for (boolean done = false; ! done; ) {
            done = true;
            for (int part = 0; part < numberOfParts; part++) {
                if (remain[part] > 0) {
                    int len = in.read(buf, 0, (int) Math.min(remain[part], buf.length));
                    if (len == -1) {
                        done = true;
                        break;
                    }
                    remain[part] -= len;
                    System.out.println("file " + part + " " + (sizeOfParts[part] - remain[part]));
                    out[part].write(buf, 0, len);
                    done = false;
                }
            }
        }
    } finally {
        if (in != null)
            in.close();
        for (int part = 0; part < out.length; part++)
            if (out[part] != null)
                out[part].close();
    }
}

相关问题