参考别人写的编码转码

-----下面是将工程里面为gbk编码的文件转为utf-8的方法,其中

-----      String content = convEncoding(value, "gbk", "utf-8");  是将gbk转为utf-8,      out.write(content.getBytes("UTF-8"));  最后输出utf-8

------同理若要从utf-8,z转成gbk.只需要更改为 String content = convEncoding(value, "utf-8, "gbk");  out.write(content.getBytes("UTF-8"));  最后输出gbk

------具体代码如下:


package myBean;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;

/**
 * 该类可以将工程里面gbk编码转为utf-8的编码
 * @author wb_zjttx
 *
 */
public class FileGBK2UTF8 {
    public static void main(String[] args) {  
        // 需要转换的文件目录  
        String fromPath = "D:\\fileTest\\input";  
        // 转换到指定的文件目录  
        String toPath = "D:\\fileTest\\output";  
        
        String [] array={fromPath,toPath};
        info("start transform [from path]={0} [to path]={1}", array);  
 
        // 递归取到所有的文件进行转换  
        transform(fromPath, toPath);  
    }  
 
    /**
     * 把一个目录中的文件转换到另一个目录中
     *  
     * @param fromPath
     *            -- 来源文件目录
     * @param toPath
     *            -- 目标文件目录
     * @return
     */  
    public static boolean transform(String fromPath, String toPath) {  
        File ftmp = new File(fromPath);  
        if (!ftmp.exists()) {  
            info("转换文件路径错误!",null);  
            return false;  
        }  
        String [] array={fromPath,toPath};
        info("frompath is [{0}], topath is [{1}]", array);  
 
        // 如果是文件,则转换,结束  
        if (ftmp.isFile()) {  
            byte[] value = fileToBytes(fromPath);  
             String content = convEncoding(value, "gbk", "utf-8");  
            return saveFileUtf8(toPath, content);  
        } else {  
            // 查找目录下面的所有文件与文件夹  
            File[] childFiles = ftmp.listFiles();  
            for (int i = 0, n = childFiles.length; i < n; i++) {  
                File child = childFiles[i];  
                String childFrom = fromPath + "/" + child.getName();  
                String childTo = toPath + "/" + child.getName();  
 
                transform(childFrom, childTo);  
            }  
        }  
 
        return true;  
    }  
 
    /**
     * 把文件内容保存到指定的文件中,如果指定的文件已存在,则先删除这个文件, 如果没有则创建一个新文件,文件内容采用UTF-8编码方式保存。
     * 如果指定的文件路径不存在,则先创建文件路径,文件路径从根目录开始创建。
     *  
     * @param fileName
     *            -- 文件路径
     * @param content
     *            -- 文件内容
     * @return
     */  
    public static boolean saveFileUtf8(String fileName, String content) {  
        if (fileName == null || fileName.length() == 0)  
            return false;  
        if (content == null)  
            return false;  
 
        // 路径中的\转换为/  
        fileName = fileName.replace('\\', '/');  
        // 处理文件路径  
        createPath(fileName.substring(0, fileName.lastIndexOf('/')));  
 
        File file = null;  
        FileOutputStream out = null;  
        try {  
            // 创建或修改文件  
            file = new File(fileName);  
 
            if (file.exists()) {  
                file.delete();  
            } else {  
                file.createNewFile();  
            }  
 
            out = new FileOutputStream(file);  
            // 添加三个字节标识为UTF-8格式,也是BOM码  
            // out.write(new byte[]{(byte)0xEF,(byte)0xBB,(byte)0xBF});  
            out.write(content.getBytes("UTF-8"));  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
            return false;  
        } catch (IOException e) {  
            e.printStackTrace();  
            return false;  
        } finally {  
            if (out != null) {  
                try {  
                    out.flush();  
                    out.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                    return false;  
                }  
            }  
        }  
 
        return true;  
    }  
 
    /**
     * 把文件内容转换为字节数组输出。
     *  
     * @param fileName
     *            -- 文件名
     * @return
     */  
    public static byte[] fileToBytes(String fileName) {  
        FileInputStream ins = null;  
        ByteArrayOutputStream bos = null;  
        try {  
            // 创建文件读入流  
            ins = new FileInputStream(new File(fileName));  
            // 创建目标输出流  
            bos = new ByteArrayOutputStream();  
 
            // 取流中的数据  
            int len = 0;  
            byte[] buf = new byte[256];  
            while ((len = ins.read(buf, 0, 256)) > -1) {  
                bos.write(buf, 0, len);  
            }  
 
            // 目标流转为字节数组返回到前台  
            return bos.toByteArray();  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (ins != null) {  
                    ins.close();  
                    ins = null;  
                }  
                if (bos != null) {  
                    bos.close();  
                    bos = null;  
                }  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
 
        return null;  
    }  
 
    /**
     * 检查指定的文件路径,如果文件路径不存在,则创建新的路径, 文件路径从根目录开始创建。
     *  
     * @param filePath
     * @return
     */  
    public static boolean createPath(String filePath) {  
        if (filePath == null || filePath.length() == 0)  
            return false;  
 
        // 路径中的\转换为/  
        filePath = filePath.replace('\\', '/');  
        // 处理文件路径  
        String[] paths = filePath.split("/");  
 
        // 处理文件名中没有的路径  
       /// StringBuilder sbpath = new StringBuilder();  
        StringBuffer sbpath = new StringBuffer();  
        for (int i = 0, n = paths.length; i < n; i++) {  
            sbpath.append(paths[i]);  
            // 检查文件路径如果没有则创建  
            File ftmp = new File(sbpath.toString());  
            if (!ftmp.exists()) {  
                ftmp.mkdir();  
            }  
 
            sbpath.append("/");  
        }  
 
        return true;  
    }  
 
    /**
     * 取路径中的文件名
     *  
     * @param path
     *            -- 文件路径,含文件名
     * @return
     */  
    public static String getFileName(String path) {  
        if (path == null || path.length() == 0)  
            return "";  
 
        path = path.replaceAll("\\\\", "/");  
        int last = path.lastIndexOf("/");  
 
        if (last >= 0) {  
            return path.substring(last + 1);  
        } else {  
            return path;  
        }  
    }  
 
    /**
     * 字符串的编码格式转换
     *  
     * @param value
     *            -- 要转换的字符串
     * @param oldCharset
     *            -- 原编码格式
     * @param newCharset
     *            -- 新编码格式
     * @return
     */  
    public static String convEncoding(byte[] value, String oldCharset,  
            String newCharset) {  
        OutputStreamWriter outWriter = null;  
        ByteArrayInputStream byteIns = null;  
        ByteArrayOutputStream byteOuts = new ByteArrayOutputStream();  
        InputStreamReader inReader = null;  
 
        char cbuf[] = new char[1024];  
        int retVal = 0;  
        try {  
            byteIns = new ByteArrayInputStream(value);  
            inReader = new InputStreamReader(byteIns, oldCharset);  
            outWriter = new OutputStreamWriter(byteOuts, newCharset);  
            while ((retVal = inReader.read(cbuf)) != -1) {  
                outWriter.write(cbuf, 0, retVal);  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (inReader != null)  
                    inReader.close();  
                if (outWriter != null)  
                    outWriter.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
 
        String temp = null;  
        try {  
            temp = new String(byteOuts.toByteArray(), newCharset);  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
        // System.out.println("temp" + temp);  
        return temp;  
    }  
 
    /**
     * 显示提示信息
     *  
     * @param message
     *            -- 信息内容
     * @param params
     *            -- 参数
     */  
    private static void info(String message, Object[]params) {  
        message = MessageFormat.format(message, params);  
 
        System.out.println(message);  
    }  
}

深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值