1. 程式人生 > >Java:自定義模擬QQ登陸視窗初級版(1),並驗證登入名和密碼是否正確

Java:自定義模擬QQ登陸視窗初級版(1),並驗證登入名和密碼是否正確

介面完成顯示


1、檔案概要


2、ClientLogin.java檔案內容

Java:ClientLogin.java檔案內容 (1)

3、db.properties檔案內容


4、FileUtils.java檔案內容
package ui.tools;

//java程式呼叫db.properties檔案內的資料
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;



public class FileUtils {
	
	//實現功能,根據db檔案的屬性name得到後邊的values
	public static String getValue(String key){
		Properties p=new Properties();  //建立一個集合類,該集合物件裝key-value元素
		try {  //異常處理
			//讀取bin資料夾下的db.properties檔案,並將該檔案放入IO流中
			InputStream readDB =FileUtils.class.getClassLoader().getResourceAsStream("db.properties");
			p.load(readDB);  //把讀取的檔案流物件,給properties物件,該物件自動獲取key-value
			return p.getProperty(key,null);  //如果沒有值則返回null
		} catch (Exception e) {
			e.printStackTrace();
		}  // load載入的是I/O流
		
		return null;
		
	}
	public static boolean checkLogin(String uname,String pawd){  //根據使用者名稱、密碼檢查是否可以登陸
		String username = getValue("username");  //從自定義db檔案中,獲取使用者名稱
		String password = getValue("pwd");
		if(username.equals(uname) && password.equals(pawd)){  //判斷是使用者名稱、密碼是否同時相等
			return true;
		}else{
			return false;  //預設返回失敗
			
		}
				
	}

//	public static void main(String[] args){
//		//獲取java檔案的路徑
//		//String path=FileUtils.class.getClassLoader().getResource("").getPath();
//		//System.out.println(path);
//		String str=getValue("username");
//		System.out.println(str);
//	}		

}