Java noob問題 - 如何將字符串存儲到新的文本文件

[英]Java noob question - how to store a string to a new text file


Here is my scenario:

這是我的情景:

Selenium grabbed some text on the html page and convert it to a string (String store_txt = selenium.getText("text");) - the text is dynamically generated.

Selenium抓取了html頁面上的一些文本並將其轉換為字符串(String store_txt = selenium.getText(“text”);) - 文本是動態生成的。

Now I want to store this string into a new text file locally every time I run this test, should I use FileWriter? Or is it as simple as writing a System.out.println("string");?

現在我想在每次運行此測試時將此字符串存儲到本地新文本文件中,我應該使用FileWriter嗎?或者它就像編寫System.out.println(“string”)一樣簡單;?

Do I have to write this as a class or can I write a method instead?

我必須把它寫成一個類,還是可以編寫一個方法?

Thanks in advance!!

提前致謝!!

3 个解决方案

#1


Use createTempFile to create a new file every time, use FileWriter to write to the file.

每次使用createTempFile創建一個新文件,使用FileWriter寫入該文件。

import java.io.File;
import java.io.IOException;
import java.io.FileWriter;

public class Main {
    public static void main(String[] args) throws IOException {
        File f = File.createTempFile("selenium", "txt");
        FileWriter writer = new FileWriter(f);
        writer.append("text");
    }
}

#2


Yes, you need to use a FileWriter to save the text to file.

是的,您需要使用FileWriter將文本保存到文件。

System.out.println("string");

just prints to the screen in console mode.

只需在控制台模式下打印到屏幕。

#3


Always remember to close the filewriter afterwards using

務必記得以后關閉文件編寫器

writer.close()

Otherwise you could end up with a half written file.

否則你最終會得到一個半文件。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/09/8b3417fd0055f675704345934a942cc2.html



 
  © 2014-2022 ITdaan.com 联系我们: