我试图为分布式系统课程的最后一个项目制作一个简化的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
将会发生。
如何在不完全更改代码的情况下解决问题?
2条答案
按热度按时间vmjh9lq91#
zphenhs42#
问题是
PART_SIZE
是一个byte
; 因此,它的最大值确实是127。你现在的代码充满了问题;例如,不正确的资源处理等。
以下是使用java.nio.file的版本: