用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();
}
}
我的文件有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();
}
}
楼主可以多测试一下,比如换成同样大小的其他文件,或者换成大小不一样的文件,看看会不会出现类似的问题。
BTW,由于你是严格按照字节来切文件的,所以比如遇到\r\n的时候,如果\r分在上一个文件,而\n分在下一个文件,可能会出现问题。
thank you 不过我还有些未明白的问题.梢后我会结帖给分.用格式整齐一些的文件做切割会好一些.不过还是会在最后一个文件那里有乱码.(这次出现的是两个中文字符变成一个字符的乱码).
还有
1比如遇到\r\n的时候,如果\r分在上一个文件,而\n分在下一个文件,可能会出现问题。
为什么这样会出现问题?这样出现的问题有办法解决吗?
2由于你是严格按照字节来切文件的
还有别的方式吗?你指的的行吗?可以我主要是要将每个文件都分为相同大小(比如本例中的1K)
在
for (;i<maxx;i++){
outt.write(inn.read());
}
之后再读一个字节int temp = inn.read();
outt.write(temp);
if (temp > 127) {
outt.write(inn.read());
}
outt.write(inn.read());}最后,这和单线程有啥关系?难道您的意思是控制几个块文件同时写?
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();
}
}
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();
}
}