黑马程序员_java中IO流--字节流
来源:互联网 发布:淘宝我要开店页面 编辑:程序博客网 时间:2024/05/21 22:40
一、java中的字节流
- 字节流的抽象基类:InputStream,OutputStream
- 输入流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个字节
- 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();}}
示例二:图片的拷贝
步骤:
- 用字节流读取对象与图片关联;
- 用字节流写入对象创建一个图片,用于存储读到的图片数据;
- 通过循环读写,完成图片的复制 ;
- 关闭流;
代码实现:
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()
获取数据。 - 实例:
- 此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用
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
- 黑马程序员_java中IO流--字节流
- 黑马程序员_JAVA中IO流-字节流与字符流比较与转换
- 黑马程序员_java IO流
- 黑马程序员_java IO流
- 黑马程序员_Java IO流
- 黑马程序员_java IO流
- 黑马程序员_java IO流
- 黑马程序员_java IO流
- 黑马程序员_java IO流
- 黑马程序员_JAVA中IO流-字符流
- 黑马程序员_java中IO流的操作规律
- 黑马程序员---IO--字节流
- 黑马程序员_JAVA之IO流(字节流与字符流)
- 黑马程序员_JAVA之IO流的(字节流与字符流)
- 黑马程序员_java中的IO流
- 黑马程序员_java之IO流
- 黑马程序员_java之IO流
- 黑马程序员_java中的IO流1
- 求100~200之间的素数
- 编写一个函数reverse_string(char * string)(递归实现)实现:将参数字符串中的字符反向排列。要求:不能使用C函数库中的字符串操作函数。
- 黑马程序员---Java基础--IO流
- 102条简短深刻的回答,看完人生豁然开朗
- 输出乘法口诀表
- 黑马程序员_java中IO流--字节流
- 如何将非ARC的项目转换成ARC项目
- LayoutInflater作用及使用
- 101 - The Blocks Problem
- Fragment的setUserVisibleHint方法实现懒加载
- 实现下拉列表的两种方式
- 不允许创建临时变量,交换两个数的内容
- Hibernate错误 Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
- 有规律简单加法计算(for循环)