文件字节输出流,负责写到磁盘,注意要刷新.flush(),非空最后记得关闭
new FileOutputStream(" ") 会清空源文件内容
new FileOutputStream(" ",true) 不会清空源文件内容
.write(byte[] a);
.write(byte[],offset,length) 写数组某几个
注:s.getBytes(); 可以返回byte数组,配合输出流
附文件拷贝代码:
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
try {
fileInputStream=new FileInputStream("D:\\git.txt");
fileOutputStream=new FileOutputStream("D:\\git1.txt");
byte[] bytes=new byte[1024*1024];
int i=0;
while(true) {
try {
if (!((i=fileInputStream.read(bytes))!=-1))
break;
fileOutputStream.write(bytes,0,i);
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally {
if(fileInputStream!=null);
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
if(fileOutputStream!=null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}