关于文件拆分的问题。

m0_37645396 m0_37645396 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
今天写了一个关于文件拆分的测试。有个地方不知道错在哪,有大神能指点一下吗?

public class TestCut {

public void cut() {

String readPath = "d:/testcut/vmware.log";
File testFile = new File(readPath);

long num = testFile.length() / 10L;

try {

RandomAccessFile file1 = new RandomAccessFile(testFile, "r");

byte[] min = new byte[1024];
int len = -1;

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

String writePath = "d:/testcut/vmware" + i + ".log";
File testOut = new File(writePath);
RandomAccessFile file2 = new RandomAccessFile(testOut,"rw");

try {
while((len = file1.read(min)) != -1) {
System.out.println(len);//最后一次len值是278
file2.write(min,0,1024);
//file2.write(min,0,len);
if(file2.length() > num) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}


上面注释的程序就是像请教一下,为什么两者得出的最后一个文件的内容不一样。

public void write(byte[] b, int off, int len)
b - 数据。
off - 数据的初始偏移量。
len - 要写入的字节数。
为什么当len大于b的数据长度但小于b数组的长度的时候最后一个文件会有内容缺失?




这是填1024的情况截图



这是填len的正常情况截图






占用空间相同,大小不同,但是大小大的文件内容却是缺失的。

有大神能讲解一下吗
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
m0_37645396
Java EE 67,535 成员 225,843 内容