黑马程序员----Java中的IO流学习笔记1

来源:互联网 发布:艾克里里淘宝店号码 编辑:程序博客网 时间:2024/06/01 08:11

------- android培训、java培训、期待与您交流! ----------


1 .IO流基本知识点

处理和传输数据是程序很重要的一部分。对于数据的传输,无论从哪个源头到哪个接收端(键盘、硬盘、内存以及网络)往往离不开IO流。JavaIO流的操作都在Java.IO包中。

IO流的分类方法:将外设设备数据读取到内存中:输入流。将内存中的数据写入到外设中:输出流。

按所操作的数据分为两种:字节流与字符流。简单点可以归纳为:字节流+编码=字符流。

字节流抽象基类:InputStream OutputStream

字符流抽象基类:ReaderWriter

(四个基本流的子类都以其结尾,如FileReader


2.FileReader/FileWriter

最基本的字符流,以char[]作为缓冲点

import java.io.FileReader;import java.io.FileWriter;/* * 创建一个文件并在控制台中写出 */public class FileRW {public static void creatFile(){FileWriter fw=null;//为了防止最后close时发生的异常,这里先定义fw。try {fw=new FileWriter("D:\\ttt.txt",true);//创建,后面加的参数true表示如果文件存在就在该文件后面的写入fw.write("asdfasdf");//写} catch (Exception e) {e.printStackTrace();}finally{try {if(fw!=null){fw.close();//必须要放到try里面,因为就算是关闭,也有可能发生异常}} catch (Exception e2) {e2.printStackTrace();}}}/* * read 的异常处理模板痛writer的 */public static void readFile(){FileReader fr=null;try {fr=new FileReader("D:\\ttt.txt");char [] ch=new char[1024];//一般定义的数组长度为2Kint len=0;while((len=fr.read(ch))!=-1){//直到读出来的返回的是-1才不循环System.out.println(new String(ch,0,len));//字符数组转换成字符串打印}} catch (Exception e) {e.printStackTrace();}finally{try {if(fr!=null){fr.close();}} catch (Exception e2) {e2.printStackTrace();}}}//----------------------------------------public static void main(String[] args) {creatFile();//创建文件readFile();//读取文件}}


2.高效缓冲流BufferedReader/Writer

通过建立缓冲区,在流的基础上对其功能进行了增强

import java.io.*;/* * 缓冲字符流实现文本文件拷贝 * 这个Demo把要复制的文件名提供出去了 */public class Test1 {public static void copyText(String originName,String copyName)throws IOException{//originName被拷贝的文件名,copyName拷贝后的文件名BufferedReader br=null;//如果在try内声明,最后finally回收的时候找不到对象,所以在这声明BufferedWriter bw=null;try {bw=new BufferedWriter(new FileWriter(copyName,true));//buffered的装饰器模式br=new BufferedReader(new FileReader(originName));String line=null;//while((line=br.readLine())!=null){//BufferedReader使用装饰器模式,一次读一行bw.write(line);//写一行bw.newLine();//换一行bw.flush();}} catch (IOException e) {throw new IOException("复制失败");}finally{try {if(bw!=null){bw.close();}} catch (IOException e) {throw new IOException("关闭失败");}try {if(br!=null){br.close();}} catch (IOException e) {throw new IOException("关闭失败");}}}public static void main(String[] args) throws IOException {copyText("bbq.txt","bbq_1.txt");}}

2.1 LineNumberReader

在缓冲流的基础上LineNumberRead 中定义了setLineNumber与getLineNumber两个方法来设置与获取当前行号


3.字节流

3.1 FileInputStream和FileOutputStream

操作过程与FileWriter和FileReader类似,不过缓冲数组变成了byte[]

package com.cn.test;import java.io.*;/** * 拷贝图片文件 */public class CopyPicDemo {public static void copyPic(String readName,String writeName){FileInputStream fis=null;FileOutputStream fos=null;try {fis=new FileInputStream(readName);fos=new FileOutputStream(writeName);int len=0;byte[] buf=new byte[1024];//缓冲数组与返回的指示intwhile((len=fis.read(buf))!=-1){fos.write(buf,0,len);//每次写入数组的长度OutputStream不用flush}} catch (IOException e) {// TODO: handle exception}finally{//关闭释放资源try {if(fis!=null){fis.close();}} catch (IOException e) {// TODO: handle exception}try {if(fos!=null){fis.close();}} catch (IOException e) {// TODO: handle exception}}}public static void main(String[] args) {Long start=System.currentTimeMillis();//测试一下运行的速率copyPic("1.jpg","2.jpg");//执行函数Long end=System.currentTimeMillis();System.out.println(end-start);}}


4.高效字节流

BufferedInputStream和BufferedOutputStream同样的加入了缓冲数组,不过是用byte[]数组进行缓存,输出流与输入流之间用Int作为连接。


package com.cn.test;import java.io.*;import java.util.Date;/**复制Pic文件
*
*/public class BufferedIOCopyDemo {public static void main(String[] args) {Long start=System.currentTimeMillis();//计算运行时间copyPic("1.jpg","2.jpg");Long end=System.currentTimeMillis();System.out.println(end-start);}public static void copyPic(String readName,String writeName){//提供复制的文件名BufferedInputStream bis=null;BufferedOutputStream  bos=null;try {FileInputStream fis=new FileInputStream(readName);FileOutputStream fos=new FileOutputStream(writeName);bis=new BufferedInputStream(fis);bos=new BufferedOutputStream(fos);int len=0;while((len=bis.read())!=-1){bos.write(len);//outputStream中不用flush}} catch (IOException e) {// TODO: handle exception}finally{try {if(bis!=null){bis.close();}} catch (IOException e) {// TODO: handle exception}try {if(bos!=null){bis.close();}} catch (IOException e) {// TODO: handle exception}}}}


5.不同的输入输出设备

根据不同的使用需求,更改不同的源于输出对象。

例子:从键盘读入一行,转换后输出

package com.cn.test;import java.io.*;/** * 键盘录入,控制台输出 * @author Administrator * */public class SysIODemo {public static void main(String[] args)  {BufferedReader br=null;BufferedWriter bw=null;try {br=new BufferedReader(new InputStreamReader(System.in));//从键盘读入一行的方法bw=new BufferedWriter(new OutputStreamWriter(System.out));String line=null;while((line=br.readLine())!=null){//从输入到输出bw.write(line.toUpperCase());bw.newLine();bw.flush();}} catch (Exception e) {// TODO: handle exception}finally{try {if(br!=null)br.close();} catch (Exception e2) {// TODO: handle exception}}}}

例子:将异常信息写入文本

package com.cn.test;import java.io.*;/** * 将异常信息写入文本 */public class ExcpToTxtDemo {public static void main(String[] args)  {try {int [] i=new int[3];System.out.println(i[3]);//构造一个角标越界异常} catch (Exception e) {try {e.printStackTrace(new PrintStream("exceptiiiiiion.txt"));//将异常信息输出到文本文件} catch (FileNotFoundException e1) {e1.printStackTrace();}}}}

例子

package com.cn.test;import java.io.*;/** * 键盘录入写入文件(使用Print打印流) */public class WriteSaveDemo {public static void main(String[] args) {System.out.println("输入文字");BufferedReader br=null;PrintWriter pw=null;try{br=new BufferedReader(new InputStreamReader(System.in));pw=new PrintWriter(new FileWriter("FromSysIn.txt"),true);//从键盘写入String line=null;while((line=br.readLine())!=null){pw.println(line); //println不用newLine,构造函数中第二个参数设置为true,不用flush}}catch(IOException e){e.printStackTrace();}finally{try {if(br!=null)br.close();} catch (IOException e2) {// TODO: handle exception}try {if(pw!=null)br.close();} catch (IOException e2) {// TODO: handle exception}}}}






0 0