FileOutputStream和文件的复制黏贴

/*
java.io.OutputStream;
java.io.FileOutputStream;   //文件字节输出流

将计算机内存中的数据写入硬盘文件
*/
import java.io.*;


public class fuck4{

public static void main(String[] args){
FileOutputStream fos=null;
try{

//1.创建文件字节输出流
//谨慎使用,会将原文件内容覆盖
//FileOutputStream(String name)
//fos=new FileOutputStream("520");//该文件不存在则自动创建

//以追加的方式写入
//FileOutputStream(File file, boolean append)
fos=new FileOutputStream("520",true);

//2.开始写
String msg="520iloveyou";

//将String类型转换成byte数组
//byte[] bytes=new byte[]; 不一定每次都是这个格式,左边是定义数组,右边视情况而定
byte[] bytes=msg.getBytes();

//fos.write(bytes);将数组全部写入
fos.write(bytes,0,3);//部分写入

//推荐最后的时候为了保证数据完全写入硬盘,所有要刷新
fos.flush();//强制写入

}catch(Exception e){
e.printStackTrace();
}finally{
if(fos!=null){
try{
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}

}

}


/*
关于文件的复制黏贴
*/
import java.io.*;
public class fuck5{

public static void main(String[] args)throws Exception{
//创建输出流,把文件输出到硬盘
FileOutputStream fos=new FileOutputStream("d:/520");

//创建输入流,把文件输入到内存
FileInputStream fis=new FileInputStream("520");

//一边读一边写
int temp=0;
byte[] bytes=new byte[1024];//每次读取1KB
while((temp=fis.read())!=-1){
fos.write(bytes,0,temp);
}
//刷新
fos.flush();

//关闭
fis.close();
fos.close();

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值