Java 每次都将数据新写入文件
我正在编写一个程序,其中有一些数据,我必须从多个文档中选择这些数据,并创建一个新文件 下面是我的代码Java 每次都将数据新写入文件,java,Java,我正在编写一个程序,其中有一些数据,我必须从多个文档中选择这些数据,并创建一个新文件 下面是我的代码 public class IndexFiles extends SwingWorker<Void, String> { private String inputPath; public IndexFiles(String inputPath) { this.inputPath = inputPath; } @Override
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();
}
}
公共类索引文件扩展SwingWorker{
私有字符串输入路径;
公共索引文件(字符串输入路径){
this.inputPath=inputPath;
}
@凌驾
受保护的Void doInBackground()引发异常{
文件目录=新文件(inputPath);
countFilesInDirectory(目录);
返回null;
}
void countFilesInDirectory(文件目录)引发IOException{
系统输出打印项次(“输入”);
File[]list=directory.listFiles();
System.out.println(“长度为”+list.length);
for(int i=0;i
这个程序第一次运行良好。正在正确创建文件。但问题是,当我再次运行这个程序时,它会被追加,而不是创建新的数据。我确信这是因为FileOutputStream fos=newfileoutputstream(temp,true)
,如果我删除true
,则每次完成一个文件时,都会更新一个新副本。但我的要求是每次运行程序时都创建一个新的数据,而不是附加到现有的数据中
我已经说了10个文件,我运行了这个程序,所有10个文件的内容都应该放到临时文件中。我再次运行它,应该是,我清除了临时文件。写下这10个文件的内容。但是现在,我运行这个程序,数据被写入临时文件,我再次运行它,它被附加上同样的10个文件数据。我想替换完整内容,但不是单个文件内容。我第一次运行时,10个文件的数据被写入临时文件。我再次运行它,应该清除临时文件并写入这10个文件数据。选择一个:
- 在countFilesInDirectory中创建不带true标志的BufferedWriter,并将其作为参数传递给GenerateFile
- 添加代码以删除countFilesInDirectory中for循环之前的临时文件
FileOutputStream fos = new FileOutputStream(temp);
而不是
FileOutputStream fos = new FileOutputStream(temp, true);
对于FileOutputStream(文件,布尔追加)构造函数
如果append设置为true,您写入的数据将被追加到文件的末尾,而不是覆盖已经存在的内容重复?我认为问题在于相同的文件名。尝试在文件名处附加时间戳。所以每次它都会创建新文件。嗨@TomRees,不是。我已经清楚地提到了上面的问题。嗨,山姆,让我试试看。)问题是他多次创造了作家。他想要的是,在运行中创建的第一个写入程序应该在写入之前清除文件,而子脚本写入程序应该追加。我想:-)我在回答中提出了两种方法。