Java - 在文件内容周围打印框

Java - print box around contents of a file

我正在尝试使用接受两个参数的方法打印被框包围的文件的内容:一个表示输入文件的扫描仪,一个表示该文件中最长输入行的长度的整数。我已经准备好大部分盒子,但不确定如何放置盒子右侧的封闭管。这是我不知道如何解决的问题:

int i = length - line.length();        
for (int j = 0; j <= i; j++) {
   System.out.print(" ");
}         
System.out.print("|"); */ 

这是我正在使用的方法:

public static void printBox(Scanner input, int length) {
    
    int top = 0;
    int bottom = 0;
    System.out.print("+");
    while (top < length + 2) {
        System.out.print("-");
        top++;
    }
    System.out.print("+");
    System.out.println();
    
    
    while (input.hasNextLine()) {
        String line = input.nextLine();
        System.out.println("| " + line);
        
        /* int i = length - line.length();        
        for (int j = 0; j <= i; j++) {
           System.out.print(" ");
        }         
        System.out.print("|"); */ 
    }
    
    System.out.print("+");
    while (bottom < length + 2) {
        System.out.print("-");
        bottom++;
    }
    System.out.print("+");
}

这是一个测试用例 printBox(new Scanner("This is some\ntext here."), 12);

没有 for 循环,我得到以下结果。

+--------------+
| This is some 
| text here.   
+--------------+

这与 for 循环

+--------------+
| This is some
 || text here.
   |+--------------+

这里的问题是您使用 System.out.printSystem.out.println 的区别。 println 在输出末尾添加一个 \n 新行。我切换了你的两个打印功能,它可以正常工作。

public static void printBox(Scanner input, int length) {
    int top = 0;
    System.out.print("+");
    while (top < length + 2) {
      System.out.print("-");
      top++;
    }
    System.out.print("+");
    System.out.println();

    while (input.hasNextLine()) {
      String line = input.nextLine();
      System.out.print("| " + line);

      int i = length - line.length();
      for (int j = 0; j <= i; j++) {
        System.out.print(" ");
      }
      System.out.println("|");
    }

    System.out.print("+");
    int bottom = 0;
    while (bottom < length + 2) {
      System.out.print("-");
      bottom++;
    }
    System.out.print("+");
  }

输出:

+--------------+
| This is some |
|  text here.  |
+--------------+

也是一个很好的提示,我将 int bottom 的 declaration/initialization 移到了它第一次使用之前。这是基于 Checkstyle VariableDeclarationUsageDistance.

的良好形式
  1. 您评论的 for 循环代码是完美的,因为它在正确的垂直线之前将额外的白色 space 添加到所需的长度。 除了您在 for 循环开始之前使用 println 这导致 spaces 被添加到下一行而不是继续。

  2. 也不是使用顶部和底部两次来打印相同的水平线。您可以重复使用上面创建的行来关闭输入。 此外,您可以使用 StringBuilder 而不是使用过多的 Sysout 和字符串连接,因为那样 memory/space 效率不高。

下面是包含上述所有建议的优化代码。

public static void printBox(Scanner input, int length) {
    int top = 0;
    StringBuilder line = new StringBuilder();
    line.append('+');
    while (top < length + 2) {
        line.append("-");
        top++;
    }
    line.append("+");
    System.out.println(line);

    while (input.hasNextLine()) {
        String innerLineStr = input.nextLine();
        StringBuilder innerline = new StringBuilder(innerLineStr);
        innerline.insert(0,"| ");

        int i = length - innerLineStr.length();
        for (int j = 0; j <= i; j++) {
            innerline.append(" ");
        }
        innerline.append("|");
        System.out.println(innerline);
    }

    System.out.println(line);
}