java IO

 

读写文件:

 

字节流——图片、mp3、影音

字符流——文本

首先介绍字符流:

public static void main(String[] args) throws Exception {
  Reader rd=null;   //读
  Writer wt=null;   //写
  try {
   rd=new FileReader("pet.template");  //读取pet.template文件内容
   char[] cr = new char[1];
   int length=rd.read(cr);  //取得内容长度
   StringBuffer str=new StringBuffer();
   while(length!=-1){  //长度为-1则无数据
    str.append(cr);
    length=rd.read(cr);
   }
   wt=new FileWriter("f:\\a.txt"); //在f盘创建a.txt
   wt.write(str.toString());  //并写入数据
   System.out.println(str.toString());
  } catch (Exception e) {
  }finally{
   try {
    if(rd!=null)
     rd.close();  //一定要关闭文件流
    if(wt!=null)
     wt.close();  //..
   } catch (Exception e2) {
   }
  }

 

字节流:

  InputStream imput=new FileInputStream("Sunset.jpg");  //读取Sunset.jpg图片
  FileOutputStream output=new FileOutputStream("f:\\a.jpg");  //要输出的位置
  byte[] img=new byte[imput.available()];
  int length=imput.read(img); //读取img长度
  output.write(img);  //把图像写入f:\a.jpg
  imput.close();
  output.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值