黑马程序员--IO流之字节流
2014-09-24 22:52
302 查看
-----------ASP.Net+Unity开发----.Net培训------------------
字节流:
InputStream:是表示字节输入流的所有类的超类。
|--- FileInputStream:从文件系统中的某个文件中获得输入字节。哪些文件可用取决于主机环境。FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑
使用 FileReader。
|--- FilterInputStream:包含其他一些输入流,它将这些流用作其基本数据源,它可以直接传输数据或提供一些额外的功能。
|--- BufferedInputStream:该类实现缓冲的输入流。
|--- Stream:
|--- ObjectInputStream:
|--- PipedInputStream:
-----------------------------------------------
OutputStream:此抽象类是表示输出字节流的所有类的超类。
|--- FileOutputStream:文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。
|--- FilterOutputStream:此类是过滤输出流的所有类的超类。
|--- BufferedOutputStream:该类实现缓冲的输出流。
|--- PrintStream:
|--- DataOutputStream:
|--- ObjectOutputStream:
|--- PipedOutputStream:
--------------------------------
缓冲区是提高效率用的,给谁提高呢?
BufferedWriter:是给字符输出流提高效率用的,那就意味着,缓冲区对象建立时,必须要先有流对象。明确要提高具体的流对象的效率。
流对象:其实很简单,就是读取和写入。但是因为功能的不同,流的体系中提供N多的对象。那么开始时,到底该用哪个对象更为合适呢?这就需要明确流的操作规律。
流的操作规律:
1,明确源和目的。
数据源:就是需要读取,可以使用两个体系:InputStream、Reader;
数据汇:就是需要写入,可以使用两个体系:OutputStream、Writer;
2,操作的数据是否是纯文本数据?
如果是:数据源:Reader
数据汇:Writer
如果不是:数据源:InputStream
数据汇:OutputStream
3,虽然确定了一个体系,但是该体系中有太多的对象,到底用哪个呢?
明确操作的数据设备。
数据源对应的设备:硬盘(File),内存(数组),键盘(System.in)
数据汇对应的设备:硬盘(File),内存(数组),控制台(System.out)。
4,需要在基本操作上附加其他功能吗?比如缓冲。
如果需要就进行装饰。
转换流特有功能:转换流可以将字节转成字符,原因在于,将获取到的字节通过查编码表获取到指定对应字符。
转换流的最强功能就是基于 字节流 + 编码表 。没有转换,没有字符流。
发现转换流有一个子类就是操作文件的字符流对象:
InputStreamReader
|--FileReader
OutputStreamWriter
|--FileWrier
想要操作文本文件,必须要进行编码转换,而编码转换动作转换流都完成了。所以操作文件的流对象只要继承自转换流就可以读取一个字符了。
但是子类有一个局限性,就是子类中使用的编码是固定的,是本机默认的编码表,对于简体中文版的系统默认码表是GBK。
FileReader fr = new FileReader("a.txt");
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"gbk");
以上两句代码功能一致,
如果仅仅使用平台默认码表,就使用FileReader fr = new FileReader("a.txt"); //因为简化。
如果需要制定码表,必须用转换流。
转换流 = 字节流+编码表。
转换流的子类File = 字节流 + 默认编码表。
凡是操作设备上的文本数据,涉及编码转换,必须使用转换流。
字节流代码示例:
sop代表System.out.println();
1、通过字节流向文件中写入数据
2、通过字节流从文件中读取数据
4、复制MP3
5、将一个文本文件中的内容打印到控制台上
6、将控制台上的输入字符存入到文本文件中
-----------ASP.Net+Unity开发----.Net培训------------------
详情请查看http://www.itheima.com
字节流:
InputStream:是表示字节输入流的所有类的超类。
|--- FileInputStream:从文件系统中的某个文件中获得输入字节。哪些文件可用取决于主机环境。FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑
使用 FileReader。
|--- FilterInputStream:包含其他一些输入流,它将这些流用作其基本数据源,它可以直接传输数据或提供一些额外的功能。
|--- BufferedInputStream:该类实现缓冲的输入流。
|--- Stream:
|--- ObjectInputStream:
|--- PipedInputStream:
-----------------------------------------------
OutputStream:此抽象类是表示输出字节流的所有类的超类。
|--- FileOutputStream:文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。
|--- FilterOutputStream:此类是过滤输出流的所有类的超类。
|--- BufferedOutputStream:该类实现缓冲的输出流。
|--- PrintStream:
|--- DataOutputStream:
|--- ObjectOutputStream:
|--- PipedOutputStream:
--------------------------------
缓冲区是提高效率用的,给谁提高呢?
BufferedWriter:是给字符输出流提高效率用的,那就意味着,缓冲区对象建立时,必须要先有流对象。明确要提高具体的流对象的效率。
FileWriter fw = new FileWriter("bufdemo.txt"); BufferedWriter bufw = new BufferedWriter(fw);//让缓冲区和指定流相关联。 for(int x=0; x<4; x++){ bufw.write(x+"abc"); bufw.newLine(); //写入一个换行符,这个换行符可以依据平台的不同写入不同的换行符。 bufw.flush();//对缓冲区进行刷新,可以让数据到目的地中。 } bufw.close();//关闭缓冲区,其实就是在关闭具体的流。
BufferedReader:
FileReader fr = new FileReader("bufdemo.txt"); BufferedReader bufr = new BufferedReader(fr); String line = null; while((line=bufr.readLine())!=null){ //readLine方法返回的时候是不带换行符的。 System.out.println(line); } bufr.close();
//记住,只要一读取键盘录入,就用这句话。 BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));//输出到控制台 String line = null; while((line=bufr.readLine())!=null){ if("over".equals(line)) break; bufw.write(line.toUpperCase());//将输入的字符转成大写字符输出 bufw.newLine(); bufw.flush(); } bufw.close(); bufr.close();
流对象:其实很简单,就是读取和写入。但是因为功能的不同,流的体系中提供N多的对象。那么开始时,到底该用哪个对象更为合适呢?这就需要明确流的操作规律。
流的操作规律:
1,明确源和目的。
数据源:就是需要读取,可以使用两个体系:InputStream、Reader;
数据汇:就是需要写入,可以使用两个体系:OutputStream、Writer;
2,操作的数据是否是纯文本数据?
如果是:数据源:Reader
数据汇:Writer
如果不是:数据源:InputStream
数据汇:OutputStream
3,虽然确定了一个体系,但是该体系中有太多的对象,到底用哪个呢?
明确操作的数据设备。
数据源对应的设备:硬盘(File),内存(数组),键盘(System.in)
数据汇对应的设备:硬盘(File),内存(数组),控制台(System.out)。
4,需要在基本操作上附加其他功能吗?比如缓冲。
如果需要就进行装饰。
转换流特有功能:转换流可以将字节转成字符,原因在于,将获取到的字节通过查编码表获取到指定对应字符。
转换流的最强功能就是基于 字节流 + 编码表 。没有转换,没有字符流。
发现转换流有一个子类就是操作文件的字符流对象:
InputStreamReader
|--FileReader
OutputStreamWriter
|--FileWrier
想要操作文本文件,必须要进行编码转换,而编码转换动作转换流都完成了。所以操作文件的流对象只要继承自转换流就可以读取一个字符了。
但是子类有一个局限性,就是子类中使用的编码是固定的,是本机默认的编码表,对于简体中文版的系统默认码表是GBK。
FileReader fr = new FileReader("a.txt");
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"gbk");
以上两句代码功能一致,
如果仅仅使用平台默认码表,就使用FileReader fr = new FileReader("a.txt"); //因为简化。
如果需要制定码表,必须用转换流。
转换流 = 字节流+编码表。
转换流的子类File = 字节流 + 默认编码表。
凡是操作设备上的文本数据,涉及编码转换,必须使用转换流。
字节流代码示例:
sop代表System.out.println();
1、通过字节流向文件中写入数据
public void writeFile() { FileOutputStream fos =null; try { fos = new FileOutputStream("e:\\fos.txt"); fos.write("abcde".getBytes()); } catch(IOException e) { sop(e.toString()); } finally { try { if(fos!=null) fos.close(); } catch(IOException e) { sop(e.toString()); } } }
2、通过字节流从文件中读取数据
public void readFile() { FileInputStream fis =null; try { fis = new FileInputStream("e:\\fos.txt"); byte[] buf = new byte[1024]; int len= 0; while((len=fis.read(buf))!=-1) { sop(new String(buf,0,len)); } //fos.read("abcde".getBytes()); } catch(IOException e) { sop(e.toString()); } finally { try { if(fis!=null) fis.close(); } catch(IOException e) { sop(e.toString()); } } }3、复制图片
public void copyPic() { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("f:\\20130417019.jpg"); fos = new FileOutputStream("e:\\20130417019.jpg"); byte[] buf = new byte[1024]; int len = 0; while((len=fis.read(buf))!=-1) { fos.write(buf,0,len); } } catch(IOException e) { sop(e.toString()); } finally { try { if(fos!=null) fos.close(); } catch(IOException e) { sop(e.toString()); } try { if(fis!=null) fis.close(); } catch(IOException e) { sop(e.toString()); } } }
4、复制MP3
public void copyMp3() { BufferedInputStream bufis = null; BufferedOutputStream bufos = null; try { bufis = new BufferedInputStream(new FileInputStream("e:\\Adam Lambert-Trespassing.mp3")); bufos = new BufferedOutputStream(new FileOutputStream("f:\\Adam Lambert-Trespassing.mp3")); int by = 0; byte[] buf = new byte[1024]; while((by=bufis.read(buf))!=-1) { bufos.write(buf,0,by); } } catch(IOException e) { sop(e.toString()); } finally { try { if(bufis!=null) bufis.close(); } catch(IOException e) { sop(e.toString()); } try { if(bufos!=null) bufos.close(); } catch(IOException e) { sop(e.toString()); } } }
5、将一个文本文件中的内容打印到控制台上
public void transStream() { BufferedWriter bfrw = null; BufferedReader bfrd = null; //FileReader fr = null; try { bfrw = new BufferedWriter(new OutputStreamWriter(System.out)); bfrd = new BufferedReader(new InputStreamReader(new FileInputStream("e:\\demo.txt"))); String line = null; while((line=bfrd.readLine())!=null) { bfrw.write(line); bfrw.newLine(); bfrw.flush(); } } catch(IOException e) { sop(e.toString()); } finally { try { if(bfrw!=null) bfrw.close(); } catch(IOException e) { sop(e.toString()); } try { if(bfrd!=null) bfrd.close(); } catch(IOException e) { sop(e.toString()); } } }
6、将控制台上的输入字符存入到文本文件中
public void transstream() { BufferedReader bufr = null; BufferedWriter bufw = null; try { bufr = new BufferedReader(new InputStreamReader(System.in)); bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("e:\\haha.txt"))); String line = null; while((line=bufr.readLine())!=null) { if(line.equals("over")) break; bufw.write(line); bufw.newLine(); bufw.flush(); } } catch(IOException e) { sop(e.toString()); } finally { try { if(bufr!=null) bufr.close(); } catch(IOException e) { sop(e.toString()); } try { if(bufw!=null) bufw.close(); } catch(IOException e) { sop(e.toString()); } } }
-----------ASP.Net+Unity开发----.Net培训------------------
详情请查看http://www.itheima.com
相关文章推荐
- 黑马程序员——io流(二)字节流
- 黑马程序员--IO流--字节流
- 黑马程序员--IO流之字节流
- 黑马程序员_IO流二(字符缓冲区,字节缓冲区,字节字符转换流)
- 黑马程序员-8-java-IO流知识点串讲(2)-字节流及字节字符转换流
- 黑马程序员_Java_IO流_字节流
- 黑马程序员_IO流之字节流
- 黑马程序员——IO流-字节流
- 黑马程序员——Java语言基础——07.IO流(1)字符流和字节流
- 黑马程序员_IO流2_(字符缓冲区,字节缓冲区,字节字符转换流)
- 黑马程序员__异常处理_File类_IO流之字节读写
- 黑马程序员——Java基础——IO流(下)之字节流
- 黑马程序员 io流中的字节流、字节缓冲流、转换流(重要)(毕向东)
- 黑马程序员——JAVA基础------IO流(四)----字节字符转换流和缓冲流
- 黑马程序员--java基础:File类和文件字节IO流
- 黑马程序员—IO流(上)字符流、字节流
- 黑马程序员---IO流-异常
- 黑马程序员_IO流
- Day20 --IO流对象 字节流
- 黑马程序员--Java基础学习笔记【IO流-字节流、转换流】