コード例 #1
0
  public static String getLatestLines(File file) throws IOException {
    long s = System.currentTimeMillis();
    BufferedReader br = null;
    List<String> list = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    try {
      br = new BufferedReader(new FileReader(file));
      String line;
      while ((line = br.readLine()) != null) {
        list.add(line + "\n");
      }
      int lastIndex = getLastIndex(list);
      for (int i = lastIndex; i < list.size(); i++) {
        sb.append(list.get(i));
      }

    } catch (FileNotFoundException ex) {
      Logger.getLogger(LogfileUtil.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      try {
        br.close();
      } catch (IOException ex) {
        Logger.getLogger(LogfileUtil.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    long e = System.currentTimeMillis();
    log.log(
        Level.OFF,
        "Deserialized log file, elapsed: {0} seconds",
        AdministratorServlet.getElapsed(s, e));

    return sb.toString();
  }
コード例 #2
0
  public static String lineByLine(File file) throws IOException {
    long s = System.currentTimeMillis();
    BufferedReader br = null;
    List<String> list = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    try {
      br = new BufferedReader(new FileReader(file));
      String line;
      while ((line = br.readLine()) != null) {
        if (list.isEmpty()) {
          String text = stripExtraInfo(line);
          if (text != null) {
            list.add(text);
          }
        } else {
          String text = stripExtraInfo(line);
          if (text != null) {
            list.add(list.size() - 1, text);
          }
        }
      }
      for (String string : list) {
        sb.append(string).append("\n");
      }
      return sb.toString();

    } catch (FileNotFoundException ex) {
      Logger.getLogger(LogfileUtil.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      try {
        br.close();
      } catch (IOException ex) {
        Logger.getLogger(LogfileUtil.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    long e = System.currentTimeMillis();
    log.log(
        Level.OFF,
        "Deserialized log file, elapsed: {0} seconds",
        AdministratorServlet.getElapsed(s, e));

    return sb.toString();
  }