如何在允许超时时间内接受来自控制台输入的指定指令
来源:互联网 发布:adaboost人脸检测算法 编辑:程序博客网 时间:2024/05/22 00:11
如何接收控制台输入的指令?可以用System.in很容易的实现,但是如何实现在超时时间内接收来自控制台输入的指定指令.直接看代码和测试类。
<span style="font-size:12px;">package com.ilucky.console.detecotr;import java.io.InputStream;/** * @author IluckySi * @date 20140723 */public class ConsoleDetectorUtil {private int timeoutMillis;private String command;public void setTimeoutMillis(int timeoutMillis) {this.timeoutMillis = timeoutMillis;}public void setCommand(String command) {if(command.length() <= 0) {try {throw new Exception("指令长度不能为0");} catch (Exception e) {e.printStackTrace();}}this.command = command;}public boolean readInputStreamWithTimeout() {boolean result = true;System.out.println("等待控制台输入指令(" + command + "), 开始初始化数据库操作:");//定义指令长度.byte[] consoleCommandByte = new byte[command.length()];//获取从控制台输入的指令长度.int readLength = 0;try {//读取数据偏移量.int bufferOffset = 0;//计算过期时间. long maxTimeMillis = System.currentTimeMillis() + timeoutMillis; //获取控制台输入流. InputStream is = System.in; //轮询控制台,获取控制台在timeoutMillis时间内输入的指令. while (System.currentTimeMillis() < maxTimeMillis && bufferOffset < command.length()) { //重点: 将控制台输入的指令写进字节数组. readLength = Math.min(is.available(), command.length() - bufferOffset); int readResult = is.read(consoleCommandByte, bufferOffset, readLength); bufferOffset += readResult; }//判断在timeoutMillis时间内从控制台输入的指令是否为command.char[] commandByte = command.toCharArray();if(readLength > 0) {for(int i = 0; i < consoleCommandByte.length; i++) {byte b = consoleCommandByte[i];char c = (char)b;if(c != commandByte[i]) {result = false;break;}}} else {result = false;}} catch (Exception e) {System.out.println("ConsoleDetectorUtil出现未知异常" + e.toString());}return result;}}</span>
<span style="font-size:12px;">package com.ilucky.console.detecotr;/** * @author IluckySi * @date 20140723 */public class MainTest {public static void main(String[] args) {ConsoleDetectorUtil cdu = new ConsoleDetectorUtil();String command = "yes";cdu.setCommand(command);cdu.setTimeoutMillis(5000);boolean result = cdu.readInputStreamWithTimeout();if(result == true) {System.out.println("接受控制台指令(" + command + "), 开始初始化数据库操作!");//省略业务操作: 初始化数据库.} else {System.out.println("未接受控制台指令(" + command + "), 忽略初始化数据库操作!");}}}/** 输出结果: java.lang.Exception: 指令长度不能为0等待控制台输入指令(), 开始初始化数据库操作:未接受控制台指令(), 忽略初始化数据库操作!at com.ilucky.console.detecotr.ConsoleDetectorUtil.setCommand(ConsoleDetectorUtil.java:22)at com.ilucky.console.detecotr.MainTest.main(MainTest.java:12)*等待控制台输入指令(yes), 开始初始化数据库操作:test未接受控制台指令(yes), 忽略初始化数据库操作!&等待控制台输入指令(yes), 开始初始化数据库操作:yes接受控制台指令(yes), 开始初始化数据库操作!*/</span>应用场景:通过在控制台输入指令的方式控制一些操作,例如初始化数据库!!!
0 0
- 如何在允许超时时间内接受来自控制台输入的指定指令
- Qt在控制台接受用户输入
- 从控制台接受用户输入的信息
- C语言中接受控制台的输入
- 如何在指定的时间内控制一个程序能否正确登陆
- [ZT]如何在图形界面中实时捕获控制台程序的标准输出,让图形界面程序向控制台程序输入特定的命令行指令
- 使用Java如何让需求在指定时间内生效
- 如何在Outlook 2003允许接受EXE附件
- 在SAP 中如何允许输入负数
- 如何实现在规定的时间内关机
- silverlight DispatcherTimer 使按钮在指定的时间内不可编辑
- 允许超时的互斥量
- 如何把控制台输入的内容写入到指定的文件中
- 如何接受命令行输入
- javascript 控制input只允许输入的各种指定内容
- 如何在最快的时间内获得三年的工作经验
- 在指定时间内令按钮失效
- Linux上通过iptables允许来自指定IP地址的访问
- FBO
- 时间紧迫的任务对于珠宝照片集分配与Packshotcreator数码灯箱
- 数据集成需求为代价商业分析
- 144_leetcode_Substring with Concatenation of All Words
- java — 读取txt文件的方法
- 如何在允许超时时间内接受来自控制台输入的指定指令
- 经纬财富:保定白银即时分析建议
- 设置图片显示部分内容
- 数据分析在企业
- Spring4.0系列
- java判断字符串中中文个数
- 提取粗金属从矿石浓缩
- Lua语言学习之字符串库
- Android自动测试工具Monkey