java—将文件拆分为大小大于127的块

2cmtqfgy  于 2021-05-30  发布在  Hadoop
关注(0)|答案(2)|浏览(315)

我试图为分布式系统课程的最后一个项目制作一个简化的hdfs(hadoop分布式文件系统)。
所以,我要做的第一件事就是写一个程序,把一个任意文件分成任意维的块。
我发现了这个有用的示例,代码是:

package javabeat.net.io;

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

/**
 * Split File Example
 *
 * @author Krishna
 *
 */
public class SplitFileExample {
    private static String FILE_NAME = "TextFile.txt";
    private static byte PART_SIZE = 5;
    public static void main(String[] args) {
        File inputFile = new File(FILE_NAME);
        FileInputStream inputStream;
        String newFileName;
        FileOutputStream filePart;
        int fileSize = (int) inputFile.length();
        int nChunks = 0, read = 0, readLength = PART_SIZE;
        byte[] byteChunkPart;
        try {
            inputStream = new FileInputStream(inputFile);
            while (fileSize > 0) {
                if (fileSize <= 5) {
                    readLength = fileSize;
                }
                byteChunkPart = new byte[readLength];
                read = inputStream.read(byteChunkPart, 0, readLength);
                fileSize -= read;
                assert (read == byteChunkPart.length);
                nChunks++;
                newFileName = FILE_NAME + ".part"
                        + Integer.toString(nChunks - 1);
                filePart = new FileOutputStream(new File(newFileName));
                filePart.write(byteChunkPart);
                filePart.flush();
                filePart.close();
                byteChunkPart = null;
                filePart = null;
            }
            inputStream.close();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }
}

但我认为有一个大问题:part\u size的值不能大于127,否则 error: possible loss of precision 将会发生。
如何在不完全更改代码的情况下解决问题?

vmjh9lq9

vmjh9lq91#

List<PDDocument> Pages=new ArrayList<PDDocument>();
     Document.load(filePath);
    try {
        Splitter splitter = new Splitter();
        splitter.setSplitAtPage(NoOfPagesDocumentWillContain);
    Pages = splitter.split(document);

    }catch(Exception e)
    {
        l
        e.getCause().printStackTrace();
    }
zphenhs4

zphenhs42#

问题是 PART_SIZE 是一个 byte ; 因此,它的最大值确实是127。
你现在的代码充满了问题;例如,不正确的资源处理等。
以下是使用java.nio.file的版本:

private static final String FILENAME = "TextFile.txt";
private static final int PART_SIZE = xxx; // HERE

public static void main(final String... args)
    throws IOException
{
    final Path file = Paths.get(FILENAME).toRealPath();
    final String filenameBase = file.getFileName().toString();
    final byte[] buf = new byte[PART_SIZE];    

    int partNumber = 0;
    Path part;
    int bytesRead;
    byte[] toWrite;

    try (
        final InputStream in = Files.newInputStream(file);
    ) {
        while ((bytesRead = in.read(buf)) != -1) {
            part = file.resolveSibling(filenameBase + ".part" + partNumber);
            toWrite = bytesRead == PART_SIZE ? buf : Arrays.copyOf(buf, bytesRead);
            Files.write(part, toWrite, StandardOpenOption.CREATE_NEW);
            partNumber++;
        }
    }
}

相关问题