operateproperties.java

来自「该多线程下载工具功能强大」· Java 代码 · 共 63 行

JAVA
63
字号
package coursedesign;

import java.io.*;
import java.util.*;

public class OperateProperties {

	private static String filePath = "E:\\";

	private FileOutputStream fos = null;

	// 读取文件里相应的值
	public String readValue(String fileName, String key) {
		Properties props = new Properties();
		try {
			File file = new File(filePath + fileName);
			FileInputStream fis = new FileInputStream(file);
			InputStream ips = new BufferedInputStream(fis);
			props.load(ips);
			String value = props.getProperty(key);

			fis.close();
			ips.close();
			return value;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	// 写入文件里相应的值
	public void writeProperties(String fileName, String paraKey,
			String paraValue) {

		File file = new File(filePath + fileName);
		try {
			if (!file.exists()) {
				file.createNewFile();

			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}

		Properties props = new Properties();
		try {
			FileInputStream fis = new FileInputStream(file);
			props.load(fis);
			props.setProperty(paraKey, paraValue);
			props.store(new FileOutputStream(file), "header");

			fis.close();

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

}

⌨️ 快捷键说明

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