0

我有一个包含 20 000 行的文本文件。其中作为列的数据。但是列之间的空间不同,列长也不同。前任

aaaaa ()()()()()bdo()()()()()()()()  ttttt ()() dgee ()()()()()  yyyy

bbb()()()()()()()ggg ()()()()()()()(  fff()()()(gbe()()()()()()( yHH

cc()()()()()()()()dddd()()()()()()() I ()()()()bdeg()()()()()()yyyyy

这里的空格从括号中代表就像那样!!!

我想用特定的单词(例如:“name”)替换第 N 个(例如:4th)列

示例输出:

aaaaa ()()()()()bdo()()()()()()()()  ttttt ()() name ()()()()()  yyyy

bbb()()()()()()()ggg ()()()()()()()(  fff()()()(name()()()()()()( yHH

cc()()()()()()()()dddd()()()()()()() I ()()()()name()()()()()()yyyyy

这里的空格代表括号中的任何人都可以帮助我吗?

4

1 回答 1

0
public static void replaceColumn(int column, String word, File file) throws IOException {
    Scanner in = new Scanner(file);
    PrintWriter out = new PrintWriter(file);
    while (in.hasNextLine()) {
        String line = in.nextLine();
        line = line.trim();
        String columns = line.split(" ");
        columns[column] = word;
        line = arrayToString(columns, " ");
        out.println(line);
    }
    in.close();
    out.close();
}

//Helper method
private static String arrayToString(Object[] array, String separator) {
    if (array.length == 0) {
        return "";
    }
    StringBuilder sb = new StringBuilder();
    for (Object element : array) {
        sb.append(element);
        sb.append(separator);
    }
    sb.delete(sb.length - separator.length(), sb.length());
    return sb.toString();
}
于 2013-09-26T17:47:50.780 回答