从java中的文本文件中删除列

时间:2014-04-02 12:02:12

标签: java text

我有一个包含二进制数的文本文件如何从文本文件中删除一列

1 0 1 1 0

0 1 1 0 1

1 1 1 0 1

0 1 0 0 1

例如我想删除第二列(0 1 1 1)。有什么方法可以完成吗?

1 个答案:

答案 0 :(得分:0)

public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new FileReader(new File("input.txt")));
    PrintWriter out = new PrintWriter(new File("output.txt"));

    int skipIndex = 2; //we want to delete a 2nd column

    String line;
    while ((line = in.readLine()) != null) {
        String[] parts = line.split(" ");
        StringBuilder outLine = new StringBuilder();
        for (int i = 0; i < parts.length; i++) {
            if (i + 1 != skipIndex) {
                outLine.append(" " + parts[i]);
            }
        }
        out.println(outLine.toString().trim());
    }

    in.close();
    out.close();
}