开发者社区> 问答> 正文

利用您写的那段控制台输入选择超时处理方式的代码,怎么获得从控制台输入的代码呢?:报错

@肖国颖先生,您好,想跟你请教个问题:一个简单的java程序,定时扫描某个文件夹里面的文件,若是不输入则扫描默认路径,但是输入路径的话,则扫描该路径,利用您写的那段代码,怎么获得从控制台输入的路径呢?我试着在改了一下代码,可是还是不行,您能帮我看看么??

public class TestTimeout {
    public static void main(String[] args) {
         final TestTimeout testTimeout = new TestTimeout();
         boolean doClean = testTimeout.readInput();    
         if (doClean) { 
             System.out.println("The database was cleaned!");
         }else {
            System.out.println("The clean operation was ignored.");
         } 
    }


     public int readInputStreamWithTimeout(InputStream is, byte[] buf, int timeoutMillis)
            throws IOException {
            BufferedReader buffer1 = new BufferedReader(new InputStreamReader(is));
            input = buffer1.readLine(); //读取输入数据
            int bufferOffset = 0;    //读取数据buf偏移量 
            long maxTimeMillis = System.currentTimeMillis() + timeoutMillis;//计算过期时间 
            while (System.currentTimeMillis() < maxTimeMillis && bufferOffset < buf.length) { 
            int readLength = Math.min(is.available(), buf.length - bufferOffset); 
            int readResult = is.read(buf, bufferOffset, readLength); 
            if (readResult == -1) {//流结束直接结束
                break;
             }
             bufferOffset += readResult;
             if (readResult > 0) {   //读取到内容结束循环
                 break;
            }
            try {
                Thread.sleep(10);          //等待10ms读取,减小cpu占用
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return bufferOffset;
    }


     public boolean readInput() {
          System.out.println("Do you want to clean and initialize the database?(y/n)");
          boolean doClean = false;
          byte[] inputData = new byte[1];//设置为1,只读第一个字符
          int readLength = 0;
          try {
               InputStream input=System.in;
               readLength = readInputStreamWithTimeout(input, inputData, 6000
          } catch (IOException e) {
              e.printStackTrace();
         }
         if (readLength > 0) {
             switch ((char) inputData[0]) {  //读取按键 
                 case 'y':
                 case 'Y': {
                    doClean = true;  //设置返回为true
                    break;
                }
            }
        }
         return doClean;
    }
}

展开
收起
kun坤 2020-06-06 15:54:09 449 0
1 条回答
写回答
取消 提交回答
  • 这代码,能格式化下么,实在没有心情看下去。######代码见 http://my.oschina.net/noahxiao/blog/157090######其实我原来是只接收一个char,你可以修改为接收整行内容

    byte[] inputData = new byte[1];//设置为1,只读第一个字符 int readLength = 0; try { InputStream input=System.in; readLength = readInputStreamWithTimeout(input, inputData, 6000 } catch (IOException e) { e.printStackTrace(); }

    为>>>>>>>>>>>>>>>>>>>(这样inputString中保存的就是你输入的这一行内容)

    byte[] inputData = new byte[1024]; String inputString=null;

        int readLength = 0;
        try {
            readLength = readInputStreamWithTimeout(System.in, inputData, 3000);
            inputString=new String(Arrays.copyOf(inputData,readLength));//由于与控制台编码一致所以new String没有加编码
        } catch (IOException e) {
            e.printStackTrace();
        }</pre> 
    




    2020-06-06 15:54:14
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
DTS控制台一本通 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载