对什么样的文本作什么样的格式化?给个Sample,我好具体运行测试一下

解决方案 »

  1.   

    所有内容都到一个StringBuffer中,当然痛苦了。
      

  2.   

    最简单就是对txt文本进行格式化。
    比如:
    a b
    a  b(中间是一个空格加一个tab)
      

  3.   

    awk '{print $1"     "$2}' sample.txt
    同样的功能,unix下面工具好用多了。
      

  4.   

    俺赞同teaky2002(种田硬手) 的意见(windows下面也可以安装awk)。
    读一行处理一行:
    <<
    final class TextFileFormater {
        private static final String FILE_SEPARATOR = System.getProperties().getProperty("line.separator");    private final String srcFile;
        private final String dstFile;    TextFileFormater(String srcFile, String dstFile) {
            this.srcFile = srcFile;
            this.dstFile = dstFile;
        }    void format() throws IOException {
            BufferedReader reader = new BufferedReader(new FileReader(srcFile));
            BufferedWriter writer = new BufferedWriter(new FileWriter(dstFile, false));
            String line;
            while((line=reader.readLine())!=null) {
                String result = formatString(line);
                writer.write(result+FILE_SEPARATOR);
            }
            reader.close();
            writer.close();
        }    private String formatString(String srcStr) {
            return srcStr.replaceAll(" \t", "\t");
        }
    }
    >>
      

  5.   

    好像是用sed的全局替换更简单。
      

  6.   

    请问xiaohaiz(城里的老土,两眼依然通红!):
    用sed的全局替换更简单
    什么意思?
    多谢指教!
      

  7.   

    SED: stream editor 流编辑。一个很常用的命令,unix平台发布通常都有,windows下面也安装安装cygwin等类似的软件包括。比如把a文件中所有的" \t"替换为"\t"到b文件,运行:sed "s/ \t/\t/g" a > b