2015-05-29 65 views
-2

由于我的上一个文本和问题非常模糊,因此我现在的源代码和一个更清晰的问题。现在是关于填充。用java填充文本文件表

这里是我的代码到现在:

import java.io.BufferedWriter; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Arrays; 

public class makeTable { 


static ArrayList<String> val1 = new ArrayList<>(Arrays.asList("field1", "field1val2", "field1val3")); 
static ArrayList<String> val2 = new ArrayList<>(Arrays.asList("field2", "field2val2", "field2val3")); 
static int col1=15; 
static int col2=15; 

public static void main(String arg[]) { 

BufferedWriter writeTable = null; 
try { 
    writeTable = new BufferedWriter(new FileWriter("C:/testtable.txt")); 

    //Anfang erste Zeile 
    writeTable.write("+ "); 
    for (int i = 0; i < col1; i++){ 
     writeTable.write("-"); 
    } 
    writeTable.write(" + "); 
    for (int i = 0; i < col2; i++){ 
     writeTable.write("-"); 
    } 
    writeTable.write(" +"); 
    writeTable.newLine(); 
    //Ende erste Zeile 

    for (int i = 0; i < val1.size(); i++){ 
     writeTable.write("| " + val1.get(i) + " "+ " + " +" "+ val2.get(i) + " "+ " |"); 
     writeTable.newLine(); 

     writeTable.write("+ "); 
     for (int j = 0; j < col1; j++){ 
      writeTable.write("-"); 
     } 
     writeTable.write(" + "); 
     for (int m = 0; m < col2; m++){ 
      writeTable.write("-"); 
     } 
     writeTable.write(" +"); 
     writeTable.newLine(); 

    } 
} catch (IOException e) { 
    System.err.println(e); 
} finally { 
    if (writeTable != null) { 
     try { 
      writeTable.close(); 
     } catch (IOException e) { 
      System.err.println(e); 
     } 
    } 
} 
} 
} 

现在我需要让结果看上去像添加填充:

+ -------------- + -------------- + 
|  field1  |  filed2  | 
+ -------------- + -------------- + 
| field1val2 | field2val2 | 
+ -------------- + -------------- + 

等。它需要居中。我只能想到添加类似val1.get(i).length()/ 2的东西,那就是添加....“的数量,但我该怎么做?

我不能使用其他库(第三方库)。

+1

究竟是什么问题?如何写入文件?或者如何获得这种模式? – jbx

+0

主要是如何获取表格的值与模式。 – Thevagabond

+2

这个小图书馆可能会有所帮助:https://github.com/assylias/Java-Text-Table-Formatter – assylias

回答

1

在下面的代码中更改appendStringBuilderBufferedWriter

public void appendCentered(StringBuilder sb, String s, int width) { 
    if (s.length() > width) { 
     s = s.substring(0, width); 
    } 
    int spaces = width - s.length(); 
    int before = spaces/2; 
    int after = spaces - before; // Could be 1 more than 'before'. 
    appendSpaces(sb, before); 
    sb.append(s); 
    appendSpaces(sb, after); 
} 

public void appendSpaces(StringBuilder sb, int width) { 
    while (width-- > 0) { 
     sb.append(' '); 
    } 
} 
2

下面是我的文字居中后的程序结果。

+ -------------- + -------------- + 
|  field1  +  field2  | 
+ -------------- + -------------- + 
| field1val2 + field2val2 | 
+ -------------- + -------------- + 
| field1val3 + field2val3 | 
+ -------------- + -------------- + 

以下是我所做的更改。

  1. 我做的最大的事情是把你的单片代码分解成方法。通过编写小方法,您可以将较大的任务分解为较小的任务。较小的任务更容易编码。

  2. 类名以大写字母开头。我将makeTable改名为MakeTable。

  3. 我改变了你的代码来使用List接口,而不是ArrayList类。通过使用该接口,将来可以更轻松地更改List的类型。我可以通过更改2行代码来使用LinkedList。

  4. 现在的主要方法只是创建输出。我无法直接写入Windows 8.1笔记本电脑上的C驱动器。我不得不在与代码相同的目录中创建文件。

  5. writeDashedLine方法写入一条虚线。我在2个地方使用代码,但通过将代码放入方法中,我只需编写一次代码。

  6. writeDashes方法写入虚线的虚线部分。再次,我使用2个地方的代码,但通过将代码放入方法中,我只需编写一次代码。

  7. writeValueLine方法将值写入一行。我在一个地方使用代码,但为了与writeDashedLine方法保持一致,我为代码编写了一个方法。

  8. 其余的方法是我写给中心文本。首先,我在两个值列表中找到了最长的值。接下来,我添加了4个填充字符。接下来,我通过向字符串的前面和后面添加填充来使文本居中。

研究这些方法,以便将来可以完成这种类型的任务。

这是格式化的代码。

package com.ggl.testing; 

import java.io.BufferedWriter; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

public class MakeTable { 

    static List<String> val1 = new ArrayList<>(Arrays.asList("field1", 
      "field1val2", "field1val3")); 
    static List<String> val2 = new ArrayList<>(Arrays.asList("field2", 
      "field2val2", "field2val3")); 

    static int length = getLongestValue(val1, val2) + 4; 

    public static void main(String arg[]) { 

     BufferedWriter writeTable = null; 
     try { 
      writeTable = new BufferedWriter(new FileWriter("testtable.txt")); 

      writeDashedLine(writeTable); 
      writeTable.newLine(); 
      // Ende erste Zeile 

      for (int i = 0; i < val1.size(); i++) { 
       writeValueLine(writeTable, val1.get(i), val2.get(i)); 
       writeTable.newLine(); 

       writeDashedLine(writeTable); 
       writeTable.newLine(); 

      } 
     } catch (IOException e) { 
      System.err.println(e); 
     } finally { 
      if (writeTable != null) { 
       try { 
        writeTable.close(); 
       } catch (IOException e) { 
        System.err.println(e); 
       } 
      } 
     } 
    } 

    public static void writeDashedLine(BufferedWriter writeTable) 
      throws IOException { 
     // Anfang erste Zeile 
     writeTable.write("+ "); 
     writeDashes(writeTable); 
     writeTable.write(" + "); 
     writeDashes(writeTable); 
     writeTable.write(" +"); 
    } 

    public static void writeDashes(BufferedWriter writeTable) 
      throws IOException { 
     for (int i = 0; i < length; i++) { 
      writeTable.write("-"); 
     } 
    } 

    public static void writeValueLine(BufferedWriter writeTable, String value1, 
      String value2) throws IOException { 
     writeTable.write("| " + centerText(value1, length) + " + " 
       + centerText(value2, length) + " |"); 
    } 

    public static String centerText(String text, int length) { 
     int textLength = text.length(); 
     if (textLength > length) { 
      return text.substring(0, length); 
     } else if (textLength == length) { 
      return text; 
     } else { 
      int diff1 = (length - textLength)/2; 
      int diff2 = length - textLength - diff1; 
      return getPadding(' ', diff1) + text + getPadding(' ', diff2); 
     } 
    } 

    public static String getPadding(char pad, int length) { 
     String padding = ""; 

     for (int i = 0; i < length; i++) { 
      padding += pad; 
     } 

     return padding; 
    } 

    public static int getLongestValue(List<String> val1, List<String> val2) { 
     int length = 0; 

     for (String s : val1) { 
      length = Math.max(length, s.length()); 
     } 

     for (String s : val2) { 
      length = Math.max(length, s.length()); 
     } 

     return length; 
    } 
}