package com.chat.util;
import java.util.Calendar;
import javax.swing.JLabel;
/**
* 时间类 , 获取时间信息
* @author 刘优
*
*/
public class TimeBean {
private static byte [] date; // year,month,day 年月日
private static byte [] time; // hour,minute,second 时分秒
private static JLabel label;
/**
* 获取当前时间
*/
static {
if (time == null || date == null) {
date = new byte[4];
time = new byte[3];
}
Calendar calendar = Calendar.getInstance();
date[0] = (byte)(calendar.get(Calendar.YEAR) - 2000);
date[1] = (byte)(calendar.get(Calendar.MONTH) + 1);
date[2] = (byte)(calendar.get(Calendar.DAY_OF_MONTH));
date[3] = (byte)(calendar.get(Calendar.DAY_OF_WEEK));
time[0] = (byte)(calendar.get(Calendar.HOUR_OF_DAY));
time[1] = (byte)(calendar.get(Calendar.MINUTE));
time[2] = (byte)(calendar.get(Calendar.SECOND));
new Thread(new Runnable() {
/**
* 使label成为电子时钟
*/
public void run() {
while (true) {
if (label !=null) {
label.setText(getTime());
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
nextSecond(); //走起来 获取下一秒
}
}
}).start();
}
public static void setLabel(JLabel l) {
label = l;
}
private static boolean leapYear(int year) {
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
return true;
}
return false;
}
private static boolean overLastDay(int year,int month,int day) {
int [] days = {32,30,32,31,32,31,32,32,31,32,31,32};
if (month != 2 && day == days[month - 1]) {
return true;
}
else if (leapYear(year) && day == 30) {
return true;
}
else if (day == 29) {
return true;
}
return false;
}
/**
* 取得下一秒的时间
*
*/
public static void nextSecond() {
if (++time[2] == 60) { // 过了一分钟
time[2] = 0;
if (++time[1] == 60) { // 过了一小时
time[1] = 0;
if (++time[0] == 24) { // 过了一天
time[0] = 0;
if (overLastDay(date[0],date[1],++date[2])) { // 过了一个月
date[2] = 1;
if (++date[1] == 13) { // 过了一年
date[1] = 1;
date[0]++;
}
}
}
}
}
}
/**
* 格式化星期显示
* @return
*/
public static String getWeek() {
String week = "星期";
switch (date[3]) {
case Calendar.SUNDAY :
week += "日";
break;
case Calendar.MONDAY :
week += "一";
break;
case Calendar.TUESDAY :
week += "二";
break;
case Calendar.WEDNESDAY :
week += "三";
break;
case Calendar.THURSDAY:
week += "四";
break;
case Calendar.FRIDAY :
week += "五";
break;
case Calendar.SATURDAY :
week += "六";
break;
default:
break;
}
return week;
}
/**
* 取得格式化的日期
*/
public static String getDate() {
return (2000 + date[0]) + "-" + (date[1] < 10 ? "0" : "") + date[1] + "-" + (date[2] < 10 ? "0" : "") + date[2] + " " + getWeek();
}
/**
* 获取当前时间的字符串, yyyy-mm-dd hh24:mi:ss
* @return String
*/
public static String getTime() {
return (time[0] < 10 ? "0" : "") + time[0] + " : " + (time[1] < 10 ? "0" : "") + time[1] + " : " + (time[2] < 10 ? "0" : "") + time[2];
}
}
package com.chat.server;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashSet;
import java.util.Vector;
/**
* 处理用户信息的类 包括验证,写文件等操作
*
* @author 刘优
*
*/
public class UserBean {
private static String userfile = "user/user"; // 用户文件,保存用户信息
//private static String fileEncode = "UTF-8";
static {
File folder = new File("user");
if (!folder.exists()) {
boolean b = folder.mkdir();
}
File f = new File(userfile);
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 获取所有用户名 在usr目录下的urs文件中
* @return vector
*/
public static Vector<String> getUserNames() {
File f = new File(userfile);
Vector<String> v = new Vector<String>();
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return v;
}
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String one = fileReader.readLine();
while (one != null) {
v.add(one.split(":")[0]);
one = fileReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null)
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return v;
}
/**
* 注册一个新用户
* @param name
* @param passwd
*/
public static void register(String name,String passwd) {
File f = new File(userfile);
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(userfile,true);
MessageDigest key = MessageDigest.getInstance("MD5"); // 把用户的密码保存为MD5密文
byte[] bytes = key.digest(passwd.getBytes());
String s = "";
for (byte b : bytes) {
s += b;
}
fileWriter.write(name + ":" + s + ":5\n");
fileWriter.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} finally {
if (fileWriter != null)
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 验证登录用户
* @param name
* @param passwd
* @return boolean
*/
public static int[] login(String name,String passwd) {
//System.out.println("user passwd -- " + name + "," + passwd);
name = name.trim();
passwd = passwd.trim();
int[] rs = {1,1};
File f = new File(userfile);
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return rs;
}
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
MessageDigest key = MessageDigest.getInstance("MD5"); // 验证用户的密码
byte[] bytes = key.digest(passwd.getBytes());
String s = "";
for (byte b : bytes) {
s += b;
}
String one = fileReader.readLine();
while (one != null) {
//System.out.println("file : " + one);
String [] infors = one.split(":");
if (infors[0].equals(name)) {
rs[0] = 0;
rs[1] = 1;
if (infors[1].trim().equals(s.trim())) {
rs[1] = 0;
}
break;
}
one = fileReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
finally {
if (fileReader != null)
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return rs;
}
/**
* 获取客户的黑名单
* @param username
* @return vector
*/
public static Vector<String []> getBlackUsers(String username) {
Vector<String []> v = new Vector<String []>();
File f = new File("user/" + username + ".black");
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String one = fileReader.readLine();
while (one != null) {
String [] line = new String[] {username,one};
v.add(line);
one = fileReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null)
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return v;
}
/**
* 把其它用户放入黑名单
* @param username
* @param blackUser
*/
public static void addBlack(String username,String blackUser) {
File f = new File("user/" + username + ".black"); //黑名单文件
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter writer = null;
try {
writer = new FileWriter(f,true); // 采用追加的方式写文件
writer.write(blackUser + "\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 写用户黑名单文件
* @param username
* @param blackUsers
*/
public static void writeBlackFile(String username,Vector<String []> blackUsers) {
PrintWriter theWriter = null;
try {
theWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream("user/" + username + ".black")));
for (String[] pair : blackUsers) {
if (pair[0].equals(username)) {
theWriter.println(pair[1]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (theWriter != null) {
theWriter.close();
}
}
}
/**
* 获取用户的好友名单
* @param name
* @return hashset
*/
public static HashSet<String> getFriends(String name) {
HashSet<String> v = new HashSet<String>();
File f = new File("user/" + name + ".friend");
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String one = fileReader.readLine();
while (one != null) {
v.add(one);
one = fileReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null)
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return v;
}
/**
* 写用户好友文件 username.friend
* @param username
* @param friends
*/
public static void writeFriendFile(String username,HashSet<String> friends) {
PrintWriter theWriter = null;
try {
theWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream("user/" + username + ".friend")));
for (String one : friends) {
theWriter.println(one);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (theWriter != null) {
theWriter.close();
}
}
}
/**
* 获取帮助文件内容 用来给TERM端客户显示
* @return 文件内容
*/
public static String getHelpFileContent() {
String s = "";
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new InputStreamReader(new FileInputStream("user/help")));//获取帮助文件
String one = fileReader.readLine();
while (one != null) {
s += one + "\n\r";
one = fileReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null)
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return s;
}
/**
* 判断一个用户是否为已注册用户
* @param name
* @return
*/
public static boolean isUser(String name) {
BufferedReader fileReader = null;
boolean is = false;
try {
fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(userfile)));
String one = fileReader.readLine();
while (one != null) {
if (one.split(":")[0].equals(name)) {
is = true;
break;
}
one = fileReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null)
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return is;
}
}
package com.chat.util;
import java.awt.Window; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import javax.swing.JFrame;
/**
* 窗口显示设置类, 使窗口置中
* @author 刘优
*
*/
public class WindowTool {
/**
* 居中显示窗口
* @param w
*/
public static void doView(Window w) {
w.setSize(w.getToolkit().getScreenSize().width,w.getToolkit().getScreenSize().height - 40);
w.setLocation(0,0);
w.setVisible(true);
if (w.getClass() == JFrame.class) {
((JFrame)w).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
((JFrame)w).setResizable(false);
}
if (w.getClass() == JDialog.class) {
((JDialog)w).setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
((JDialog)w).setResizable(false);
}
}
/**
* 居中显示窗口
* @param w
* @param width
* @param height
*/
public static void doView(Window w,int width,int height) {
w.setSize(width,height);
int width2 = w.getToolkit().getScreenSize().width;
width2 = (width2 - w.getWidth()) / 2;
int height2 = w.getToolkit().getScreenSize().height;
height2 = (height2 - w.getHeight()) / 2;
w.setLocation(width2,height2);
w.setVisible(true);
if (w.getClass() == JFrame.class) {
((JFrame)w).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
((JFrame)w).setResizable(false);
}
/*
if (w.getClass() == JDialog.class) {
((JDialog)w).setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
((JDialog)w).setResizable(false);
}
*/
}
/**
* 设置窗口关闭
* @param w 窗口
*/
public static void doClose(Window w) {
final Window w2 = w;
w.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
w2.dispose();
}
});
}
}
import java.util.Calendar;
import javax.swing.JLabel;
/**
* 时间类 , 获取时间信息
* @author 刘优
*
*/
public class TimeBean {
private static byte [] date; // year,month,day 年月日
private static byte [] time; // hour,minute,second 时分秒
private static JLabel label;
/**
* 获取当前时间
*/
static {
if (time == null || date == null) {
date = new byte[4];
time = new byte[3];
}
Calendar calendar = Calendar.getInstance();
date[0] = (byte)(calendar.get(Calendar.YEAR) - 2000);
date[1] = (byte)(calendar.get(Calendar.MONTH) + 1);
date[2] = (byte)(calendar.get(Calendar.DAY_OF_MONTH));
date[3] = (byte)(calendar.get(Calendar.DAY_OF_WEEK));
time[0] = (byte)(calendar.get(Calendar.HOUR_OF_DAY));
time[1] = (byte)(calendar.get(Calendar.MINUTE));
time[2] = (byte)(calendar.get(Calendar.SECOND));
new Thread(new Runnable() {
/**
* 使label成为电子时钟
*/
public void run() {
while (true) {
if (label !=null) {
label.setText(getTime());
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
nextSecond(); //走起来 获取下一秒
}
}
}).start();
}
public static void setLabel(JLabel l) {
label = l;
}
private static boolean leapYear(int year) {
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
return true;
}
return false;
}
private static boolean overLastDay(int year,int month,int day) {
int [] days = {32,30,32,31,32,31,32,32,31,32,31,32};
if (month != 2 && day == days[month - 1]) {
return true;
}
else if (leapYear(year) && day == 30) {
return true;
}
else if (day == 29) {
return true;
}
return false;
}
/**
* 取得下一秒的时间
*
*/
public static void nextSecond() {
if (++time[2] == 60) { // 过了一分钟
time[2] = 0;
if (++time[1] == 60) { // 过了一小时
time[1] = 0;
if (++time[0] == 24) { // 过了一天
time[0] = 0;
if (overLastDay(date[0],date[1],++date[2])) { // 过了一个月
date[2] = 1;
if (++date[1] == 13) { // 过了一年
date[1] = 1;
date[0]++;
}
}
}
}
}
}
/**
* 格式化星期显示
* @return
*/
public static String getWeek() {
String week = "星期";
switch (date[3]) {
case Calendar.SUNDAY :
week += "日";
break;
case Calendar.MONDAY :
week += "一";
break;
case Calendar.TUESDAY :
week += "二";
break;
case Calendar.WEDNESDAY :
week += "三";
break;
case Calendar.THURSDAY:
week += "四";
break;
case Calendar.FRIDAY :
week += "五";
break;
case Calendar.SATURDAY :
week += "六";
break;
default:
break;
}
return week;
}
/**
* 取得格式化的日期
*/
public static String getDate() {
return (2000 + date[0]) + "-" + (date[1] < 10 ? "0" : "") + date[1] + "-" + (date[2] < 10 ? "0" : "") + date[2] + " " + getWeek();
}
/**
* 获取当前时间的字符串, yyyy-mm-dd hh24:mi:ss
* @return String
*/
public static String getTime() {
return (time[0] < 10 ? "0" : "") + time[0] + " : " + (time[1] < 10 ? "0" : "") + time[1] + " : " + (time[2] < 10 ? "0" : "") + time[2];
}
}
package com.chat.server;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashSet;
import java.util.Vector;
/**
* 处理用户信息的类 包括验证,写文件等操作
*
* @author 刘优
*
*/
public class UserBean {
private static String userfile = "user/user"; // 用户文件,保存用户信息
//private static String fileEncode = "UTF-8";
static {
File folder = new File("user");
if (!folder.exists()) {
boolean b = folder.mkdir();
}
File f = new File(userfile);
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 获取所有用户名 在usr目录下的urs文件中
* @return vector
*/
public static Vector<String> getUserNames() {
File f = new File(userfile);
Vector<String> v = new Vector<String>();
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return v;
}
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String one = fileReader.readLine();
while (one != null) {
v.add(one.split(":")[0]);
one = fileReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null)
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return v;
}
/**
* 注册一个新用户
* @param name
* @param passwd
*/
public static void register(String name,String passwd) {
File f = new File(userfile);
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(userfile,true);
MessageDigest key = MessageDigest.getInstance("MD5"); // 把用户的密码保存为MD5密文
byte[] bytes = key.digest(passwd.getBytes());
String s = "";
for (byte b : bytes) {
s += b;
}
fileWriter.write(name + ":" + s + ":5\n");
fileWriter.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} finally {
if (fileWriter != null)
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 验证登录用户
* @param name
* @param passwd
* @return boolean
*/
public static int[] login(String name,String passwd) {
//System.out.println("user passwd -- " + name + "," + passwd);
name = name.trim();
passwd = passwd.trim();
int[] rs = {1,1};
File f = new File(userfile);
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return rs;
}
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
MessageDigest key = MessageDigest.getInstance("MD5"); // 验证用户的密码
byte[] bytes = key.digest(passwd.getBytes());
String s = "";
for (byte b : bytes) {
s += b;
}
String one = fileReader.readLine();
while (one != null) {
//System.out.println("file : " + one);
String [] infors = one.split(":");
if (infors[0].equals(name)) {
rs[0] = 0;
rs[1] = 1;
if (infors[1].trim().equals(s.trim())) {
rs[1] = 0;
}
break;
}
one = fileReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
finally {
if (fileReader != null)
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return rs;
}
/**
* 获取客户的黑名单
* @param username
* @return vector
*/
public static Vector<String []> getBlackUsers(String username) {
Vector<String []> v = new Vector<String []>();
File f = new File("user/" + username + ".black");
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String one = fileReader.readLine();
while (one != null) {
String [] line = new String[] {username,one};
v.add(line);
one = fileReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null)
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return v;
}
/**
* 把其它用户放入黑名单
* @param username
* @param blackUser
*/
public static void addBlack(String username,String blackUser) {
File f = new File("user/" + username + ".black"); //黑名单文件
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter writer = null;
try {
writer = new FileWriter(f,true); // 采用追加的方式写文件
writer.write(blackUser + "\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 写用户黑名单文件
* @param username
* @param blackUsers
*/
public static void writeBlackFile(String username,Vector<String []> blackUsers) {
PrintWriter theWriter = null;
try {
theWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream("user/" + username + ".black")));
for (String[] pair : blackUsers) {
if (pair[0].equals(username)) {
theWriter.println(pair[1]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (theWriter != null) {
theWriter.close();
}
}
}
/**
* 获取用户的好友名单
* @param name
* @return hashset
*/
public static HashSet<String> getFriends(String name) {
HashSet<String> v = new HashSet<String>();
File f = new File("user/" + name + ".friend");
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String one = fileReader.readLine();
while (one != null) {
v.add(one);
one = fileReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null)
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return v;
}
/**
* 写用户好友文件 username.friend
* @param username
* @param friends
*/
public static void writeFriendFile(String username,HashSet<String> friends) {
PrintWriter theWriter = null;
try {
theWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream("user/" + username + ".friend")));
for (String one : friends) {
theWriter.println(one);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (theWriter != null) {
theWriter.close();
}
}
}
/**
* 获取帮助文件内容 用来给TERM端客户显示
* @return 文件内容
*/
public static String getHelpFileContent() {
String s = "";
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new InputStreamReader(new FileInputStream("user/help")));//获取帮助文件
String one = fileReader.readLine();
while (one != null) {
s += one + "\n\r";
one = fileReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null)
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return s;
}
/**
* 判断一个用户是否为已注册用户
* @param name
* @return
*/
public static boolean isUser(String name) {
BufferedReader fileReader = null;
boolean is = false;
try {
fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(userfile)));
String one = fileReader.readLine();
while (one != null) {
if (one.split(":")[0].equals(name)) {
is = true;
break;
}
one = fileReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null)
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return is;
}
}
|
package com.chat.util;
import java.awt.Window; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import javax.swing.JFrame;
/**
* 窗口显示设置类, 使窗口置中
* @author 刘优
*
*/
public class WindowTool {
/**
* 居中显示窗口
* @param w
*/
public static void doView(Window w) {
w.setSize(w.getToolkit().getScreenSize().width,w.getToolkit().getScreenSize().height - 40);
w.setLocation(0,0);
w.setVisible(true);
if (w.getClass() == JFrame.class) {
((JFrame)w).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
((JFrame)w).setResizable(false);
}
if (w.getClass() == JDialog.class) {
((JDialog)w).setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
((JDialog)w).setResizable(false);
}
}
/**
* 居中显示窗口
* @param w
* @param width
* @param height
*/
public static void doView(Window w,int width,int height) {
w.setSize(width,height);
int width2 = w.getToolkit().getScreenSize().width;
width2 = (width2 - w.getWidth()) / 2;
int height2 = w.getToolkit().getScreenSize().height;
height2 = (height2 - w.getHeight()) / 2;
w.setLocation(width2,height2);
w.setVisible(true);
if (w.getClass() == JFrame.class) {
((JFrame)w).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
((JFrame)w).setResizable(false);
}
/*
if (w.getClass() == JDialog.class) {
((JDialog)w).setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
((JDialog)w).setResizable(false);
}
*/
}
/**
* 设置窗口关闭
* @param w 窗口
*/
public static void doClose(Window w) {
final Window w2 = w;
w.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
w2.dispose();
}
});
}
}