在Selenium自动化中追加数据到CSV文件中。

huangapple go评论89阅读模式
英文:

Appending data in a CSV in selenium automation

问题

public static void writeCSV(String filePath, String separator) throws IOException {
    try (OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(filePath));
            Writer outStreamWriter = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8);
            BufferedWriter buffWriter = new BufferedWriter(outStreamWriter)) {
        buffWriter.append("https://mobile/sample_v4.zip");
        buffWriter.append(separator);
        buffWriter.append(createdTitle);
        buffWriter.append(separator);
        buffWriter.append("http://2-title-conversion/documentlibrary");
        buffWriter.append(separator);
        buffWriter.append("TRUE");
        buffWriter.append(separator);
        buffWriter.append("TRUE");
        buffWriter.flush();
    }
}

@Test segment,

loginPg.writeCSV("C:\\Users\\urathya\\Documents\\Automation\-11\\resources\\CS.csv", ",");
英文:

I have a CSV file in Resources of my automation script and I need to amend one cell value to a parameter value I get by creating a folder in a site, I ran this code but then an error comes:

"(The process cannot access the file because it is being used by another process)".

Can anyone let me know how to write my parameter value to CSV file cell, please.
TIA

Method:

 public static void writeCSV(String filePath, String separator) throws IOException {
		try (OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(filePath));
			 Writer outStreamWriter = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8);
			 BufferedWriter buffWriter = new BufferedWriter(outStreamWriter)) {
			buffWriter.append("https://mobile/sample_v4.zip");
			buffWriter.append(separator);
			buffWriter.append(createdTitle);
			buffWriter.append(separator);
			buffWriter.append("http://2-title-conversion/documentlibrary");
			buffWriter.append(separator);
			buffWriter.append("TRUE");
			buffWriter.append(separator);
			buffWriter.append("TRUE");
			buffWriter.flush();
		}


@Test segment,

loginPg.writeCSV("C:\\Users\\urathya\\Documents\\Automation\-11\\resources\\CS.csv",",");

答案1

得分: 0

你没有关闭输出流,请关闭它,这将关闭文件,然后您可以使用相同的文件来追加数据。

英文:

You are not closing the output stream, please close it, it will close file and you can use the same file to append the data.

huangapple
  • 本文由 发表于 2020年4月8日 15:39:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61095567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定