使用BufferedInputStream来实现读取本地文件

/**
 * Created by @author xu on @date 2018年 1月 10日
 */
public class IOUtils {
	public static String ReadFile(String filepath) {
		String str = ""; // 保存读取的全部内容
		StringBuffer sb = new StringBuffer(str); // 使用StringBuffer来拼接字符串
		File file = new File(filepath);
		BufferedInputStream bis = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(file));
			int len = 0;
			byte[] temp = new byte[1024];
			while ((len = bis.read(temp)) != -1) {
				sb.append(new String(temp, 0, len));
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally { // 关闭流资源
			try {
				bis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString(); // 将读取的结果以字符串的形式返回
	}
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Java 代码示例,使用 RSA 算法实现本地文件的非对称加密: ```java import java.io.*; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; public class FileEncryption { public static void main(String[] args) { String inputFile = "input.txt"; // 待加密的文件名 String outputFile = "output.enc"; // 加密后的文件名 String publicKeyFile = "public.key"; // 公钥文件名 try { // 读取公钥 PublicKey publicKey = readPublicKey(publicKeyFile); // 加载待加密的文件 byte[] input = readInputFile(inputFile); // 创建Cipher对象,使用公钥进行加密 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); // 加密文件内容 byte[] encrypted = cipher.doFinal(input); // 将加密后的内容写入到输出文件中 writeOutputFile(outputFile, encrypted); System.out.println("Encryption completed successfully."); } catch (Exception e) { e.printStackTrace(); } } // 读取公钥 public static PublicKey readPublicKey(String fileName) throws Exception { InputStream in = new FileInputStream(fileName); ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in)); try { BigInteger m = (BigInteger) oin.readObject(); BigInteger e = (BigInteger) oin.readObject(); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e); KeyFactory factory = KeyFactory.getInstance("RSA"); PublicKey key = factory.generatePublic(keySpec); return key; } finally { oin.close(); } } // 读取待加密的文件 public static byte[] readInputFile(String fileName) throws IOException { File file = new File(fileName); FileInputStream fis = new FileInputStream(file); byte[] data = new byte[(int) file.length()]; fis.read(data); fis.close(); return data; } // 将加密后的内容写入输出文件 public static void writeOutputFile(String fileName, byte[] data) throws IOException { FileOutputStream fos = new FileOutputStream(fileName); fos.write(data); fos.close(); } } ``` 在上面的代码中,我们使用Java 内置的 RSA 算法进行加密。首先,我们需要读取公钥文件(public.key),然后使用公钥进行加密。加密后的内容将被写入到输出文件(output.enc)中。 当然,这只是一个简单的示例代码,实际应用中,我们还需要考虑很多细节,如密钥管理、加密算法的选择、加密模式、填充方式等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值