黑马程序员_java中IO流--字节流

来源:互联网 发布:淘宝我要开店页面 编辑:程序博客网 时间:2024/05/21 22:40
一、java中的字节流
  1. 字节流的抽象基类:InputStream,OutputStream
  2. 输入流InputStream基本方法
    • int b = in.read();//读取一个byte字节,无符号填充到int的低八位,eof为-1
    • in.read(byte[] buf);读取数据填充到buf中
    • in.read(byte[] buf,int start,int size) 读取数据填充到buf中
    • in.skip(long n) 跳过和丢弃此输入流中数据的n个字节
     3. 输出流OutputStream的基本方法
    • ​out.write(int b);//写出一个byte到流b的第八位输出
    • out.write(byte[] buf);//将缓冲区buf都写入到流
    • out.write(byte[] buf,int start,int size);
    • out.flush();//清除缓存
    • out.close();//关闭流
                                 InputStream        OutputStream
文件(Byte序列)--->输入流------------>应用程序------------->输出流----->文件(Byte序列)
                                   in.read()                out.write()

二、实例:
1. FileInputStream与FIleOutputStream流 
文件读取方式示例一:
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class InputFileDemo {public static void main(String[] args) throws IOException { writeFile(); readFile3();}//在文件中写入内容public static void writeFile() throws IOException{ //1.先创建一个输出字节流对象 FileOutputStream fos = new FileOutputStream("luqh.txt"); fos.write("abc".getBytes()); fos.close();}//读取文件中的内容//方式1.单个字符循环读取public static void readFile1() throws IOException{ //先创建输入节流对象 FileInputStream in = new FileInputStream("luqh.txt"); int ch = 0; while((ch = in.read())!=-1){  System.out.println((char)ch); } in.close();}//方式2.引用缓存技术循环读取public static void readFile2() throws IOException{ //先创建文件输入流对象 FileInputStream in = new FileInputStream("luqh.txt"); byte[] buf = new byte[1024]; int len = 0; //in.read(buf)从输入流中读取一定长度的字节存储到缓冲数组中 while((len=in.read(buf))!=-1){  System.out.println(new String(buf,0,len)); } //别忘记关闭 in.close();}//方式3.引用缓存技术读取,与上述不同的是数组长度固定public static void readFile3() throws IOException{ FileInputStream in = new FileInputStream("luqh.txt"); //按照输入流中长度定义一个长度刚刚好的数组,不用循环了//但是此种情况只使用于文件较小情况 byte[] buf = new byte[in.available()]; in.read(buf); System.out.println(new String(buf)); //别忘记关闭 in.close();}}
示例二:图片的拷贝
步骤:
  1. 用字节流读取对象与图片关联; 
  2. 用字节流写入对象创建一个图片,用于存储读到的图片数据;
  3. 通过循环读写,完成图片的复制 ;
  4. 关闭流;
代码实现:
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class bmpCopyDemo {public static void main(String[] args){ FileInputStream in = null; FileOutputStream  out = null; try{ in = new FileInputStream("c:\\1.bmp"); out = new FileOutputStream("c:\\2.bmp"); byte[] buf = new byte[1024]; int len = 0; while((len=in.read(buf))!=-1){  out.write(buf,0,len); } } catch(IOException e){  throw new RuntimeException("复制文件失败。"); } finally{  try{   if(in!=null){    in.close();   }  }catch(IOException e){   throw new RuntimeException("读取文件关闭失败。");  }  try{   if(out!=null){    in.close();   }  }catch(IOException e){   throw new RuntimeException("写入文件关闭失败。");  } } }}
示例三:通过缓冲区,MP3文件的拷贝
public class mp3CopyDemo {public static void main(String[] args) throws IOException { writer();}public static void writer() throws IOException{//通过缓冲区复制MP3,当然上述中的1.bmp也可以通过缓冲区复制//(new FileInputStream("c:\\0.mp3")读硬盘。把从硬盘读取的数据存放到缓冲区bifs 中 BufferedInputStream bifs = new BufferedInputStream(new FileInputStream("c:\\0.mp3")); BufferedOutputStream bofs = new BufferedOutputStream(new FileOutputStream("c:\\1.mp3")); int by = 0; while((by=bifs.read())!=-1){  bofs.write(by); }   bofs.close();}}
2、字节输入数组流ByteArrayInputStream、字节输出数组流ByteArrayOutputStream
  • 字节输出数组流ByteArrayOutputStream
  • ByteArrayInputStream 包含一个内部缓冲区,该缓冲区包含从流中读取的字节。内部计数器跟踪 read 方法要提供的下一个字节。
  • 关闭 ByteArrayInputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOExceptio
  • 实例:              
import java.io.ByteArrayInputStream;public class ArrayInputStreamDemo { public static void main(String[] args) {  String str = "luqh";  //字符串转换成字节数组  byte[] buf = str.getBytes();  //创建一个字节数组输入流对象,buf当作其缓存数组  ByteArrayInputStream bytein = new ByteArrayInputStream(buf);  //从字节数组输入流中读取字节  int data = bytein.read();  while(data!=-1){   char upper = Character.toUpperCase((char)data);   System.out.print(upper);   data = bytein.read();  }}}//运行结果:LUQH
  • 字节输出数组流ByteArrayOutputStream
    • 此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray() 和 toString() 获取数据。
    • 实例:
import java.io.ByteArrayOutputStream; import java.io.IOException; public class ArrayOutputStreamDemo { public static void main(String[] args) throws IOException {  String str="luqh is a girl";  //创建一个字节数组输出流对象  ByteArrayOutputStream byteOut = new ByteArrayOutputStream();  byte[] buf = str.getBytes();  //将指定的byte数组写到字节数组输出流中  byteOut.write(buf);  //将字节数组输出流中的内容转换成字符串输出  System.out.println(byteOut.toString());    //将字节数组输出流中的内容复制到字节数组  byte[] b = byteOut.toByteArray();  for(int i = 0;i<b.length;i++){   System.out.print((char)b[i]);  } } } //输出结果:luqh is a girl // luqh is a girl


        
0 0
原创粉丝点击