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

📄 nettime.java

📁 上网计时器 上网计时器 上网计时器
💻 JAVA
字号:
import java.io.*;
import java.net.InetAddress;
import java.text.DateFormat;
import java.util.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class NetTime {

	private TableItem tableItem;
	private TableColumn newColumnTableColumn_1;
	private TableColumn newColumnTableColumn_2;
	private TableColumn newColumnTableColumn_3;
	private TableColumn newColumnTableColumn_4;
	private Table table;
	protected Shell shell;
	private boolean is = false;
	private Date start;
	private Date stop;
	private String[] sss = new String[4];
	private StringBuilder sb = new StringBuilder();
	private final byte[] b = {127,0,0,1};
	private DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,Locale.CHINA);
	private RandomAccessFile raf;
	private File datafile = new File("./data.txt");
	private static NetTime instance;
	
	private NetTime(){};
	
	public static NetTime newinstance(){
		if (instance == null){
			instance = new NetTime();
		}			
		return instance;
	}

	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			NetTime window = newinstance();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window
	 * @throws IOException 
	 */
	public void open() throws IOException {
		final Display display = Display.getDefault();
		createContents();
		inittable();
		shell.open();
		shell.layout();
		
		while (!shell.isDisposed()) {
			
			if (!display.readAndDispatch()){
				display.sleep();
			}
			if(!Arrays.equals(InetAddress.getLocalHost().getAddress(),b)){
				if(is == false){
					is = true;
					sss[0] = iptoString(InetAddress.getLocalHost().getAddress());
					sb.setLength(0);
					start = new Date();
					sss[1] = df.format(start);
				}
				
			}else{
				if(is == true){
					is = false;
					stop = new Date();
					sss[2] = df.format(stop);
					sss[3] = toTime(stop.getTime()-start.getTime());
					sb.setLength(0);
					tableItem = new TableItem(table,SWT.NONE);
					tableItem.setText(sss);
					shell.layout();
					raf = new RandomAccessFile(datafile,"rws");
					//System.out.println("oooo");
					raf.seek(raf.length());
					raf.writeBytes(sss[0]);
					raf.writeBytes("|");
					raf.writeBytes(sss[1]);
					raf.writeBytes("|");
					raf.writeBytes(sss[2]);
					raf.writeBytes("|");
					raf.writeBytes(sss[3]);
					raf.writeBytes("\r\n");
					raf.close();
				}
				
			}
				
		}
		display.dispose();
		//System.out.println(df.format(new Date()));
	}

	/**
	 * Create contents of the window
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setLayout(new FillLayout());
		shell.setSize(500, 375);
		shell.setText("SWT Application");
		// 创建控件
		{
			table = new Table(shell, SWT.BORDER);
			table.setLinesVisible(true);
			table.setHeaderVisible(true);
			// 创建控件
			{
				newColumnTableColumn_1 = new TableColumn(table, SWT.NONE);
				newColumnTableColumn_1.setWidth(134);
				newColumnTableColumn_1.setText("IP 地址");
			}
			// 创建控件
			{
				newColumnTableColumn_2 = new TableColumn(table, SWT.NONE);
				newColumnTableColumn_2.setWidth(147);
				newColumnTableColumn_2.setText("开始时刻");
			}
			// 创建控件
			{
				newColumnTableColumn_3 = new TableColumn(table, SWT.NONE);
				newColumnTableColumn_3.setWidth(149);
				newColumnTableColumn_3.setText("挂断时刻");
			}
			// 创建控件
			{
				newColumnTableColumn_4 = new TableColumn(table, SWT.NONE);
				newColumnTableColumn_4.setWidth(143);
				newColumnTableColumn_4.setText("上网时间");
			}
		}
	}
	/**
	 * 初始化表格
	 * @throws IOException 
	 */
	private void inittable() throws IOException{
		try {
			raf = new RandomAccessFile(datafile,"rws");
			while(true){
				String s = raf.readLine();
				if(s != null){
					String[] str = s.split("\\|");
					tableItem = new TableItem(table,SWT.NONE);
					tableItem.setText(str);
				}else{
					break;
				}
			}
		} catch (FileNotFoundException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}finally{
			raf.close();
			//System.out.println(df.format(new Date()));
			System.out.println(iptoString(InetAddress.getLocalHost().getAddress()));
			//System.out.println();
		}
	}
	private String toTime(long time){
		long t = time/3600000;
		long m = (time%3600000)/60000;
		long s = ((time%3600000)%60000)/1000;
		sb.append(t);
		sb.append(':');
		sb.append(m);
		sb.append(':');
		sb.append(s);
		return sb.toString();
	}
	private String iptoString(byte[] ip){
		int[] t = new int[4];
		for(int i = 0; i < 4; ++i){
			if(ip[i] < 0){
				t[i] = 256 + ip[i];
			}else{
				t[i] = ip[i];
			}
		}
		sb.append(t[0]);
		sb.append('.');
		sb.append(t[1]);
		sb.append('.');
		sb.append(t[2]);
		sb.append('.');
		sb.append(t[3]);
		return sb.toString();
	}
}

⌨️ 快捷键说明

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