【问题标题】:Write data to file freshly each time每次都将数据新鲜写入文件
【发布时间】:2016-07-21 04:32:41
【问题描述】:

我正在编写一个程序,其中有一些数据,我必须从多个文档中挑选并创建一个新文件。

下面是我的代码。

public class IndexFiles extends SwingWorker<Void, String> {
    private String inputPath;

    public IndexFiles(String inputPath) {
        this.inputPath = inputPath;
    }

    @Override
    protected Void doInBackground() throws Exception {
        File directory = new File(inputPath);
        countFilesInDirectory(directory);
        return null;
    }

    void countFilesInDirectory(File directory) throws IOException {
        System.out.println("entered");
        File[] list = directory.listFiles();
        System.out.println("length is " + list.length);
        for (int i = 0; i < list.length; i++) {
            GenerateFiles(list[i].getAbsoluteFile().toString());
        }

    }

    private void GenerateFiles(String inputfilePath) throws IOException {
        String input = inputfilePath;
        URL url = new URL("file:///" + input);
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String inputLine;

        String tempPath = inputfilePath.substring(0, inputfilePath.lastIndexOf("\\") + 1);
        String secPath = tempPath.substring(0, tempPath.lastIndexOf("\\"));
        String finalPath = secPath.substring(0, secPath.lastIndexOf("\\")) + "\\OP\\";

        File outPath = new File(finalPath);
        if (!outPath.exists()) {
            outPath.mkdir();
        }
        File temp = new File(finalPath + "temp.txt");
        FileOutputStream fos = new FileOutputStream(temp, true);

        if (!temp.exists()) {
            temp.createNewFile();
        }
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        bw.write("");
        while ((inputLine = in.readLine()) != null) {
            if (inputLine.contains("/section")) {
                break;
            }
            if (inputLine.contains("DOCTYPE")) {
                inputLine = inputLine.replace("<!DOCTYPE html>", "");
            }
            if (inputLine.contains("html")) {
                inputLine = inputLine.replaceAll(
                        "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"content-type\" content=\"text/html\" charset=\"utf-8\"/>(.*)<section>",
                        "");
            }
            if (inputLine.contains("?")) {
                inputLine = inputLine.replace("<?", "");
            }
            if (inputLine.contains("pb")) {
                inputLine = inputLine.replaceAll("pb label=\"(.*)\"?>", "");
            }
            if (!(inputLine.trim().isEmpty())) {
                bw.append(inputLine);
                bw.newLine();
            }
        }
        bw.close();
        in.close();

    }

}

这个程序第一次运行良好。该文件正在正确创建。但是当我再次运行它时问题就来了,它被附加而不是创建一个新的新数据。我确定这是因为FileOutputStream fos = new FileOutputStream(temp, true);,如果我删除true,每次完成文件时,都会更新一个新副本。但我的要求是每次运行程序时都创建一个新数据,而不是附加到现有数据中。

我说有 10 个文件,我运行程序,所有 10 个文件的内容都应该进入临时文件。我再次运行它,应该是,我清除了临时文件。写下这10个文件的内容。但是现在,我运行程序,数据被写入临时文件,我再次运行它,它附加了相同的 10 个文件数据。我想替换全部内容,而不是单个文件内容。第一次运行时,10 个文件数据被写入临时文件。我再运行一次,应该清空临时文件,写入这10个文件数据。

【问题讨论】:

  • 我认为问题在于相同的文件名。尝试在文件名处附加时间戳。所以每次它都会创建新文件。
  • 嗨@TomRees,不,不是。我已经清楚地提到了上面的问题。
  • 嗨@sAm,让我试试。 ;)
  • 问题是他多次创建了writer。他想要的是在运行中创建的第一个写入者应该在写入之前清除文件,而后续写入者应该追加。我认为 :-) 我在回答中提出了两种方法来做到这一点。

标签: java


【解决方案1】:

选择一个:

  • 在 countFilesInDirectory 中创建不带 true 标志的 BufferedWriter,并将其作为参数传递给 GenerateFiles
  • 在 countFilesInDirectory 的 for 循环之前添加删除临时文件的代码

【讨论】:

    【解决方案2】:

    使用

     FileOutputStream fos = new FileOutputStream(temp);
    

    而不是

     FileOutputStream fos = new FileOutputStream(temp, true);
    

    对于 FileOutputStream(File file, boolean append) 构造函数 如果 append 设置为 true,您写入的数据将附加到文件末尾,而不是覆盖已经存在的内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 2011-02-24
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多