package java_helloword;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
/**
* 实现简单的ini 文件读写
*/
public class IniReader
{
String configpath = "f:/test.ini";//文件路径
private static Properties properties =new Properties();
FileInputStream fis = null; // 读
OutputStream fos ;
/**
*
*/
public IniReader()
{
try
{
fis = new FileInputStream(configpath);
properties.load(fis);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public String getProperty(String key)
{
Object object = properties.get(key);
return object.toString();
}
public void setProperty(String key, String value)
{
try
{
fos = new FileOutputStream(configpath);// 加载读取文件流
properties.setProperty(key, value);
properties.store(fos, null);
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
* @test
*/
public static void main(String[] args)
{
IniReader ini = new IniReader();
// System.out.println(ini.getProperty("a"));
// ini.setProperty("a", "zzzzzzzzzzzzz");
// System.out.println(ini.getProperty("sec2"));
// ini.setProperty("sec2", "ccccccccccccccccccC://ee");
System.out.println("before-sec1:"+ini.getProperty("Language"));
//ini.setProperty("sec1", "wangry");
System.out.println("after-sec1:"+ini.getProperty("BaudRate"));
//test
String str = "Hello";
byte[] srtbyte = str.getBytes();
for (int i = 0; i < srtbyte.length; i++) {
System.out.println(srtbyte[i]);
}
byte[] srtbyte2 = new byte[]{'A','B','C','D'};
String res = new String(srtbyte2);
System.out.println(res);
}
}
IniReader
最新推荐文章于 2021-02-24 02:12:53 发布