gpt4 book ai didi

java - 使用输出流将数据添加到特定文件

转载 作者:行者123 更新时间:2023-12-01 05:28:25 26 4
gpt4 key购买 nike

String data="this we want append in that file";
byte[] getbyte = data.getBytes();
outputstream.write(getbyte);
outputstream.flush();

通过使用这些代码,我尝试在特定文件中写入数据,但数据未添加到该文件

最佳答案

导入java.io.*;

公共(public)类WriteFileExample {

public static void main(String[] args) {

String strFilePath = "d:/FileIO.txt";

try
{
FileOutputStream fos = new FileOutputStream(strFilePath);
String strContent = "Write File using Java FileOutputStream example !";

/*
* To write byte array to a file, use
* void write(byte[] bArray) method of Java FileOutputStream class.
*
* This method writes given byte array to a file.
*/

fos.write(strContent.getBytes());

/*
* Close FileOutputStream using,
* void close() method of Java FileOutputStream class.
*
*/

fos.close();

}
catch(FileNotFoundException ex)
{
System.out.println("FileNotFoundException : " + ex);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
}

} }

关于java - 使用输出流将数据添加到特定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9407626/

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