常见字符编码以及利用IO实现文件分割与合并(终极IO应用)

常见字符编码
课程大纲
一、 常见字符编码
1、 在程序中如果没有处理好字符编码的问题就有可能出现乱码问题,在计算机世界里,任何文字都是以指定方式存在的。
2、 常见的编码有:ISO8859-1,GBK/GB2312,unicode,UTF,
3、 (1)、ISO8859-1:编码属于单字节编码,最多只能表示0—255的字符范围,主要在用英文上应用。
(2)、GBK/GB2312:中文的国际编码,专门用来表示汉字,是双字节编码。
(3)、unicode:Java中就是运用此编码,也是最标准的一种编码,是使用16进制表示的编码,但此编码不兼容ISO8859-1编码。
(4)、UTF-8:由于Unicode不兼容ISO8859-1编码,而且容易占用更多的空间,而且对于英文也需要使用两个字节编码,这样使用Unicode不便于传输和存储,因此产生了UTF-8编码。UTF-8兼容了ISO8859-1编码,也可以用来表示所有的语言字符,不过UTF-8是不定长编码,每个字符的编码从1-6个字节不等,一般在中文网页中使用此编码,因为这样可以节省空间。
4、造成乱码的根本原因:
(1)、程序使用的编码和本机使用的编码不统一。
(2)、在网络中,客户端与服务器端编码不统一。
一、实现文件的分割与合并

文件的分割

package us.google.www;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.SynchronousQueue;

/**
* 实现一个文件分割器,把一个大文件分割成一个小文件,(可根据情况自行设计)
* 分割后的文件扩展民为dat,文件名为:原文件名+原扩展名+编号+.dat;
* @author chongrubujing
*
*/
public class Homework1 {
/**
*
* @param src 源文件的路径
* @param mb 所分割的每个文件的大小
* @param dest 目标文件的路径
*/
public static void split(String src,int mb,String dest)
{
//根据源文件的 地址获取源文件,
File srcFile = new File(src);
if(!srcFile.exists())
{
return ;
}
//获取文件的总大小(单位是字节)
long countSize = srcFile.length();
//要分割的每个文件的大小(单位是字节)
long filesize = mb*1024*1024;
//要分割的份 数
int num = 0;
if(countSize%filesize==0)
{
num = (int)(countSize/filesize);

    }
    else {
        num = (int)(countSize/filesize)+1;

    }
    //构造文件输入流(程序读文件的)
    try {
        //根据源文件构造字节输入流对象
        InputStream is = new FileInputStream(srcFile);
        //根据输入流对象构造字节缓冲输入流
        BufferedInputStream bis = new BufferedInputStream(is);
        //定义字节数组,每次读取一MB的内容,
        byte[] bytes = new byte[1024*1024];
        //每次实际读的内容大小
        int len = -1;
        //循环把每个分割的小文件写入内容
        for (int i = 0; i < num; i++) {
            //每个文件的表示方式
            String newFile = dest+File.separator+srcFile.getName()+"-"+i+".dat";
            //根据分割后的每个小文件构造一个字节缓冲输入流
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
            //记录每个文件瞬时(当前)的大小
            int count = 0;
            //开始读取文件
            while ((len = bis.read(bytes))!=-1) {
                bos.write(bytes,0,len);
                bos.flush();
                count += len;
                if(count>=filesize)
                {
                    break;
                }
            }
            bos.close();
        }
        bis.close();
        is.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 
}
public static void main(String[] args) {
    //实际的源文件的路径
    String src = "D:\\JDK_API_1_6_zh_CN.CHM";
    //实际的目标文件的路径
    String dest = "D:\\";
    int mb = 10;
    System.out.println("文件分割开始。。。。。");
    split(src,mb,dest);
    System.out.println("文件分割结束!");
}

}

把分割的文件再次合并成原来的文件

package us.google.www;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* 把分割后的文件在合并成完整文件(文件还原),与原文件一致。
* @author chongrubujing
*
*/
public class Homework2 {
/**
* 文件合并
* @param desc 合并后文件所放的位置
* @param files 由于不确定文件的多少,所以采用动态数组,
*/
public static void merge(String desc,File… files)
{
//先获取小文件的文件名(小文件的文件名前面都一样)
String filename = files[0].getName();
filename = filename.substring(0, filename.lastIndexOf(“-“));

    try {
        //根据目标文件构造一个字节输出缓冲流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc+File.separator+filename));
        //定义每次读取文件的大小
        byte[] bytes = new byte[1024*1024];
        //定义每次实际读取数组的大小
        int len = -1;
        //循环读取每个文件
        for (int i = 0; i < files.length; i++) {
            //构造一个字节输入流
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(files[i]));
            while ((len = bis.read(bytes))!=-1) {
                bos.write(bytes,0,len);
                bos.flush();
            }
            bis.close();
        }
        bos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


}
public static void main(String[] args) {
    String desc = "D:\\";

    System.out.println("开始合并。。。。。。。");
    merge(desc,new File("D:\\JDK_API_1_6_zh_CN.CHM-0.dat"),
            new File("D:\\JDK_API_1_6_zh_CN.CHM-1.dat"),
            new File("D:\\JDK_API_1_6_zh_CN.CHM-2.dat"),
            new File("D:\\JDK_API_1_6_zh_CN.CHM-3.dat"));
    System.out.println("合并结束!");
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值