方法名 描述 public void write(int b) 将制定字节写入此文件输出流 public void write(byte[] b) 将b.length个字节从制定byte数组写入此文件输出流中 public void write(byte[] b,int off,int len) 将制定byte数组中从偏移量off开始的len个字节写入此文件输出流 public void close() 关闭此文件输出流并释放与此流有关的所有系统资源
Java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputDemo {
public static void main (String[] args) {
FileOutputStream fos;
FileInputStream fis;
try {
fos = new FileOutputStream("imooc.txt" ,true );
fis = new FileInputStream("imooc.txt" );
fos.write(50 );
fos.write('a' );
System.out.println(fis.read());
System.out.println((char ) fis.read());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputDemo1 {
public static void main (String[] args) {
try {
FileInputStream fis = new FileInputStream("happy.gif" );
FileOutputStream fos = new FileOutputStream("happycopy.gif" );
int n = 0 ;
byte [] b = new byte [1024 ];
while ((n = fis.read(b)) != -1 ) {
fos.write(b);
}
fis.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}