gpt4 book ai didi

java - Java将数据写入外部文件

转载 作者:行者123 更新时间:2023-12-01 17:16:48 25 4
gpt4 key购买 nike

我的 BufferedReader 中有数据,但我想在外部文件中打印数据。我该怎么做?

    URL url=new URL(input);
BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while((inputLine=br.readLine())!=null)
System.out.println(inputLine);
br.close();

上面的代码对我有用。相反,我想将数据保存在外部文件中。

最佳答案

URL url=new URL(input);
BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
BufferedWriter writer = Files.newBufferedWriter(Paths.get(""));
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
writer.write(inputLine);
// must do this: .readLine() will have stripped line endings
writer.newLine();
}
writer.close();
br.close();

关于java - Java将数据写入外部文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61371886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com