java文件编码转换器_java读取文件并获得文件编码,转换为指定编码的工具类代码...

package com.zuidaima.util;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

public class Test {

public static int fileCount = 0;

public static String sourceFileRoot = "D:/workspace/test/src/com/zuidaima/"; // 将要转换文件所在的根目录

public static String sourceCharset = "gbk"; // 源文件编码

public static String targetCharset = "utf8"; // 目标文件编码

public static void main(String[] args) throws IOException {

File fileDir = new File(sourceFileRoot);

convert(fileDir);

System.out.println("Total Dealed : " + fileCount + "Files");

}

public static void convert(File file) throws IOException {

// 如果是文件则进行编码转换,写入覆盖原文件

if (file.isFile()) {

// 只处理.java结尾的代码文件

if (file.getPath().indexOf(".java") == -1) {

return;

}

InputStreamReader isr = new InputStreamReader(new FileInputStream(

file), sourceCharset);

BufferedReader br = new BufferedReader(isr);

StringBuffer sb = new StringBuffer();

String line = null;

while ((line = br.readLine()) != null) {

// 注意写入换行符

sb.append(line + "\n");

}

br.close();

isr.close();

File targetFile = new File(file.getPath() + "." + targetCharset);

OutputStreamWriter osw = new OutputStreamWriter(

new FileOutputStream(targetFile), targetCharset);

BufferedWriter bw = new BufferedWriter(osw);

// 以字符串的形式一次性写入

bw.write(sb.toString());

bw.close();

osw.close();

System.out.println("Deal:" + file.getPath());

fileCount++;

} else {

for (File subFile : file.listFiles()) {

convert(subFile);

}

}

}

}

5f44669d9023f819b9cfa4031178418d.png

该代码可以把某个工程的gbk编码java文件全部自动转换为utf-8编码,在工作中很实用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 RSAUtil.java 文件代码,你可以在自己的项目中使用该代码文件,实现 RSA 加解密功能。 ```java package com.github.yeecode.rsautil; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.security.InvalidKeyException; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.security.Security; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import org.bouncycastle.jce.provider.BouncyCastleProvider; /** * RSA 加解密工具类 */ public class RSAUtil { /** * 生成公钥和私钥文件 * * @param publicKeyFilePath 公钥文件路径 * @param privateKeyFilePath 私钥文件路径 */ public static void generateKeyPair(String publicKeyFilePath, String privateKeyFilePath) { try { Security.addProvider(new BouncyCastleProvider()); // 创建密钥对生成器 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC"); // 初始化密钥对生成器 keyPairGenerator.initialize(1024, new SecureRandom()); // 生成密钥对 KeyPair keyPair = keyPairGenerator.generateKeyPair(); // 获取公钥和私钥 PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); // 将公钥和私钥写入文件 FileOutputStream publicKeyOutputStream = new FileOutputStream(publicKeyFilePath); publicKeyOutputStream.write(publicKey.getEncoded()); publicKeyOutputStream.close(); FileOutputStream privateKeyOutputStream = new FileOutputStream(privateKeyFilePath); privateKeyOutputStream.write(privateKey.getEncoded()); privateKeyOutputStream.close(); } catch (NoSuchAlgorithmException | NoSuchProviderException | IOException e) { e.printStackTrace(); } } /** * 加密 * * @param plainText 明文 * @param publicKeyFilePath 公钥文件路径 * @return 密文 */ public static String encrypt(String plainText, String publicKeyFilePath) { try { Security.addProvider(new BouncyCastleProvider()); // 读取公钥文件 FileInputStream publicKeyInputStream = new FileInputStream(publicKeyFilePath); ByteArrayOutputStream publicKeyByteArrayOutputStream = new ByteArrayOutputStream(); byte[] publicKeyBytes = new byte[1024]; int publicKeyLength = 0; while ((publicKeyLength = publicKeyInputStream.read(publicKeyBytes)) != -1) { publicKeyByteArrayOutputStream.write(publicKeyBytes, 0, publicKeyLength); } publicKeyInputStream.close(); publicKeyByteArrayOutputStream.close(); byte[] publicKeyByteArray = publicKeyByteArrayOutputStream.toByteArray(); // 将公钥字节数组转换为 PublicKey 对象 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKeyByteArray); KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC"); PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); // 加密 Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] cipherTextBytes = cipher.doFinal(plainText.getBytes()); return Base64Util.encode(cipherTextBytes); } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException | IOException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) { e.printStackTrace(); return null; } } /** * 解密 * * @param cipherText 密文 * @param privateKeyFilePath 私钥文件路径 * @return 明文 */ public static String decrypt(String cipherText, String privateKeyFilePath) { try { Security.addProvider(new BouncyCastleProvider()); // 读取私钥文件 FileInputStream privateKeyInputStream = new FileInputStream(privateKeyFilePath); ByteArrayOutputStream privateKeyByteArrayOutputStream = new ByteArrayOutputStream(); byte[] privateKeyBytes = new byte[1024]; int privateKeyLength = 0; while ((privateKeyLength = privateKeyInputStream.read(privateKeyBytes)) != -1) { privateKeyByteArrayOutputStream.write(privateKeyBytes, 0, privateKeyLength); } privateKeyInputStream.close(); privateKeyByteArrayOutputStream.close(); byte[] privateKeyByteArray = privateKeyByteArrayOutputStream.toByteArray(); // 将私钥字节数组转换为 PrivateKey 对象 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyByteArray); KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC"); PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); // 解密 Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] plainTextBytes = cipher.doFinal(Base64Util.decode(cipherText)); return new String(plainTextBytes); } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException | IOException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) { e.printStackTrace(); return null; } } } ``` 该代码文件中的 `Base64Util` 类是一个 Base64 编码工具类,用于将二进制数据转换为可读的字符串。您需要将其导入到您的项目中,或者使用 Java 内置的 `Base64` 类代替。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值