vdrapb
2023-01-06 11:06:01 +08:00
你如果是 java8 的,你把 passer9527 这位老哥的代码改造一下就可以了
// 题目要求
int singleMaxSize = 255;
// DIR 就是存放 p1, p2, p3 的目录
Path logPath = DIR.resolve("splitlog.txt");
Path jpgPath = DIR.resolve("photo.jpg");
Map<String, InputStream> inputStreamMap = new HashMap<String, InputStream>(){
private static final long serialVersionUID = 1L;
{
put("0", Files.newInputStream(DIR.resolve("p1")));
put("1", Files.newInputStream(DIR.resolve("p2")));
put("2", Files.newInputStream(DIR.resolve("p3")));
}};
List<String> list = Files.readAllLines(logPath);
try (OutputStream outputStream = Files.newOutputStream(jpgPath, StandardOpenOption.CREATE)){
list.forEach(line ->{
String[] split = line.split("\t");
String index = split[0];
int size = Integer.parseInt(split[1]);
byte[] bytes = new byte[Math.min(size, singleMaxSize)];
try {
if(inputStreamMap.get(index).read(bytes) > 0){
outputStream.write(bytes);
}
} catch (IOException e) {
e.printStackTrace();
}
});
}
inputStreamMap.values().forEach(input ->{
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
});