【问题标题】:Change from using a BufferedReader for a file, to a String从对文件使用 BufferedReader 更改为 String
【发布时间】:2014-12-06 22:49:34
【问题描述】:

我有一个可以工作的应用程序,它需要一个文本文件,分阶段修改它,直到它整洁可用。 每个阶段都会接受一个文件并对其进行修改,然后吐出一个文件供下一个阶段缓冲。

我试图让它更干净,所以我想停止拉入文件,除了第一个文件,并将输出作为字符串传递给应用程序。 使用此代码,我将如何做到这一点?

这是第二阶段。

try {
                BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("C:/Stage_Two.txt")));
                StringBuffer stringBuffer = new StringBuffer();
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    Pattern pattern = Pattern.compile("ALL|MESSAGE|Time|PAPER_MAIN|GSP");
                    if (pattern.matcher(line).find()) {
                        continue;
                    }
                    stringBuffer.append(line);
                    stringBuffer.append("\n");
                }
                BufferedWriter bwr = new BufferedWriter(new FileWriter(new File("C:/Stage_Three.txt")));
                bwr.write(stringBuffer.toString());
                bwr.flush();
                bwr.close();
                // to see in console
                //System.out.println(stringBuffer);
            } catch (IOException e) {
            }

我已经研究过 InputStream、InputStreamReader 和 Reader……但如果是其中之一,我似乎无法取得进展。

【问题讨论】:

  • 您只是在寻找代码审查吗?
  • 不,从拉入文件到使用字符串进行更改的方向。
  • 阅读一些关于装饰器模式的内容。考虑让与这些“修饰符类”无关的不同组件负责读取和/或写入文件。
  • 如果你想要一个肮脏的黑客然后使用StringReaderStringWriter。否则,只需处理使用来自Flies.lines()Stream<String> 并创建一个管道。

标签: java inputstream bufferedreader


【解决方案1】:

我不确定字符串如何清理它。使用 reader 和 writers 的好处是您不需要将所有内容都保存在内存中。以下代码将允许处理非常大的文件。

public void transformFile(File in, File out) throws IOException {

    /*
     * This method allocates the resources needed to perform the operation
     * and releases them once the operation is done. This mechanism is know
     * as a try-with-resource. After the try statement exits, the resources
     * are closed
     */

    try (BufferedReader bin = new BufferedReader(new FileReader(in));
            Writer bout = new FileWriter(out)) {

        transformBufferedReader(bin, bout);
    }
}

private void transformBufferedReader(BufferedReader in, Writer out) throws IOException {
    /*
     * This method iterates over the lines in the reader and figures out if
     * it should be written to the file
     */

    String line = null;
    while ((line = in.readLine()) != null) {
        if (isWriteLine(line)) writeLine(line, out);
    }
}

private boolean isWriteLine(String line) throws IOException {

    /*
     * This tests if the line should be written
     */

    return !line.matches("ALL|MESSAGE|Time|PAPER_MAIN|GSP");
}

private void writeLine(String line, Writer writer) throws IOException {

    /*
     * Write a line out to the writer
     */

    writer.append(line);
    writer.append('\n');
}

如果你坚持使用字符串,你可以添加以下方法。

public String transformString(String str) {
    try (BufferedReader bin = new BufferedReader(new StringReader(str));
            Writer bout = new StringWriter()) {

        transformBufferedReader(bin, bout);
        return bout.toString();
    } catch (IOException e) {
        throw new IllegalStateException("the string readers shouln't be throwing IOExceptions");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 2017-02-11
    • 1970-01-01
    • 2017-12-01
    相关资源
    最近更新 更多