java 字符流图片_java 字节流、字符流(复制图片、复制文本)

1、File

1)构造方法

70dc6db531c2

2)创建

70dc6db531c2

3)判断、获取

70dc6db531c2

4)删除

70dc6db531c2

2、字节流

70dc6db531c2

1)字节流写数据的3种方式

70dc6db531c2

FileOutputStream fos = new FileOutputStream("e:\\a.txx");

//将字符转为字节

Bytes[]bytes = "abcdefffjdlsajflasdfjklasdf".getBytes();

fos.write(bytes,0,bytes.length);

//关闭资源

fos.close();

2)字节流写数据实现换行

fos.write("\rn\".getBytes());

windows:\r\n

linux:\n

mac:\r

3)字节流写数据实现追加

FileOutputStream fos = new FileOutputStream("e:\\a.txx",true);

//加上true之后,就是从文件的末尾写入,不加true,默认是从文件的开头写入

4)异常处理

FileOutputStream fos = null;

try{

fos = new FileOutputStream("e:\\a.txx",true);

}catch(IOExcetion p){

e.printStackTrace();

}finally{

//不管文件读写有没有出错,最后一定要 close资源

if(fos !=null) fos.close();

}

3、读写文件

1)读取文件

70dc6db531c2

//读取文件的数据,读物1024整数个字节

public static  void readFile2()throws IOException {

FileInputStream input =new FileInputStream("e:\\file.txt");

//读取1024字节及其整数倍

byte[]bytes =new byte[1024];

//单次读取的长度

int len;

while ((len =input.read(bytes)) != -1){

System.out.println("长度:"+len);

System.out.println(new String(bytes,0,len));

}

2)写入文件,读1024整数倍个字节

70dc6db531c2

//实现将file.txt内容写入到file2.txt,读取1024整数倍个字节

public static void  readAndWrite() throws IOException {

//先创建file2.txt

FileOutputStream out = new FileOutputStream("e:\\file2.txt",true);

//获得file.txt,用于读取文件

FileInputStream input  = new FileInputStream("e:\\file.txt");

//装读取文件的流

byte[] b  = new byte[1024];

//装单次读入大小

int  len;

//读取文件数据

while((len = input.read(b)) != -1){

//将数据写入到file2.txt

out.write(b,0,len);

}

out.close();

input.close();

}

3)读取1个字节就写入一个字节

70dc6db531c2

4、复制图片

70dc6db531c2

//复制图片

public  static  void readPicture() throws IOException {

//图片目的地

FileOutputStream out = new FileOutputStream("e:\\pic.png");

//图片原始位置

FileInputStream inputStream =new FileInputStream("e:\\25.png");

//存取单次读入的数据

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值