用java实现的文件分割.每一块是1k.
我的文件有7k.分成了7块.前5块没问题.后来2块的前面部分有乱码.不知道是什么原因,怎么解决.?在相应目录下建立7k的名为"思想汇报.txt"的文件即可运行以下程序.
就算文件不是7k,被分割的子文件往往后面几块会出现乱码.请高手协助解决.
 
import java.io.*;public class Fen{
 String fileName;
 int size; Fen(String fileName,String size){
  this.fileName = fileName;
  this.size = Integer.parseInt(size)*1024; 
 }
  
  public void cut()throws Exception{
   int maxx = 0;
   File inFile = new File(fileName);
   
   int fileLength = (int)inFile.length();  //取得文件的大小
   int value;             //取得要分割的个数
   
   RandomAccessFile inn = new RandomAccessFile(inFile,"r");//打开要分割的文件
   
   
   value = fileLength/size;
   
   int i=0;
   int j=0;    //根据要分割的数目输出文件
   for (;j<value;j++){


    File outFile = new File(inFile.getName()+j+"zzii");
    RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
    maxx+=size;
    for (;i<maxx;i++){   
     outt.write(inn.read());
    }
    outt.close();
   }
   File outFile = new File(inFile.getName()+j+"zzii");
   RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
   System.out.println(i);
   for(;i<fileLength;i++){
  
    outt.write(inn.read());
   }
   outt.close();
 
   inn.close();
 } public static  void main(String [] args)throws Exception{
Fen cutt = new Fen("思想汇报.txt","1");
cutt.cut();
 }
}

解决方案 »

  1.   

    我用你的程序测试了一下,没发现乱码。
    楼主可以多测试一下,比如换成同样大小的其他文件,或者换成大小不一样的文件,看看会不会出现类似的问题。
    BTW,由于你是严格按照字节来切文件的,所以比如遇到\r\n的时候,如果\r分在上一个文件,而\n分在下一个文件,可能会出现问题。
      

  2.   

    brooksychen(初晨之阳)  
    thank you 不过我还有些未明白的问题.梢后我会结帖给分.用格式整齐一些的文件做切割会好一些.不过还是会在最后一个文件那里有乱码.(这次出现的是两个中文字符变成一个字符的乱码).
    还有
    1比如遇到\r\n的时候,如果\r分在上一个文件,而\n分在下一个文件,可能会出现问题。
      为什么这样会出现问题?这样出现的问题有办法解决吗?
    2由于你是严格按照字节来切文件的
      还有别的方式吗?你指的的行吗?可以我主要是要将每个文件都分为相同大小(比如本例中的1K)
      

  3.   

    单线程比较好处理:

    for (;i<maxx;i++){   
    outt.write(inn.read());
    }
    之后再读一个字节int temp = inn.read();
    outt.write(temp);
    if (temp > 127) {
    outt.write(inn.read());
    }
      

  4.   

    当然相应的maxx要增加1或2。并且注意maxx是否已经大于等于fileLength
      

  5.   

    楼上的这种做法有点看不懂.能解释一下吗? 在每写完"1k"字节到块文件中后,为什么要再读一个字节,用意何在? 而且为什么这个字节大于127的话,还要再写一个字节? if (temp > 127) {
    outt.write(inn.read());}最后,这和单线程有啥关系?难道您的意思是控制几个块文件同时写?
      

  6.   

    我觉得CrazyGou的做法应该是有一定道理.可以我在分割一篇文章时(.txt文件,7k),还是在第3块的前面部分出现诸如"嫠窒硎芨呷艘坏鹊......."的乱码.
      

  7.   

    想到一个办法,楼主试试:import java.io.*;public class Fen{
    String fileName;
    int size;Fen(String fileName,String size){
    this.fileName = fileName;
    this.size = Integer.parseInt(size)*1024; 
    }public void cut()throws Exception{
    int maxx = 0;
    File inFile = new File(fileName);
    int count = 0;  //记录汉字数,一个汉字计数增加2
    int fileLength = (int)inFile.length();  //取得文件的大小
    System.out.println(fileLength);
    int value;             //取得要分割的个数RandomAccessFile inn = new RandomAccessFile(inFile,"r");//打开要分割的文件
    value = fileLength/size;int i=0;
    int j=0;//根据要分割的数目输出文件
    for (;j<value;j++){
    File outFile = new File(inFile.getName()+j+".txt");RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
    int temp = 0;
    maxx+=size;
    maxx = Math.min(fileLength, maxx);
    for (;i<maxx;i++){   
    temp = inn.read();
    if (temp > 127)  //大于127表示读了半个双字节的字符
    count++;
    outt.write(temp);
    }
    if (count % 2 != 0) {  //count为奇数表示最后读了半个汉字
    outt.write(inn.read());
    maxx++;
    }
    count = 0;
    outt.close();
    }
    File outFile = new File(inFile.getName()+j+".txt");
    RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
    System.out.println(i);
    for(;i<fileLength;i++){
    outt.write(inn.read());
    }
    outt.close();inn.close();
    System.out.println(i);
    }public static  void main(String [] args)throws Exception{
    Fen cutt = new Fen("d:/temp/test.txt","1");
    cutt.cut();
    }
    }
      

  8.   

    结尾有yyyyyyyyyy,y上有两点的那种乱码
      

  9.   

    那是因为inFile.length()得到的文件长度包括一些其他信息>=文件内容的长度,所以最后的inn.read()返回了-1。写了一些-1到文件结尾,导致乱码。修改如下:import java.io.*;public class Fen{
    String fileName;
    int size;Fen(String fileName,String size){
    this.fileName = fileName;
    this.size = Integer.parseInt(size)*1024; 
    }public void cut()throws Exception{
    int maxx = 0;
    File inFile = new File(fileName);
    int count = 0;  //记录汉字数,一个汉字计数增加2
    int fileLength = (int)inFile.length();  //取得文件的大小
    int value;             //取得要分割的个数RandomAccessFile inn = new RandomAccessFile(inFile,"r");//打开要分割的文件
    value = fileLength/size;int i=0;
    int j=0;//根据要分割的数目输出文件
    for (;j<value;j++){File outFile = new File(inFile.getName()+j+".txt");RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
    int temp = 0;
    maxx+=size;
    for (;i < maxx && (temp = inn.read()) != -1;i++){
    if (temp > 127)  //大于127表示读了半个汉字
    count = ++count % 2;  //模2防止count溢出
    outt.write(temp);
    }
    if (count == 1 && (temp = inn.read()) != -1) {  //count若为1表示最后读取了半个汉字
    outt.write(temp);
    maxx++;
    }
    count = 0;
    outt.close();
    }
    File outFile = new File(inFile.getName()+j+".txt");
    RandomAccessFile outt= new RandomAccessFile(outFile,"rw");
    int temp = 0;
    while ((temp = inn.read()) != -1) {
    outt.write(temp);
    }
    outt.close();inn.close();
    }public static  void main(String [] args)throws Exception{
    Fen cutt = new Fen("d:/temp/test.txt","1");
    cutt.cut();
    }
    }