⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tool.java

📁 图书管理系统 运行环境 windows 2003 windows xp
💻 JAVA
字号:
package com.book.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Tool {

	public static final String DB_CONFIG_FILE = "db.config";

	public static final String DB_CONFIG_FILE_DRIVER = "driverClass";

	public static final String DB_CONFIG_FILE_URL = "url";

	public static final String DB_CONFIG_FILE_USERNAME = "username";

	public static final String DB_CONFIG_FILE_PWD = "password";

	public static final String EQUAL_SIGN_DELIMITERS = "=";

	public static final String COMMA_SIGN_DELIMITERS = ",";

	public static final String NEWLINE_CHAR_SIGN = "\n";

	public static Map readFile2Memory(String dbFile) throws FileReadException,
			SplitCharException {

		Map configMap = new HashMap();
		FileInputStream readfile = null;
		InputStreamReader ir = null;
		BufferedReader in = null;

		try {
			readfile = new FileInputStream(dbFile);
			ir = new InputStreamReader(readfile);
			in = new BufferedReader(ir);
			String str = in.readLine();
			while (str != null) {
				// System.out.println(str);
				str = str.trim();
				String[] splitString = null;
				int hasSplitChar = str.indexOf(COMMA_SIGN_DELIMITERS);
				if (hasSplitChar != -1) {
					splitString = str.split(COMMA_SIGN_DELIMITERS);
					configMap.put(splitString[0], str);
				} else {
					throw new SplitCharException("splite char error.");
				}

				str = in.readLine();
			}
		} catch (IOException e) {
			throw new FileReadException("read file error.");
		} finally {
			try {
				if (readfile != null) {
					readfile.close();
				}
				if (ir != null) {
					ir.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return configMap;
	}

	public static void writeMemory2File(String dbFile, Map table)
			throws FileReadException, SplitCharException {

		FileOutputStream writefile = null;
		OutputStreamWriter or = null;
		BufferedWriter out = null;

		try {
			writefile = new FileOutputStream(dbFile);

			or = new OutputStreamWriter(writefile);
			out = new BufferedWriter(or);
			Collection  rows = table.values();
			Iterator it = rows.iterator();
			while (it.hasNext()) {
				out.write(it.next().toString() + NEWLINE_CHAR_SIGN);
			}
			out.flush();
		} catch (IOException e) {
			throw new FileReadException("read file error.");
		} finally {
			try {
				if (writefile != null) {
					writefile.close();
				}
				if (or != null) {
					or.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

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