base64helper.java

来自「通过java方式实现的DESEDE加密算法,其中包括选区择不同的模式与填充方式进」· Java 代码 · 共 163 行

JAVA
163
字号
package com.des.test;

/*
 * @author CuCuChen
 * 
 * @version $Id$
 * 
 */ 

import java.io.*; 

class Base64Helper { 

// 从文本文件对象中读取内容并转换为字符数组

public static char[] readChars(File file) 

{ 

     CharArrayWriter caw = new CharArrayWriter(); 

     try 

     { 

         Reader fr = new FileReader(file); 

        Reader in = new BufferedReader(fr); 

         int count = 0; 

         char[] buf = new char[16384]; 

        while ((count=in.read(buf)) != -1) { 

             if (count > 0) caw.write(buf, 0, count); 

         } 

         in.close(); 

     } 

     catch (Exception e) { e.printStackTrace(); } 

     return caw.toCharArray(); 

} 

// 从字符串对象中读取内容并转换为字符数组

public static char[] readChars(String string) 

{ 

     CharArrayWriter caw = new CharArrayWriter(); 

     try 

     { 

         Reader sr = new StringReader(string.trim()); 

        Reader in = new BufferedReader(sr); 

         int count = 0; 

         char[] buf = new char[16384]; 

        while ((count=in.read(buf)) != -1) { 

             if (count > 0) caw.write(buf, 0, count); 

         } 

         in.close(); 

     } 

     catch (Exception e) { e.printStackTrace(); } 

     return caw.toCharArray(); 

} 

// 从二进制文件对象中读取内容并转换为字节数组

public static byte[] readBytes(File file) 

{ 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

     try 

     { 

         InputStream fis = new FileInputStream(file); 

         InputStream is = new BufferedInputStream(fis); 

        int count = 0; 

         byte[] buf = new byte[16384]; 

         while ((count=is.read(buf)) != -1) { 

             if (count > 0) baos.write(buf, 0, count); 

         } 

         is.close(); 

    } 

     catch (Exception e) { e.printStackTrace(); } 

     return baos.toByteArray(); 

} 

// 写字节数组内容到二进制文件

public static void writeBytes(File file, byte[] data) { 

     try { 

         OutputStream fos = new FileOutputStream(file); 

         OutputStream os = new BufferedOutputStream(fos); 

         os.write(data); 

         os.close(); 

     } 

     catch (Exception e) { e.printStackTrace(); } 

} 

// 写字符数组内容到文本文件

public static void writeChars(File file, char[] data) { 

     try { 

         Writer fos = new FileWriter(file); 

         Writer os = new BufferedWriter(fos); 

        os.write(data); 

         os.close(); 

     } 

     catch (Exception e) { e.printStackTrace(); } 

} 

} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?