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

📄 textdatabase.java

📁 采用JAVA开发
💻 JAVA
字号:
/*
 * Created on 2004-12-7
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.gctech.sms.tj;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * <p>Title:文本数据库</p>
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) Gctech 2004-12-7</p>
 * <p>Company: 吉芙德资讯有限公司</p>
 *
 * @version 1.0
 * @author liyi
 *
 */
public class TextDatabase {
	File mainFile = null;
	File tempFile = null;
	public boolean update(String key, String value, char split, String newKey, String newValue) {
		boolean foundAnswer = false;
		try {
			BufferedReader in = new BufferedReader(new FileReader(mainFile));
			PrintWriter out = new PrintWriter(new FileWriter(tempFile));
			String line;

			while ((line = in.readLine()) != null) {
				int colonPos = line.indexOf(split);
				if (colonPos < 0)
					continue;

				String keyStr = line.substring(0, colonPos);
				String valueStr = line.substring(colonPos + 1);
				if (key.equals(keyStr) && value.equals(valueStr)) {
					foundAnswer = true;
					keyStr = newKey;
					valueStr = newValue;
				}
				out.println(keyStr + split + valueStr);
			}
			if (!foundAnswer)
				out.println(key + split + value);
			out.close();
			in.close();
			mainFile.delete();
			tempFile.renameTo(mainFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return foundAnswer;
	}
	public boolean creat(String key, String value, char split) {
		boolean foundAnswer = false;
		try {
			BufferedReader in = new BufferedReader(new FileReader(mainFile));
			PrintWriter out = new PrintWriter(new FileWriter(tempFile));
			String line;

			while ((line = in.readLine()) != null) {
				int colonPos = line.indexOf(split);
				if (colonPos < 0)
					continue;

				String keyStr = line.substring(0, colonPos);
				String valueStr = line.substring(colonPos + 1);
				if (key.equals(keyStr) && value.equals(valueStr)) {
					foundAnswer = true;
				}
				out.println(keyStr + split + valueStr);
			}
			if (!foundAnswer)
				out.println(key + split + value);
			out.close();
			in.close();
			mainFile.delete();
			tempFile.renameTo(mainFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return foundAnswer;
	}
	public boolean find(String key, String value, char split) {
		boolean foundAnswer = false;
		try {
			BufferedReader in = new BufferedReader(new FileReader(mainFile));
			String line;

			while ((line = in.readLine()) != null) {
				int colonPos = line.indexOf(split);
				if (colonPos < 0)
					continue;
				String keyStr = line.substring(0, colonPos);
				String valueStr = line.substring(colonPos + 1);
				if (key.equals(keyStr) && value.equals("*")) {
					foundAnswer = true;
					break;
				}
				if (key.equals(keyStr) && value.equals(valueStr)) {
					foundAnswer = true;
					break;
				}
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return foundAnswer;
	}
	public boolean delete(String key, String value, char split) {
		boolean foundAnswer = false;
		try {
			BufferedReader in = new BufferedReader(new FileReader(mainFile));
			PrintWriter out = new PrintWriter(new FileWriter(tempFile));
			String line;

			while ((line = in.readLine()) != null) {
				int colonPos = line.indexOf(split);
				if (colonPos < 0)
					continue;

				String keyStr = line.substring(0, colonPos);
				String valueStr = line.substring(colonPos + 1);
				if (key.equals(keyStr) && value.equals("*")) {
					foundAnswer = true;
					continue;
				}
				if (key.equals(keyStr) && value.equals(valueStr)) {
					foundAnswer = true;
					continue;
				}
				out.println(keyStr + split + valueStr);
			}
			out.close();
			in.close();
			mainFile.delete();
			tempFile.renameTo(mainFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return foundAnswer;
	}
	public TextDatabase(String path, String dbname) {
		try {
			if (!path.equals("")) {
				mainFile = new File(path, dbname + ".dat");
				if (!mainFile.exists())
					mainFile.createNewFile();
				tempFile = new File(path, dbname + ".tmp");
			} else {
				mainFile = new File(dbname + ".dat");
				tempFile = new File(dbname + ".tmp");
			}
			boolean createSucceeded = false;

			for (int i = 0; i < 30; i++) {
				if (tempFile.createNewFile()) {
					createSucceeded = true;
					break;
				}
				try {
					Thread.sleep(1000);
				} catch (Exception ignore) {
				}
			}

			if (!createSucceeded) {
				throw new IOException("Unable to lock file");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	public void close() {
		if (tempFile.exists())
			tempFile.delete();
	}
	public static void main(String[] args) {
		TextDatabase db = new TextDatabase("./conf/", "user");
		System.out.println("rr");
		db.creat("13683389820", "DBXH", '@');
		System.out.println("ee");
		if (db.find("13683389821", "DBXH", '@'))
			System.out.println("找到了");
		db.update("13683389820", "DBXH", '@', "1232323", "QX");
		db.delete("1232323", "QX", '@');
	}
}

⌨️ 快捷键说明

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