Java 使用outputstream向特定文件添加数据
通过使用这些代码,我试图在一个特殊的文件中写入数据,但数据并没有添加到该文件中import java.io.* 公共类WriteFileExample{ 公共静态void main(字符串[]args){ }Java 使用outputstream向特定文件添加数据,java,io,fileoutputstream,Java,Io,Fileoutputstream,通过使用这些代码,我试图在一个特殊的文件中写入数据,但数据并没有添加到该文件中import java.io.* 公共类WriteFileExample{ 公共静态void main(字符串[]args){ } }如何创建outputstream?我怀疑OP想要保存字符串的字节,不是序列化的字符串对象。Thanx pravin,但通过这些方法,我无法获得该文件的位置。我将该文件放在同一个包中。Hi Jeremy,在上面的代码中,我将该sting转换为字节数组,然后将该字符串写入特定的文件。你到底是
}如何创建
outputstream
?我怀疑OP想要保存字符串的字节,不是序列化的字符串对象。Thanx pravin,但通过这些方法,我无法获得该文件的位置。我将该文件放在同一个包中。Hi Jeremy,在上面的代码中,我将该sting转换为字节数组,然后将该字符串写入特定的文件。你到底是什么意思
String data="this we want append in that file";
byte[] getbyte = data.getBytes();
outputstream.write(getbyte);
outputstream.flush();
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);
}