阻塞读取文本数据

99 阅读1分钟
    public static void main(String[] args) throws IOException, InterruptedException {
        File file=new File("D:\\log\\ceshi.txt");
        Long len=file.length();
        System.out.println(len);
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
        BufferedReader bd=new BufferedReader(isr);
        String str;
        Integer count=1;
        String url=null ;
        String responseBody=null;
        List<StringPojo> list=new ArrayList<>();
   go: while(true){
        str=bd.readLine();
        int newcount=count;
       if(str==null){
           len=file.length();
           while(len==file.length()){
              Thread.sleep(1000);
              String state = readString("D:\\log\\state.txt");
              System.out.println(state);
              if("2".equals(state)){
                  break go;
              }
           }
           Thread.sleep(2000);
       }else {
           if (str.contains("Request url:")) {
               String str2 =getUtf8(str);
               url = str2.substring(12, str2.length());
           }
           if (str.contains("response body:")) {
               String str1 = getUtf8(str);
               responseBody = str1.substring(14, str1.length());
               newcount++;
           }
           if (newcount > count) {
               System.out.println(new StringPojo(url, responseBody));
               //list.add(new StringPojo(url,responseBody));
               url = null;
               responseBody = null;
               count++;
           }
       }
    }
//        for (StringPojo stringPojo : list) {
//            System.out.println(stringPojo);
//        }

}
public static String readString(String file){
    String str = null;
    byte[] bytes = null;
    FileInputStream in = null;
    try {
        in = new FileInputStream(file);
        int size = in.available();
        bytes = new byte[size];
        in.read(bytes);
        if(bytes == null){
            return null;
        }
        str= new String(bytes,"utf-8");
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        if(in!=null){
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return str;

}
private static String getUtf8(String str) throws UnsupportedEncodingException {
    return new String(str.getBytes("utf-8"), "utf-8");
}