java OutputStream write off 的作用
package test;
import java.io.*;
public class test4 {
public static void main(String[] args) throws IOException
{
OutputStream out = new FileOutputStream("./test.txt");
String a = "123456789";
byte[] buff = a.getBytes();
int len=3;
out.write(buff, 0, 3);
out.flush();
out.write(buff, 0, 4);
out.close();
}
}
package test;
import java.io.*;
public class test4 {
public static void main(String[] args) throws IOException
{
OutputStream out = new FileOutputStream("./test.txt");
String a = "123456789";
byte[] buff = a.getBytes();
int len=3;
out.write(buff, 0, 3);
out.write(buff, 3, 4);
out.close();
}
}
write(byte[] b, int off, int len)
其中off和len都不允许为负值
off是相对与你的字节数组,即buff的偏移量
len是从off那个偏移位置开始写几个字节
文件的写入默认的是一点点往后加的,即C语言的fwrite(buffer,size,count,fp);
C: 要想实现改变对文件读写的偏移指针需要用fseek();
Java: 要想实现改变对文件读写的偏移指针需要用RandomAccessFile类中的seek();
另外OutputStream out = new FileOutputStream("./test.txt",true);为追加写入,即在原文件后追加写入。