不多bb,上代码
@Test
void f1() throws IOException{
File file = new File("./a.txt");
byte[] s="123456".getBytes();
//方法一
OutputStream out=new FileOutputStream(file,true);// 如果为true ,则字节将写入文件的末尾而不是开头
out.write(s);
//方法二
RandomAccessFile randomAccessFile=new RandomAccessFile(file,"rw");// w写,r读
randomAccessFile.seek(file.length());//调整文件指针,学过C/C艹的应该知道
randomAccessFile.write(s);
}
其中 RandomAccessFile
更为灵活 , 不过OutputStream
安全点,自己取舍