1.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class StreamTest06 {
public static void main(String[] args) {
FileOutputStream fileOutputStream=null;
try {
//1.创建文件字节输出流
// fileOutputStream=new FileOutputStream("temp09.txt");
//以追加的方式写入
fileOutputStream=new FileOutputStream("temp09.txt",true);
//2.开始写
String msString="HelloWorld";
//将String转换成byte数组
byte[] bytes=msString.getBytes();
//将byte数组中所以数据写入
//fileOutputStream.write(bytes);
//将byte数组中一部分写入
fileOutputStream.write(bytes, 0, 4);
//推荐最后的时候为了保证数据完全写入硬盘,所以要刷新
fileOutputStream.flush();//强制刷新
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(fileOutputStream!=null) {
try {
fileOutputStream.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
}
2.copy文件
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* 关于文件的赋值粘贴
* @author LK
*
*/
public class StreamTest07 {
public static void main(String[] args) throws Exception {
//创建输入流
FileInputStream fileInputStream=new FileInputStream("temp09.txt");
//创建输出流
FileOutputStream fileOutputStream=new FileOutputStream("temp10.txt");
//一边读,一边写
byte[] bytes=new byte[1024];
int temp=0;
while ((temp=fileInputStream.read(bytes))!=-1) {
//将byte数组中内容直接写入
fileOutputStream.write(bytes,0,temp);
}
//刷新
fileOutputStream.flush();
//关闭
fileInputStream.close();
fileOutputStream.close();
}
}