public static void main(String[] args) throws Exception {
   PrintStream out = System.out;
   out.println(test(10000));
 }
示例#2
0
  @Test
  public void canFollowLogfile() throws IOException {
    File tempFile =
        File.createTempFile("commons-io", "", new File(System.getProperty("java.io.tmpdir")));
    tempFile.deleteOnExit();
    System.out.println("Temp file = " + tempFile.getAbsolutePath());
    PrintStream log = new PrintStream(tempFile);
    LogfileFollower follower = new LogfileFollower(tempFile);
    List<String> lines;

    // Empty file:
    lines = follower.newLines();
    assertEquals(0, lines.size());

    // Write two lines:
    log.println("Line 1");
    log.println("Line 2");
    lines = follower.newLines();
    assertEquals(2, lines.size());
    assertEquals("Line 2", lines.get(1));

    // Write one more line:
    log.println("Line 3");
    lines = follower.newLines();
    assertEquals(1, lines.size());
    assertEquals("Line 3", lines.get(0));

    // Write one and a half line and finish later:
    log.println("Line 4");
    log.print("Line 5 begin");
    lines = follower.newLines();
    assertEquals(1, lines.size());

    // End last line and start a new one:
    log.println(" end");
    log.print("Line 6 begin");
    lines = follower.newLines();
    assertEquals(1, lines.size());
    assertEquals("Line 5 begin end", lines.get(0));

    // End last line:
    log.println(" end");
    lines = follower.newLines();
    assertEquals(1, lines.size());
    assertEquals("Line 6 begin end", lines.get(0));

    // A line only missing a newline:
    log.print("Line 7");
    lines = follower.newLines();
    assertEquals(0, lines.size());
    log.println();
    lines = follower.newLines();
    assertEquals(1, lines.size());
    assertEquals("Line 7", lines.get(0));

    // Delete:
    log.close();
    lines = follower.newLines();
    assertEquals(0, lines.size());
  }
 private static void doPrint(int n) {
   PrintStream out = System.out;
   for (int i = 0; i < n; i++) {
     out.print('x');
   }
 }