使用随机文件访问拆分文件不起作用
splitting file using random file access not working
我想根据大小将一个大文件拆分成多个部分。我使用了随机访问文件。我所做的是获取文件的长度拆分文件并为每个文件设置文件大小
File file = new File("/home/hello/test.txt");
if(file.exists()){
long filesize = file.length();
int noofparts = 5;
int perfilesize = (int) (filesize/5);
for(int i=0;i<noofparts;i++){
try {
OutputStream os = new FileOutputStream("/home/solomon/test"+i+".txt");
RandomAccessFile racces = new RandomAccessFile(file, "r");
racces.seek(seek);
int read;
for(int j=0;j<perfilesize;perfilesize++){
os.write(racces.readInt());
}
os.flush();
os.close();
seek = seek+perfilesize;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
它正在分裂但没有得到正确的输出
输入
French police were searching the headquarters of Marine Le Pe
.....长文本
输出
rhl eaithqt MnensrgFttaeoa Manloo egontagmsfran doyrmaastaNfaoRe
eraaatsih hr Ferr
并且在五个文件中写入相同的输出
您需要将循环更改为
for(int j=0; j<perfilesize; j++){
我想根据大小将一个大文件拆分成多个部分。我使用了随机访问文件。我所做的是获取文件的长度拆分文件并为每个文件设置文件大小
File file = new File("/home/hello/test.txt");
if(file.exists()){
long filesize = file.length();
int noofparts = 5;
int perfilesize = (int) (filesize/5);
for(int i=0;i<noofparts;i++){
try {
OutputStream os = new FileOutputStream("/home/solomon/test"+i+".txt");
RandomAccessFile racces = new RandomAccessFile(file, "r");
racces.seek(seek);
int read;
for(int j=0;j<perfilesize;perfilesize++){
os.write(racces.readInt());
}
os.flush();
os.close();
seek = seek+perfilesize;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
它正在分裂但没有得到正确的输出
输入
French police were searching the headquarters of Marine Le Pe
.....长文本
输出
rhl eaithqt MnensrgFttaeoa Manloo egontagmsfran doyrmaastaNfaoRe eraaatsih hr Ferr
并且在五个文件中写入相同的输出
您需要将循环更改为
for(int j=0; j<perfilesize; j++){