import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class MainFrame extends JFrame
{
MainFrame()
{
JFrame frame = new JFrame("测试界面");
Container container = frame.getContentPane();
JLabel lable = new JLabel("IP地址");
lable.setHorizontalAlignment(SwingConstants.CENTER);
// final JComboBox comboxIPforWrite = new JComboBox();
// JComboBox comboxIPforWrite = new JComboBox();
final JComboBox comboxIPforShow= new JComboBox();
comboxIPforShow.setEditable(true);
JButton button = new JButton();
button.setText("确定");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
int numItem = comboxIPforShow.getItemCount();
ArrayList listIP = new ArrayList();
for (int i = 0; i < numItem; i++)
{
listIP.add((String) comboxIPforShow.getItemAt(i));
}
String ip = (String) comboxIPforShow.getSelectedItem();
int size = listIP.size();
if (size > 0)
{
for (int i = 0; i < size; i++)
{
//如果原来已经存储了,则删掉重复的,插入链表首,否则直接插入链表首
if (listIP.get(i).toString().equalsIgnoreCase(ip))
{
listIP.remove(i);//将以前的重复的去掉
break;
}
}
listIP.add(0, ip);//永远插在链表首
}
else
{
listIP.add(0, ip);//刚开始登陆的头一份,插在链表首
}
FileLax.insertIPtoLax(listIP, "ZXSS20.LAX");
System.out.println(ip);
}
});
//for()
/*
* 窗体放置的控件内容
*/
JPanel panelMain = new JPanel();
panelMain.setLayout(new BorderLayout());
JPanel panelIP = new JPanel();
panelIP.setLayout(new GridBagLayout());
/***
*
*/
//int numItem = comboxIPforShow.getItemCount();
ArrayList listIP = new ArrayList();
// XMLClass xmlClass = new XMLClass();
// listIP = xmlClass.getIPbyArrayList("user.xml");
//FileLax fileLax = new FileLax();
listIP = FileLax.getIPfromLax("ZXSS20.LAX");
int numItem = listIP.size();
for(int i =0 ;i<numItem; i++)
{
//listIP.add((String) comboxIPforWrite.getItemAt(i));
//listIP
comboxIPforShow.addItem(listIP.get(i));
}
/***
*
*/
panelIP.add(lable, new GridBagConstraints(0,0,1,1,100,100,GridBagConstraints.CENTER,GridBagConstraints.BOTH , new Insets(0, 0, 0 ,0),0, 0 ));
panelIP.add(comboxIPforShow, new GridBagConstraints(1,0,1,1,100,100,GridBagConstraints.CENTER,GridBagConstraints.BOTH , new Insets(0, 0, 0 ,0),0, 0 ));
panelMain.add(panelIP ,BorderLayout.NORTH);
panelMain.add(button ,BorderLayout.SOUTH);
//panelMain.setSize(new Dimension(61,39));
//comboxIPforShow = beforeShow(comboxIPforShow);
/*
* 窗体放置的控件内容
*/
container.add(panelMain);
frame.setVisible(true);
frame.setSize(618,382);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
// private JComboBox beforeShow(JComboBox comboxIPforShow)
// {
// int numItem = comboxIPforShow.getItemCount();
// ArrayList listIP = new ArrayList();
// XMLClass xmlClass = new XMLClass();
// listIP = xmlClass.getIPbyArrayList("user.xml");
// for(int i =0 ;i<numItem; i++)
// {
// //listIP.add((String) comboxIPforWrite.getItemAt(i));
// //listIP
// comboxIPforShow.addItem(listIP.get(i));
//
// }
// return comboxIPforShow;
//
// }
public static void main (String[] args)
{
MainFrame mainFrame = new MainFrame();
// try
// {
// FileWriter fw = new FileWriter("zxss20.lax", true);
// }
// catch (IOException e)
// {
// // TODO 自动生成 catch 块
// e.printStackTrace();
// }
}
}
------------------------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class FileLax
{
FileLax()
{
}
/**
* 从zxss20.lax中读取记录的历史IP
* @param strPath
* @return
*/
public static ArrayList getIPfromLax(String strPath)
{
ArrayList ipArrayList = new ArrayList();
FileReader fr = null;
try
{
fr = new FileReader(strPath);
}
catch(FileNotFoundException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
BufferedReader buff = new BufferedReader(fr);
boolean eof = false;
while(! eof)
{
String line = null;
try
{
line = buff.readLine();
}
catch(IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
if(line == null)
{
eof = true;
}
else
{
System.out.println(line);
if(line.indexOf("userIP")>=0 && line.indexOf("=")>=0)
{
String strIP = line.substring(line.indexOf("=")+1);
ipArrayList.add(strIP);
}
}
}
try
{
buff.close();
}
catch(IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
return ipArrayList ;
}
/**
* 从zxss20.lax中读取内容,将含类似userIP = 127.0.0.1的内容删除掉,为了多次写入zxss20.lax做准备
*
* @param strPath
* @return
*/
public static ArrayList getContNoIP(String strPath)
{
ArrayList listContNoIP = new ArrayList();
FileReader fr = null;
try
{
fr = new FileReader(strPath);
}
catch(FileNotFoundException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
BufferedReader buff = new BufferedReader(fr);
boolean eof = false;
while(!eof)
{
String line = null;
try
{
line = buff.readLine();
}
catch(IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
if(line == null)
{
eof = true;
}
else
{
System.out.println(line);
if(isIPLine(line))
{
// do nothing
}
else if(isContainsNotes(line))
{
//do nothing
}
else
{
listContNoIP.add(line);
}
}
}
try
{
buff.close();
}
catch(IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
return listContNoIP;
}
/**
* 判断该行是否包含特殊用于保存历史IP地址注释
*
* @param strLine
* @return
*/
private static boolean isContainsNotes(String strLine)
{
if(strLine.trim().equalsIgnoreCase("# History IP BEGIN")
|| strLine.trim().equalsIgnoreCase("# IP Order by Time: Newest--->Oldest corresponding Top--->Bottom")
|| strLine.trim().equalsIgnoreCase("# History IP END"))
{
return true;
}
else
{
return false;
}
}
/**
* 判断该行是否是保存的历史IP地址
*
* @param strLine
* @return
*/
private static boolean isIPLine(String strLine)
{
if(strLine.indexOf("userIP") >= 0 && strLine.indexOf("=") >= 0)
{
return true;
}
else
{
return false;
}
}
/**
* 向xss20.lax中底部插入历史IP 之所以要以链表的形式插入,不是一条一条的插入,是考虑到保存的历史IP不能有重复,且要有先后顺序: 最新---最老
* @param strPath
* @return
*/
public static void insertIPtoLax( ArrayList ipArrayList ,String strPath)
{
recoverLaxNoIP( strPath );
FileWriter fw = null;
try
{
fw = new FileWriter(strPath, true);
}
catch(IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
BufferedWriter buffWrite = new BufferedWriter(fw);
ArrayList listNotesHead = new ArrayList();
ArrayList listNotesEnd = new ArrayList();
listNotesHead.add("# History IP BEGIN");
listNotesHead.add("# IP Order by Time: Newest--->Oldest corresponding Top--->Bottom" );
listNotesEnd.add("# History IP END");
/**
* 写标题注释HEAD
*/
writeNote( buffWrite , listNotesHead);
/**
* 写历史IP内容
*/
writeIPBody(buffWrite ,ipArrayList);
/**
* 写标题注释END
*/
writeNote( buffWrite , listNotesEnd);
try
{
buffWrite.close();
}
catch(IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
/**
* 写注释
* @param buffWrite
* @param arrayListNotes
*/
private static void writeNote(BufferedWriter buffWrite ,ArrayList arrayListNotes)
{
int size = arrayListNotes.size();
try
{
for(int i = 0;i<size;i++)
{
buffWrite.write(arrayListNotes.get(i).toString());
buffWrite.newLine();
}
}
catch(IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
/**
* 写IP内容,写入的IP加上了前缀。 格式举例"userIP = 127.0.0.1"
* @param buffWrite
* @param ipArrayList
*/
private static void writeIPBody(BufferedWriter buffWrite ,ArrayList ipArrayList)
{
int size = ipArrayList.size();
for(int i = 0; i < size; i++)
{
String line ="userIP = "+ (String) ipArrayList.get(i);
System.out.println(line);
try
{
buffWrite.write(line);
buffWrite.newLine();
}
catch(IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
/**
* 向buffWrite中写内容,
* @param buffWrite
* @param ipArrayList
*/
private static void writeLax(BufferedWriter buffWrite ,ArrayList listContNoIP)
{
int size = listContNoIP.size();
for(int i = 0; i < size; i++)
{
String line = (String) listContNoIP.get(i);
System.out.println(line);
try
{
buffWrite.write(line);
buffWrite.newLine();
}
catch(IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
/**
* 恢复ZXSS20.LAX中内容,使之不包含IP地址的内容
* @param ipArrayList
* @param strPath
*/
public static void recoverLaxNoIP(String strPath )
{
ArrayList listContNoIP = null;
listContNoIP = getContNoIP( strPath);
FileWriter fw = null;
try
{
fw = new FileWriter(strPath, false);//非在文件后面插入形式的,重新写一个新文件的形式
}
catch(IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
BufferedWriter buffWrite = new BufferedWriter(fw);
writeLax(buffWrite ,listContNoIP);
try
{
buffWrite.close();
}
catch(IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args)
{
String[] arrayIP = {"10.40.109.6" ,"10.40.109.7","10.40.109.8","10.40.109.9","10.40.109.10"};
/**写入ZXSS20.LAX内容*/
ArrayList ipList = new ArrayList();
for(int i = 0; i < arrayIP.length; i++)
{
ipList.add(arrayIP[i]);
}
try
{
FileLax.insertIPtoLax(ipList, "ZXSS20.LAX");
}
catch(Exception e)
{
e.printStackTrace();
}
for(int i = 0; i < ipList.size(); i++)
{
System.out.println(ipList.get(i));
}
/**读取ZXSS20.LAX内容*/
ArrayList ipListShow = new ArrayList();
try
{
ipListShow = FileLax.getIPfromLax("ZXSS20.LAX");
}
catch (Exception e)
{
e.printStackTrace();
}
for(int i=0 ;i< ipListShow.size(); i++)
{
System.out.println(ipListShow.get(i));
}
}
}