6 9

java中读取properties配置文件,并完成简单的加密解密

本文概要:读取properties文件,并对其中是值进行简单的加密,写入另一个文件.然后读取加密后的文件,进行解密操作.

这是今天去公司笔试的题目.

原题大概如下:

有一个properties文件,里面有若干个配置属性,现有一个接口, 

public interface Encode { 
    public void encode(String fileInputName,String fileOutputName) throws Exception; 
    public void decode(String fileInputName,String fileOutputName) throws Exception; 
}

encode方法功能:读取配置文件,把其中每项的值的每个字母ASCII码值+10,然后把密码后的配置文件写入fileOutputName文件中. 
decode方法功能与encode相反. 
写一个类实现这个接口,并写出main方法进行测试.

哎...悲剧的我压根儿就没用过properties文件,自然是不做啊,我直接用最原始的方法去读熟稔串进行解析的,感觉应该也没有错的.回来后在网上查了下. 
其实很简单.

本文概要:读取properties文件,并对其中是值进行简单的加密,写入另一个文件.然后读取加密后的文件,进行解密操作.

这是今天去公司笔试的题目.

原题大概如下:

有一个properties文件,里面有若干个配置属性,现有一个接口,

public interface Encode {
    public void encode(String fileInputName,String fileOutputName) throws Exception;
    public void decode(String fileInputName,String fileOutputName) throws Exception;
}

encode方法功能:读取配置文件,把其中每项的值的每个字母ASCII码值+10,然后把密码后的配置文件写入fileOutputName文件中.
decode方法功能与encode相反.
写一个类实现这个接口,并写出main方法进行测试.

哎…悲剧的我压根儿就没用过properties文件,自然是不做啊,我直接用最原始的方法去读熟稔串进行解析的,感觉应该也没有错的.回来后在网上查了下.
其实很简单.

代码如下:

prop.properties文件

name=tonyzzp
age=21
sex=male
homepage=www.streamcave.com/

 

Encode.java文件

package org.zzp.test;
 
public interface Encode {
   public void encode(String fileInputName,String fileOutputName) throws Exception;
   public void decode(String fileInputName,String fileOutputName) throws Exception;
}

 

EncodeImpl.java

package org.zzp.test;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.Properties;
 
public class EncodeImpl implements Encode {
 
   @Override
   public void decode(String fileInputName, String fileOutputName)
         throws Exception {
      FileInputStream in=new FileInputStream(fileInputName);
      FileOutputStream out=new FileOutputStream(fileOutputName);
      Properties prop=new Properties();
      prop.load(in);
      Enumeration<?> names=prop.propertyNames();
      while(names.hasMoreElements()){
         String name=(String)names.nextElement();
         String value=prop.getProperty(name);
         String result="";
         for(char c:value.toCharArray()){
            c-=10;
            result+=c;
         }
         prop.setProperty(name, result);
      }
      prop.store(out, "");
      System.out.println("解密完成");
   }
 
   @Override
   public void encode(String fileInputName, String fileOutputName)
         throws Exception {
      FileInputStream in=new FileInputStream(fileInputName);
      FileOutputStream out=new FileOutputStream(fileOutputName);
      Properties prop=new Properties();
      prop.load(in);
      Enumeration<?> names=prop.propertyNames();
      while(names.hasMoreElements()){
         String name=(String)names.nextElement();
         String value=prop.getProperty(name);
         String result="";
         for(char c:value.toCharArray()){
            c+=10;
            result+=c;
         }
         prop.setProperty(name, result);
      }
      prop.store(out, "");
      System.out.println("加密完成");
   }
   
   public static void main(String[] args) {
      EncodeImpl e=new EncodeImpl();
      try {
         e.encode("prop.properties", "encode.txt");
         e.decode("encode.txt", "decode.txt");
      } catch (Exception e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
      }
   }
}