java IO中的乱码问题
1 //文件拆分
2 public void SplitFileStream(String src){
3 //数据源
4 File file = new File(src);
5 //总长度
6 long len = file.length();
7 //块大小
8 int blockSize = 500;
9 //块个数
10 int size = (int)Math.ceil(len*1.0/blockSize);
11
12 //起始位置和实际大小
13 int beginPos = 0;
14 int actualSize = 0;
15 for(int i= 0;i < size;++i){
16 beginPos = i*blockSize;
17 if(i == size -1){//最后一块另外处理
18 actualSize = (int)len;
19 }else{//剩余块数
20 actualSize = blockSize;
21 len-=actualSize;
22 }
23 Split(file,i,beginPos, actualSize);
24 }
25 }
26 public void Split(File srcFile,int i,long beginPos,int actualSize){
27 RandomAccessFile in = null;
28 RandomAccessFile out = null;
29 try {
30 in = new RandomAccessFile((srcFile), "r");
31 out = new RandomAccessFile(new File("D:\\Test\\split\\"+i+"-"+srcFile.getName()), "rw");
32 in.seek(beginPos);
33 byte[] buffer = new byte[1024];
34 int length = -1;
35 while ((length = in.read(buffer, 0, buffer.length))!=-1){
36 if (actualSize>length){
37 out.write(buffer, 0, length);
38 actualSize-=length;
39 }else{
40 out.write(buffer, 0, actualSize);
41 break;
42 }
43 }
44 System.out.println(i+"-->"+"已完成!");
45 } catch (FileNotFoundException e) {
46 System.out.println(i+"-->"+"有误!");
47 } catch (IOException e) {
48 System.out.println(i+"-->"+"有误!");
49 }finally {
50 if(out!=null){
51 try {
52 out.close();
53 } catch (IOException e) {
54 e.printStackTrace();
55 }
56 }
57 if (in!=null){
58 try {
59 in.close();
60 } catch (IOException e) {
61 e.printStackTrace();
62 }
63 }
64 }
65 }
执行上面的代码可以将某文件拆分成多个文件存储,但会出现部分正常,部分乱码的问题???