2017-02-21 78 views
0

我想将一个大文件拆分成基于大小的部分。我使用随机访问文件。我所做的工作是获取文件的长度,拆分文件并为每个文件设置文件大小文件使用随机文件访问不能正常工作

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(); 
      } 

     } 

它拆分,但没有得到适当的输出

INPUT

法国警方正在搜寻总部马里的NE乐Pe的

.....长文本

OUTPUT

RHL eaithqt MnensrgFttaeoa Manloo egontagmsfran doyrmaastaNfaoRe eraaatsih小时FERR

和相同的输出是写在五个文件中

+0

?这样做的风险会不会让文本变得乱七八糟? –

回答

1

你需要你为什么会与当它是你正在处理一个文本文件的随机存取分裂,你的循环改变

for(int j=0; j<perfilesize; j++){ 
+0

也将os.write(racces.readInt())更改为int read; \t \t \t \t \t \t \t \t \t \t对(INT J = 0;Ĵ

+0

是的,不知道你为什么要做'readInt' –